1, Sort three numbers
Method 1: function
#include<stdio.h> int MAX(int a,int b) { int max=a>b?a:b; return max; } int MIN(int a,int b) { int min=a<b?a:b; return min; } int main() { int a=0,b=0,c=0; scanf("%d %d %d",&a,&b,&c); int max=MAX(MAX(a,b),c); int min=MIN(MIN(a,b),c); int mid=a+b+c-max-min; printf("%d,%d,%d",max,mid,min); return 0; }
Method 2: exchange values in turn
#include<stdio.h> int main() { int a=0,b=0,c=0; scanf("%d %d %d",&a,&b,&c); if(a<b) { int tmp=a; a=b; b=tmp; }//A is the larger of a and B; if(a<c) { int tmp=a; a=c; c=tmp; } if(b<c) { int tmp=b; b=c; c=tmp; } printf("%d %d %d",a,b,c); return 0; }
Method 3: judge the condition with if statement
#include<stdio.h> int main() { int a=0,b=0,c=0; scanf("%d %d %d",&a,&b,&c); if(a<b&&b<c) printf("%d %d %d",a,b,c); if(a<c&&c<b) printf("%d %d %d",a,c,b); if(b<a&&a<c) printf("%d %d %d",b,a,c); if(b<c&&c<a) printf("%d %d %d",b,c,a); if(c<a&&a<b) printf("%d %d %d",c,a,b); if(c<b&&b<a) printf("%d %d %d",c,b,a); return 0; }
2, Maximum common divisor and minimum common multiple
1. Maximum common divisor
Find the maximum common divisor of integers a and B
(1) Method 1: ergodic method
#include<stdio.h> int main() { int a=0,b=0; scanf("%d %d",&a,&b); int ret=0; if(a>b) ret=b; else ret=a; while(1) { if(n%ret==0&&m%ret==0) { break; } else ret--; } prinft("The greatest common divisor is:%d",ret); return 0; }
(2) Rolling phase division
The rolling division method is also called Euclidean method. The specific calculation process is as follows:
#include<stdio.h> int main() { int a=0,b=0; scanf("%d %d",&a,&b); if(a<b) { int tmp=a; a=b; b=tmp; } int ret=0; while(ret=a%b) { a=b; b=ret; } printf("The greatest common divisor is:%d",b); return 0; }
2. Minimum common multiple:
The method of finding the least common multiple of two numbers is relatively simple. It only needs a*b divided by the maximum common divisor of a and B
#include<stdio.h> int main() { int a=0,b=0; scanf("%d %d",&a,&b); int num=b; if(a<b) { int tmp=a; a=b; b=tmp; } int ret=0; while(ret=a%b) { a=b; b=ret; } printf("The least common multiple is:%d",a*num/b); return 0; }
3, Year and date issues
Determine whether it is a leap year:
//Find all leap years from 1000 to 2000 #include<stdio.h> #include <stdbool.h> bool isrun(int n) { return (n%4==0&&n%400!=0)||(n%400==0); } int main() { int i=0; for(i=1000;i<=2000;i++) { if(isrun(i)) { printf("%d ",i); } } return 0; }
//The idea is to use Kim larsen formula or zelle formula char * dayOfTheWeek(int day, int month, int year) { if(month==1||month==2) { month+=12; year--; } int w=(day+2*month+3*(month+1)/5+year+year/4-year/100+year/400+1)%7; char *arr[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; return arr[w]; }
bool isleapyear(int n) { return (n%4==0&&n%400!=0)||(n%400==0); } int strtoint(char*str,int len)//len is the length of the string passed { int i=0; int sum=0; for(i=0;i<len;i++) { sum=sum*10+(str[i]-'0'); } return sum; } int dayofyear(char*date) { int monthday=[31,28,31,30,31,30,31,31,30,31,30,31}; int year=strtoint(date,4); int month=strtoint(date+5,2); int dat=strtoint(date+8,2); monthday[1]=isleapyear(year)?29:28; int allday=0; for(int i=1;i<month;i++) { allday=allday+monthday[i-1]; } alllday=allday+day; return allday; }
4, Prime number
Judge the number of a prime number
//Determine the number of prime numbers between 100 and 200 #include<stdio.h> #include<math.h> int main() { int i=0; int cnt=0; for(i=100;i<201;i++) { int flag=1; int j=0; for(j=2;j<=sqrt(i);j++) { if(i%j==0) { flag=0; break; } } if(flag==1) { printf("%d ", i); cnt++; } } printf("\n"); printf("The number of prime numbers is%d",cnt); return 0; }
Prime determination and Ehrlich screening
int countPrimes(int n) { if (n < 2) { return 0; } int isprime[n]; memset(isprime,0,sizeof(isprime)); int cnt=0; for(int i=2;i<n;i++) { if(!isprime[i])//2 is even prime { cnt++; if((long long)i*i<n) { for(int j=i*i;j<n;j+=i) { isprime[j]=1; } } } } return cnt; }
5, Several functions
1.strcmp()
The strcmp function compares the relationship between two strings
There are three conditions for the return value of strcmp function: greater than 0, equal to 0 and less than 0;
The return value of each of these functions represents the dictionary relationship from string1 to string2. Value relationship from string1 to string2
< 0 string1 is less than string2
0 string1 is the same as string2
>0 string1 is greater than string2
The size here is the ASCII value of the value string. The two strings compare the ASCII values of each character from left to right until different characters or '\ 0' are encountered.
#include <string.h> int main() { int i = 0; char password[20] = ""; for (i = 0; i < 3; i++) { printf("Please input a password:>"); scanf("%s", password); if (strcmp(password, "123456") == 0)//Compare whether two strings are equal. You can't use = =, but strcmp { //This function printf("Login successful\n"); break; } else { printf("Password error\n"); } if (i == 3) { printf("The password is wrong for three times. Exit the program\n"); } return 0; }
2.rand function, srand function and time function
1.rand function
Rand returns a pseudo-random number. The rand() function uses the linear congruence method. It is not a real random number. Because its period is very long, it can be regarded as random in a certain range.
The rand function returns a range from 0 to RAND_ The pseudo-random integer of max. Use the srand function to seed the pseudo-random number generator before calling Rand.
Generate random number for [0,1]
num=rand()/RAND_MAX;
2.srand function
The srand function is used to generate the seed of random numbers. Any other value of the seed sets the generator to a random starting point. rand retrieves the generated pseudo-random number. The sequence generated by calling rand before calling any calls to srand is the same as the sequence generated by calling srand from the 1 passed seed.
So how do we introduce a seed?
3.time function and timestamp
Function function: get the current calendar time or set the calendar time. Return value: time returns the time in seconds. The return value is converted to a timestamp.
Generally speaking, a timestamp is a complete and verifiable data that can represent the existence of a data at a specific point in time.
Generally speaking, we will use time(NULL) or time(0) to represent the seed of change, which is of type unsigned int. When we run the program at different times, there will be different random number seeds, so we can get different results. If we want to generate random numbers, we can use the following code
srand((unsign int) time(NULL)) int num=rand()
6, Simple guessing game
#include<stdio.h> #include<stdlib.h> #include<time.h> void menu() { printf("*******************************************\n"); printf("*******************************************\n"); printf("*******************************************\n"); printf("*************** 1.play ***************\n"); printf("*************** 0.exit ***************\n"); printf("*******************************************\n"); printf("*******************************************\n"); printf("*******************************************\n"); } void play() { int k = rand()%100+1; int a = 0; printf("The number is between 1 and 100\n"); for (int i = 5;i > 0;i--) { printf("Please enter the correct number:\n"); scanf("%d", &a); if (a > k) { printf("That's a big number\n"); } else if (a == k) { printf("Congratulations, you guessed right\n"); printf("very impressive\n"); break; } else { printf("This number is too small\n"); } if (i - 1 > 0) { printf("come on. You have%d Second chance\n", i - 1); } if (i - 1 == 0) { printf("Unfortunately, we have run out of opportunities. Please start over"); break; } } } int main() { srand((unsigned int)time(NULL));//Generating random time; int input = 0; do { printf("---------------Figure guessing game----------------\n"); menu(); printf("Please select Start Game:\n"); scanf("%d", &input); switch(input) { case 1: printf("Start the game\n"); play(); break; case 2: printf("Exit the game\n"); break; default : printf("Please enter the correct number:\n"); break; } } while (input); return 0; }