catalogue
Print large to small numbers (a-c)
Give two numbers and find the greatest common divisor of the two numbers
Print prime numbers between 100-200
Find the maximum of 10 integers
Figure guessing game
Number guessing game requirements:
1. Generate a random number
2. Guess the number (big, small, right)
3. You can play repeatedly
#include <stdio.h> #include <stdlib.h> #include <time.h> void menu() { printf("**********************************\n"); printf("*********** 1.play **********\n"); printf("*********** 0.exit **********\n"); printf("**********************************\n"); } void game() { int guess = 0; //1. Generate random number (1-100) int ret = rand()%100+1;//Call srand before using rand //printf("%d\n", ret); //2. Guess the number while (1) { printf("Please guess the number:>"); scanf("%d", &guess); if (guess < ret) { printf("Guess it's small\n"); } else if (guess>ret) { printf("Guess big\n"); } else { printf("You guessed right\n"); break; } } } int main() { int input = 0; //The time function gets the timestamp //Sets the starting point for generating random numbers srand((unsigned int)time(NULL));//Take the timestamp as the parameter do { //Print menu menu(); printf("Please select:>"); scanf("%d", &input);// 1 0 switch (input) { case 1: game();//Call game functions break; case 0: printf("Exit the game\n"); break; default: printf("Selection error, reselect\n"); break; } } while(input); return 0; }
The most important thing in the above code is how to generate a random number. In C language, there is a function dedicated to generating random numbers, which is Rand. Before using the rand function, srand should be used to set the starting point of generating random numbers. In srand, the time function should be used to obtain the timestamp, so that the random numbers generated by Rand are random all the time.
Print large to small numbers (a-c)
int main() { int a = 0; int b = 0; int c = 0; scanf("%d%d%d", &a, &b, &c); if (a<b) { int x = a; a = b; b = x; } if (a<c) { int x = a; a = c; c = x; } if (b<c) { int x = b; b = c; c = x; } printf("a=%d b=%d c=%d\n", a, b, c); return 0; }
In the above code, it is assumed that a is the largest. If a is smaller than b or c, it must be exchanged. Similarly, b and c are the same.
Print multiples of 3 in 1-100
int main() { int i = 1; while (i <= 100) { if (i % 3 == 0) { printf("%d\n", i); } i++; } return 0; }
Give two numbers and find the greatest common divisor of the two numbers
Method 1: rolling phase division
int main() { int a = 18; int b = 24; int c = 0; while (c = a%b) { a = b; b = c; } printf("%d\n", b); return 0; }
Method 2:
int main() { int a = 0; int b = 0; scanf("%d%d", &a, &b); int m = (a > b ? b : a); while (1) { if (a%m == 0 && b%m == 0) { printf("%d", m); } m--; } }
Print leap year (1000-2000)
Leap year: a leap year that can be divided by 4, a leap year that cannot be divided by 100, and a leap year that can be divided by 400
int main() { int x = 1000; while (x <= 2000) { if ((x % 4 == 0) && (x % 100 !=0)) { printf("%d ", x); } if (x % 400 == 0) { printf("%d ", x); } x++; } return 0; }
Print prime numbers between 100-200
int main() { int i = 0; int count = 0; for (i = 101; i <= 200; i++) { int j = 0; for (j = 2; j<i; j++) { if (i%j == 0) { break; } } if (j == i) { count++; printf("%d ", i); } } printf("\ncount = %d\n", count); return 0; }
Prime numbers can only be divided by 1 or itself. To print 100-200 direct prime numbers, first generate 100-200 direct numbers, and then divide them by 2 to their own numbers.
The above code can be optimized:
#include <math.h> int main() { int i = 0; int count = 0; for (i = 101; i <= 200; i++) { int j = 0; for (j = 2; j<sqrt(i); j++) { if (i%j == 0) { break; } } if (j == i) { count++; printf("%d ", i); } } printf("\ncount = %d\n", count); return 0; }
1-100 numbers with 9 directly
int main() { int i = 0; int count = 0; for (i = 1; i <= 100; i++) { if (i % 10 == 9 ) { count++; } if(i / 10 == 9) { count++; } } printf("%d\n", count); return 0; }
The number with 9 is the remainder. If it is equal to 9, it is a bit with 9. If it is divided by 10, it is a ten bit with 9.
Calculate the value of 1 / 1 - 1 / 2 + 1 / 3 - 1 / 4 + 1 / 5... + 1 / 99 - 1 / 100 and print the result
int main() { int i = 0; double sum = 0; int flag = 1; for (i = 1; i <= 100; i++) { sum += flag*(1.0 / i); flag = -flag; } return 0; }
Find the maximum of 10 integers
int main() { int arr[10] = { 0 }; int i = 0; int max = 0; for (i = 0; i<10; i++) { scanf("%d", &arr[i]); } // max = arr[0]; for (i = 1; i<10; i++) { if (arr[i]>max) max = arr[i]; } printf("max = %d\n", max); return 0; }
Assume that the maximum number is the first one, and then compare the first one with the later one to get the maximum value.
9 * 9 multiplication table
int main() { int i = 0; //Number of control rows for (i = 1; i <= 9; i++) { //Print each line with i expressions int j = 0; for (j = 1; j <= i; j++) { printf("%d*%d=%2d ", i, j, i*j); } printf("\n"); } return 0; }