Mom doesn't have to worry about my C language anymore!
Keep a good habit, point a praise, pay attention and then go! It is recommended to collect those summarized during the preparation for college entrance examination.
Algorithm summary
x + x + 1 + x + 2 \sqrt{x+x+1+x+2} x+x+1+x+2 Is an integer
Programming, find and output all the numbers x satisfying the condition "the square root of x+x+1+x+2 is an integer" in [1,98] (for example, output 2 and 11, because the sum of 2 + 3 + 4 is 9, 11 + 12 + 13 and 36, and their square roots 3 and 6 are integers)
#include <stdio.h> #include<math.h> void main() { int i,s; for(i=1;i<=98;i++) { s=3*i+3; if(sqrt(s)==(int)sqrt(s)){ printf("%d\n",i); } } }
s(n)=a+aa+aaa+aa...a
#include<stdio.h> int main() { int n,a,s=0,term=0; printf("a,n="); scanf("%d%d", &a, &n); for(int i=1; i<=n; i++) { term+=a; s+=term; a*=10; } printf("%d\n", s); return 0; }
1!+2!+3!+4!+...+20!
#include<stdio.h> int main() { int i; //It is defined as double type to obtain more accuracy, otherwise the obtained results cannot be accommodated double s=0,m=1; for(i=1; i<=20; i++) { m*=i; s+=m; } //When outputting, use 22.15e format to make the data width 22 and the number of small and medium digits in the digital part 15 printf("%22.15e\n", s); return 0; }
Raw rabbit (Fibonacci sequence)
Born rabbits: there are a pair of rabbits. From the third month after birth, a pair of rabbits will be born every month. When the little rabbit grows to the third month, another pair of rabbits will be born every month. If the rabbits do not die, what is the total number of rabbits every month?
#include<stdio.h> int main() { long int t1,t2; t1=t2=1; for(int i=1; i<=20; i++) { printf("%12ld%12ld", t1, t2); if(i%2==0) { printf("\n"); } t1=t1+t2; t2=t1+t2; } return 0; }
Odd numbers that can be composed of 0-7
tips: the first digit can only be 1-7; The last digit can only be 1, 3, 5, 7; The middle bit can be 0-8;
#include<stdio.h> int main() { //The initial value of cnt is 4, which means that there are 4 odd numbers composed of only one digit long total=4,cnt=4; int i; for(i=2; i<=8; i++) { printf("%d The number of odd digits is%ld\n", i-1, cnt); if(i<=2) { cnt*=7; } else { cnt*=8; } total+=cnt; } printf("%d The number of odd digits is%ld\n", i-1, cnt); printf("The total number of odd numbers is%ld\n", total); return 0; }
n-order clockwise spiral matrix
Print the n-order clockwise spiral square matrix specified by the user (n < 10)
#include<stdio.h> #include<string.h> int main() { int a[10][10]; int i,j,k=0,m,n; scanf("%d", &n); //The relationship between the number of layers m of the square matrix and the order n of the square matrix: m=(n+1)/2 if(n%2==0) { m=n/2; } else { m=(n+1)/2; } for(i=0; i<m; i++) { for(j=i; j<n-i; j++) { k++; a[i][j]=k; } for(j=i+1; j<n-i; j++) { k++; a[j][n-i-1]=k; } for(j=n-i-2; j>=i; j--) { k++; a[n-i-1][j]=k; } for(j=n-i-2; j>=i+1; j--) { k++; a[j][i]=k; } } for(i=0; i<n; i++) { for(j=0; j<n; j++) { printf("%5d", a[i][j]); } printf("\n"); } return 0; }
Find the heel of a quadratic equation of one variable
#include<stdio.h> #include<math.h> int main(){ double a,b,c,x1,x2,disc,realpart,imagpart; scanf("%lf%lf%lf", &a, &b, &c); //Error less than 10 ^ (- 6) if(fabs(a)<=1e-6) { printf("Not a quadratic equation of one variable\n"); } else { disc=b*b-4*a*c; //Judge whether the discriminant is less than a small number, 1e-6=10^(-6). If it is less than this number, disc is considered equal to 0 and has two equal real roots if(fabs(disc)<=1e-6) { printf("Two equal real roots:%8.4f\n", -b/(2*a)); } //Disc > 0, there are two unequal real roots else if(disc>1e-6) { x1=(-b+sqrt(disc))/2*a; x2=(-b-sqrt(disc))/2*a; printf("Two unequal real roots:%8.4f and %8.4f\n", x1, x2); } //Disc < 0, there are two unequal conjugate complex heels else { realpart=-b/(2*a); imagpart=sqrt(-disc)/(2*a); printf("Two unequal conjugate complexes:\n"); printf("%8.4f+%8.4fi\n", realpart, imagpart); printf("%8.4f-%8.4fi", realpart, imagpart); } } return 0; }
Pointer exchange two variables (2)
Enter two integers and output the two numbers in the order of first large and then small. Please program with pointer
#include<stdio.h> int main() { int a,b,*p,*q,*r; printf("Please enter two integers:\n"); scanf("%d,%d",&a,&b); p=&a; q=&b; if(a<b) { r=p; p=q; q=r; } printf("Larger digit%d,Smaller digit%d. \n",*p,*q); return 0; }
#include<stdio.h> int main() { int a,b,*p,*q,t; printf("Please enter two integers:\n"); scanf("%d,%d",&a,&b); p=&a; q=&b; if(a<b) { t=*p; *p=*q; *q=t; } printf("Larger digit%d,Smaller digit%d. \n",a,b); return 0; }
Reverse order output of two-dimensional array
#include<stdio.h> #define M 2 #define N 3 int main() { void inverse(int a[][N], int b[][N]); int a[M][N],b[M][N],i,j; for(i=0; i<M; i++) { for(j=0; j<N; j++) { scanf("%d", &a[i][j]); } } inverse(a,b); for(i=0; i<M; i++) { for(j=0; j<N; j++) { printf("%d\t", b[i][j]); } //Remember to wrap a line after the output of a two-dimensional array printf("\n"); } return 0; } void inverse(int a[][N], int b[][N]) { int i,j; for(i=0; i<M; i++) { for(j=0; j<N; j++) { b[M-i-1][N-j-1]=a[i][j]; } } }
One dimensional array reverse order output (2)
Store n integers in array a in reverse order, using array name as parameter and pointer as parameter respectively
Method 1: use the array name as the parameter
#include<stdio.h> int main() { void reverse(int a[], int n); int a[5]={3,43,-4,45,5}; int i; reverse(a,5); for(i=0; i<5; i++) { printf("%d\t", a[i]); } return 0; } void reverse(int a[], int n) { int i,j,t,middle; middle=(n-1)/2; for(i=0; i<=middle; i++) { j=n-1-i; t=a[i]; a[i]=a[j]; a[j]=t; } }
Method 2: use pointer as parameter
#include<stdio.h> int main() { void reverse(int *a, int n); int a[5]={3,43,-4,45,5}; int i; reverse(a,5); for(i=0; i<5; i++) { printf("%d\t", a[i]); } return 0; } void reverse(int *a, int n) { int *i,*j,*p,t,middle; middle=(n-1)/2; i=a; j=n-1+a; p=middle+a; for( ; i<=p; i++,j--) { t=*i;//i points to the first element of the array *i=*j;//j points to the last element of the array *j=t;//p points to the middle value } }
Find the most value of one-dimensional array
Input 10 numbers and output the element with the largest value and the number. Array elements are required as function arguments
#include<stdio.h> int main() { int max(int x,int y); int a[10],i,m,idx; for(i=0; i<10; i++) { scanf("%d", &a[i]); } m=a[0]; for(i=1; i<10; i++) { if(max(m,a[i])>m) { m=max(m,a[i]); idx=i; } } printf("%d %d\n", m, idx+1); return 0; } int max(int x,int y) { return x>y?x:y; }
Two dimensional array output value
Output the value of two-dimensional array elements with pointer variables
tips: * (p+i)+j is the element address of row i and column j of the two-dimensional array, while * (* (p+i)+j) is the value of row i and column j elements
#include<stdio.h> int main(){ int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12}; int (*p)[4]; int i,j; p=a; for(i=0;i<3;i++){ for(j=0;j<4;j++){ printf("%2d ", *(*(p+i)+j)); } } return 0; }
*Saddle finding point of two-dimensional array
Find the saddle point in a two-dimensional array, that is, the element at this position is the largest on the row and the smallest on the column. There may also be no saddle point.
#include<stdio.h> #define N 4 #define M 5 int main() { int i,j,k,a[N][M],max,maxj,flag; for(i=0; i<N; i++) { for(j=0; j<M; j++) { scanf("%d", &a[i][j]); } } for(i=0; i<N; i++) { max=a[i][0]; maxj=0; //Find the maximum number in line i for(j=0; j<M; j++) { if(a[i][j]>max) { //Store the maximum number of rows in max max=a[i][j]; maxj=j; } flag=1; for(k=0; k<N; k++) { //Compare the maximum number with its elements in the same column if(max>a[k][maxj]) { flag=0; continue; } } } if(flag) { printf("a[%d][%d]=%d\n", i, maxj, max); break; } } if(!flag) { printf("It is not exist!\n"); } return 0; }
Case conversion
Design function to convert lowercase letters into uppercase letters in a string
#include<stdio.h> #include<ctype.h> int main() { void convert(char *s); char s[]="124asdfAJLJAsdj4Nr"; convert(s); puts(s); return 0; } void convert(char *s) { while(*s!='\0') { if(islower(*s)) { *s=toupper(*s); } s++; } }
The greater of three integers
Call the function to find the larger of the three integers
#include<stdio.h> int main() { int max(); extern int a,b,c; scanf("%d%d%d", &a, &b, &c); printf("max=%d\n", max()); } int a,b,c; int max() { int m; m=a>b?a:b; if(c>m) { m=c; } return m; }
Count the number of words
#include<stdio.h> int main() { char a[81]; int w=0,n=0; gets(a); for(int i=0; a[i]!='\0'; i++) { if(a[i]==' ') { w=0; } else if(w==0) { w=1; n++; } } printf("There are %d words int this line.\n", n); }
Number of Statistics
Count the number of 0 to 9 numbers in the given data
#include<stdio.h> int main() { int i,data[20]={0,1,2,2,3,3,6,3,8,0,1,1,3,4,5,1,7,8,9,3} ; int cnt[10]={0} ; for(i=0; i<20; i++) { cnt[data[i]]+=1; } for(i=0; i<10; i++){ printf("number%d Number of:%d\n",i,cnt[i]); } return 0; }
Count the number of characters
There is an article, a total of three lines of text, each line has 80 characters. It is required to count the number of English capital letters, lowercase letters, numbers, spaces and other characters
#include<stdio.h> int main() { char a[3][80]; int i,j,Letter=0,letter=0,digital=0,space=0,other=0; for(i=0; i<3; i++) { gets(a[i]); for(j=0; j<80&&a[i][j]!='\0'; j++) { if(a[i][j]>='A'&&a[i][j]<='Z') { Letter++; } else if(a[i][j]>='a'&&a[i][j]<='z') { letter++; } else if(a[i][j]>='0'&&a[i][j]<='9') { digital++; } else if(a[i][j]==' ') { space++; } else { other++; } } } printf("Letter=%d\nletter=%d\ndigital=%d\nspace=%d\nother=%d\n", Letter, letter, digital, space, other); return 0; }
Delete specified character
#include<stdio.h> int main() { void del_char(char a[],char n); char a[80],n; gets(a); n=getchar(); del_char(a,n); return 0; } void del_char(char a[],char n) { char p[80]; int i,j; for(i=0,j=0; a[i]!='\0'; i++) { if(n!=a[i]) { p[j++]=a[i]; } } p[j]='\0'; puts(p); }
Maximum string found
There are three strings. It is required to find the largest one
#include<stdio.h> #include<string.h> int main() { char str[3][20]; char string[20]; int i; for(i=0; i<3; i++) { gets(str[i]); } if(strcmp(str[0],str[1])>0) { strcpy(string,str[0]); } else { strcpy(string,str[1]); } if(strcmp(str[2],string)>0) { strcpy(string,str[2]); } printf("\nthe largest string is:%s\n", string); return 0; }
Find the approximate value of pi
Use pi/4=1-1/3+1/5-1/7 +... Formula to calculate the approximate value of PI until the absolute value of a certain item is found to be less than 1e-6 (this item is not accumulated).
#include<stdio.h> #include<math.h> int main() { int sign=1; double pi=0.0,n=1.0,term=1.0; while(fabs(term)>=1e-6) { pi+=term; n+=2; sign=-sign; term=sign/n; } printf("pi=%10.8f\n", pi*4); return 0; }
String copy (5)
tips: copy n characters from the m-th character in the string to another string
- Use of gets() and puts()
- The gets() function is used to read the string from the standard input device (keyboard) until the end of the newline character, but the newline character will be discarded, and then the '\ 0' character will be added at the end
- Put() stops when it encounters' \ 0 '(i.e. character terminator) when outputting a string; And automatically adds a newline character to the end of the string
#include<stdio.h> int main() { char a[100],b[100]; int m,n,i; gets(a); scanf("%d %d", &m, &n); //I is added from 0, and the condition is less than the number to be copied (i.e. I < n), because the subscript starts from zero after putting in the new array //It seems to be nonsense, because the array subscript starts from 0, hahaha for(i=0; i<n; i++) { //If it's not easy to understand, you can go in algebra b[i]=a[m+i-1]; } printf("%s\n", b); return 0; }
Copy string a to string b
Method 1: subscript method
#include<stdio.h> int main(){ char a[]="I am a student!",b[100]; int i; // *(a+1) is equivalent to a[i]. Similarly: * (b+i) is equivalent to b[i] for(i=0; *(a+i)!='\0'; i++) { *(b+i)=*(a+i); } *(b+i)='\0'; printf("%s\n", a); //You can directly use puts(b) to output in the character array, or use the format string% s for(i=0; b[i]!='\0'; i++) { printf("%c", b[i]); } return 0; }
Method 2: pointer method
#include<stdio.h> int main(){ char a[]="I am a student!",b[100],*p1,*p2; for(p1=a,p2=b; *p1!='\0'; p1++,p2++) { *p2=*p1; } *p2='\0'; printf("string a is:%s\n", a); printf("string b is:%s\n", b); return 0; }
Copy string with function call (use character array as parameter; use character pointer variable for formal parameter)
Method 1: the formal parameter uses the character array as the parameter
#include<stdio.h> int main() { void copy_string(char a[], char b[]); char a[100]="I am a student!",b[100]; copy_string(a,b); puts(a); puts(b); return 0; } void copy_string(char a[], char b[]) { int i; for(i=0; a[i]!='\0'; i++) { b[i]=a[i]; } b[i]='\0'; }
Method 2: pointer parameters for formal parameters
#include<stdio.h> int main() { void copy_string(char a[], char b[]); char *a="I am a student!",b[100]; copy_string(a,b); puts(a); puts(b); return 0; } void copy_string(char *a, char *b) { for( ; *a!='\0'; a++,b++) { *b=*a; } *b='\0'; }
String reverse order
#include<stdio.h> #include<string.h> int main() { //The intermediate variable t is a switch char a[100],t; int i,j,k; scanf("%s", &a); k=strlen(a); //Don't be confused when you see this for statement. Pay attention to `` that will do for(i=0,j=k-1; i<k/2; i++,j--) { t=a[i]; a[i]=a[j]; a[j]=t; } printf("%s\n", a); return 0; }
String insertion
Input a string a from the keyboard and insert another input string b after the largest element in the string
#include<stdio.h> #include<string.h> int main() { char a[100],b[]="@#@",max; int i=0,j; gets(a); max=a[i]; for(i=0; a[i]!='\0'; i++) { if(max<a[i]) { max=a[i]; j=i; } } i=strlen(a)+strlen(b)-1; for( ; i>j; i--) { a[i]=a[i-strlen(b)]; } j=0; i++; while(b[j]!='\0') { a[i]=b[j]; printf("%c\n", a[i]); i++; j++; } puts(a); return 0; }
string comparison
Compare two strings. If S1 > S2, output a positive number; If s1=s2, output 0; If S1 < S2, a complex number is output, and the output value is the difference between the ASCII codes of the corresponding characters of the two strings.
#include<stdio.h> int main() { char s1[80],s2[80]; gets(s1); gets(s2); int i=0,rel; while(s1[i]==s2[i]&&s1[i]!='\0') { i++; } if(s1[i]=='\0'&&s2[i]=='\0') { rel=0; } else { rel=s1[i]-s2[i]; } printf("%d\n", rel); return 0; }
Input two strings a and b from the keyboard. It is required to connect the first five characters of string b to string a without using the library function strcat; If the length of b is less than 5, all elements of b are connected to a
#include<stdio.h> int main() { char a[100],b[100]; int i=0,j; gets(a); gets(b); while(a[i]!='\0') { i++; } for(j=0; j<5&&b[j]!='\0'; j++) { a[i++]=b[j]; } a[i]='\0'; puts(a); return 0; }
multiplication table
#include<stdio.h> int main() { int i,j; for(i=1; i<=9; i++) { for(j=1; j<=i; j++) { printf("%dx%d=%-3d ", j, i, i*j); } printf("\n"); } return 0; }
Yang Hui triangle
#include <stdio.h> #define N 10 int main() { int a[N][N], i, j; for(i=0; i<N; i++) { //Column must be less than or equal to row, or wrap for(j=0; j<=i; j++) { //The value of the zeroth column and the rows and columns are equal (that is, the two beveled edges) is 1 if(i==j || j==0) { a[i][j]=1; } else { //Previous row, previous column + previous row, same column a[i][j]=a[i-1][j-1]+a[i-1][j]; } printf("%6d", a[i][j]); } printf("\n"); } return 0; }
Half search method
Input an integer from the keyboard and find the position of the number in 10 ordered integer arrays a by half search method. If the number is not in a, the corresponding information will be printed out
#include<stdio.h> int main() { int a[10]={1,3,5,7,9,11,13,15,17,19},n; int low=0,high=9,mid; scanf("%d", &n); while(low<=high) { mid=(low+high)/2; if(n==a[mid]) { break; } else if(n>a[mid]) { low=mid+1; } else { high=mid-1; } } if(low<=high) { printf("Found! position is %d\n", mid); } else { printf("Not found %d\n", n); } return 0; }
A hundred dollars for a hundred chickens
A chicken is worth five, a hen is worth three, a chicken is worth three, and a chicken is worth one. A hundred dollars buys a hundred chickens. Ask the chicken Weng, the mother and the chick? Output all the buying methods, and at least one chicken of each type
#include <stdio.h> int main( ) { int roo,hen,chick; for(roo=1; roo<20; roo++) { for(hen=1; hen<33; hen++) { chick=100-roo-hen; if(roo*5+hen*3+chick/3==100) { printf("roo=%d, hen=%d, chick=%d\n", roo, hen, chick); } } } return 0; }
greatest common divisor
#include<stdio.h> int main() { int m,n,t,r,p=0; scanf("%d%d", &m, &n); p=m*n; if(m>n) { t=m; m=n; n=t; } while(n!=0) { r=m%n; m=n; n=r; } printf("%d %d", m, p/m); return 0; }
The table tennis match
Two table tennis teams compete, each with three players. Team A has A, B and C3 people, and team B has x, Y and Z3 people. It is known that A does not compete with X, C does not compete with X and Z. please program to find out the list of 3 pairs of players
#include <stdio.h> int main() { char i,j,k; for(i='X'; i<='Z'; i++) { for(j='X'; j<='Z'; j++) { if(i!=j) { for(k='X'; k<='Z'; k++) { if(k!=i&&k!=j) { if(i!='X'&&k!='X'&&k!='Z') { printf("A---%c\nB---%c\nC---%c\n", i, j, k); } } } } } } return 0; }
Judge the day
Give the year, month and day, and calculate the day of the year
#include<stdio.h> int main() { int sum_day(int month,int day); int leap(int year); int year,month,day,days=0; scanf("%d-%d-%d", &year, &month, &day); days=sum_day(month,day); if(leap(year)&&month>=3) { days+=1; } printf("%d\n", days); return 0; } int sum_day(int month,int day) { int day_tab[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; for(int i=1; i<month; i++) { day+=day_tab[i]; } return day; } int leap(int year) { int leap=(year%4==0&&year%100!=0)||year%400==0; return leap; }
Insert sort
#include<stdio.h> int main() { int a[11]={2,12,13,22,49,58,132,144,194,920}; int i,j,t1,t2,n; scanf("%d", &n); int len=sizeof(a)/sizeof(a[0]); if(n>=a[9]) { a[10]=n; } else { for(i=0; i<len-1; i++) { if(n<a[i]) { t1=a[i]; a[i]=n; for(j=i+1; j<len; j++) { t2=a[j]; a[j]=t1; t1=t2; } break; } } } for(i=0; i<len; i++) { printf("%5d", a[i]); } return 0; }
Bubble sorting
#include<stdio.h> int main() { int a[10]={2,12,43,-222,49,58,232,4,-594,90}; int i,j,t; int len=sizeof(a)/sizeof(a[0]); for(i=0; i<len-1; i++) { for(j=0; j<len-1-i; j++) { if(a[j]>a[j+1]) { t=a[j]; a[j]=a[j+1]; a[j+1]=t; } } } for(i=0; i<len; i++) { printf("%5d", a[i]); } return 0; }
Select sort
#include<stdio.h> int main() { int a[10]={2,12,43,-222,49,58,232,4,-594,90}; int min,t,i,j; int len=sizeof(a)/sizeof(a[0]); for(i=0; i<len-1; i++) { min=i; for(j=i+1; j<len; j++) { if(a[min]>a[j]) { min=j; } } t=a[i]; a[i]=a[min]; a[min]=t; } for(i=0; i<len; i++) { printf("%5d", a[i]); } return 0; }
Narcissistic number
#include<stdio.h> int main() { int a,b,c; for(int i=100; i<=999; i++) { a=i/100; b=i/10%10; c=i%10; if(i==a*a*a+b*b*b+c*c*c) { printf("%d\n", i); } } return 0; }
Reverse order output (2)
Output in reverse order: your program will read in a series of positive integers. You don't know the number of positive integers in advance. Once you read - 1, it means that the input is over. Then, the read numbers are output in the reverse order of input, excluding - 1 at the end of the last identification
#include<stdio.h> int main(){ int n,i,a[10]; for(i=0; ; ) { scanf("%d", &n); if(n==-1) { break; } a[i++]=n; } for(i=i-1; i>=0; i--) { printf("%d\t", a[i]); } return 0; }
Three digits in reverse order:
#include<stdio.h> int main(){ int n,a,b,c; scanf("%d", &n); a=n/100; b=n/10%10; c=n%10; n=c*100+b*10+a; printf("%d", n); return 0; }
Sequence summation
There is a fractional sequence: 2 / 1, 3 / 2, 5 / 3, 8 / 5, 13 / 8, 21 / 13... Find the sum of the first 20 items of this sequence
#include<stdio.h> int main() { int i; double s=0,a=2,b=1,t; for(i=1; i<=20; i++) { s+=a/b; t=a; a=a+b; b=t; } printf("%16.10f\n", s); return 0; }
Print graphics
* *** ***** ******* ***** *** *
#include <stdio.h> #include<math.h> int main() { int i,j,k; for(i=1; i<=4; i++) { for(j=1; j<=5-i; j++) { printf(" "); } for(k=1; k<=2*i-1; k++) { printf("*"); } printf("\n"); } for(i=1; i<=3; i++) { for(j=1; j<=i+1; j++) { printf(" "); } for(k=1; k<=7-2*i; k++) { printf("*"); } printf("\n"); } return 0; }
* * * * * * * * * * * * * * * * * * * * * * * * *
#include <stdio.h> #include<math.h> int main() { int i,j,k; for(i=0; i<5; i++) { for(j=1; j<=i; j++) { printf(" "); } for(k=0; k<5; k++) { printf("* "); } printf("\n"); } return 0; }
Message password (2)
A line of message has been translated into password according to the following rules: a = > Z, B = > y, C = > x... A = > Z, B = > y, C = > x... That is, the first letter becomes the 26th letter, the ith letter becomes the (26i + 1) letter, and the non alphabetic characters remain unchanged. Program to translate the password back to the original text, and output the password and the original text
#include<stdio.h> int main() { char a[81]; int i; gets(a); for(i=0; a[i]!='\0'; i++) { if(a[i]>='A'&&a[i]<='Z') { //The ith letter: 26-i + 1 = > 26 - (a [i] - 64) + 1 = > 91-a [i] //ASCII code of this letter: 91-a [i] + 64 = > 115-a [i] a[i]=155-a[i]; } else if(a[i]>='a'&&a[i]<='z') { //The ith letter: 26-i + 1 = > 26 - (a [i] - 96) + 1 = > 123-a [i] //ASCII code of this letter: 123-a [i] + 96 = > 219-a [i] a[i]=219-a[i]; } } puts(a); return 0; }
Change the letter a into the letter E, a into e, that is, the fourth letter after it, W into a, X into B, Y into C and Z into D. Such as "China!" Convert to "Glmre!".
#include<stdio.h> int main() { char c; while((c=getchar())!='\0') { if((c>='a'&&c<='z')||(c>='A'&&c<='Z')) { if((c>='w'&&c<='z')||(c>='W'&&c<='Z')) { c=c+4-26; } else { c=c+4; } } printf("%c", c); } return 0; }
Free fall
A ball falls freely from the height of 100m. After each landing, it jumps back to half of the original height, falls again and rebounds again. Ask how many meters it passes and how high it bounces for the 10th time.
#include<stdio.h> int main() { double sn=100,hn=sn/2; int n; for(n=2; n<=10; n++) { sn+=2*hn; hn/=2; } printf("The 10th landing passed a total of%f rice\n", sn); printf("10th rebound%f rice\n", hn); return 0; }
Monkeys eat peaches
On the first day, the monkey picked several peaches and ate half of them immediately. It was not fun, so he ate another one. After that, I ate the remaining half and one every morning. By the morning of the 10th day, there was only one left. Ask how many peaches you picked on the first day.
#include<stdio.h> int main() { int day,x1,x2; day=9; x2=1; while(day>0) { x1=(x2+1)*2; x2=x1; day--; } printf("total=%d\n", x1); }
Recursively find n!
#include <stdio.h> int main() { int f(int n); int n; scanf("%d", &n); printf("%d!=%d\n", n, f(n)); return 0; } int f(int n) { int x; if(n<0) { printf("n<0,data error!"); } else if(n==0||n==1) { x=1; } else { x=f(n-1)*n; } return x; }
Student age
There are five students sitting together. The first student is 10 years old, the second student is 2 years older than the first student, the third student is 2 years older than the second student... And so on. The fifth student is 2 years older than the fourth student. Find the age of the fifth student.
#include <stdio.h> int main() { int age(int n); printf("%d\n", age(5)); return 0; } int age(int n) { int c; if(n==1) { c=10; } else { c=age(n-1)+2; } return c; }
Figure Space
Write a function and input a 4-digit number. It is required to output these 4 numeric characters, but there is a space between each two numbers. If 1990 is input, "1 9 0" shall be output.
#include<stdio.h> #include<string.h> int main() { void insert(char arr[]); char str[80]; printf("input four digits:"); gets(str); insert(str); return 0; } void insert(char arr[]) { for(int i=strlen(arr); i>0; i--) { arr[2*i]=arr[i]; arr[2*i-1]=' '; } puts(arr); }
Array reverse order (2)
Store n integers in array a in reverse order
#include<stdio.h> int main() { void fun(int a[],int n); int a[10]={10,20,30,40,50,60,70,80,90,100}; printf("original:\n"); for(int i=0; i<10; i++) { printf("%5d", a[i]); } fun(a,10); printf("\noriginal:\n"); for(int i=0; i<10; i++) { printf("%5d", a[i]); } return 0; } void fun(int a[],int n) { int i,j,t,m=(n-1)/2; for(i=0; i<=m; i++) { j=n-1-i; int t=a[i]; a[i]=a[j]; a[j]=t; } }
With pointer
#include<stdio.h> int main() { void fun(int a[],int n); int a[10]={10,20,30,40,50,60,70,80,90,100}; printf("original:\n"); for(int i=0; i<10; i++) { printf("%5d", a[i]); } fun(a,10); printf("\noriginal:\n"); for(int i=0; i<10; i++) { printf("%5d", a[i]); } return 0; } void fun(int a[],int n) { int *p,*i,*j,t,m=(n-1)/2; i=a; j=n-1+a; p=a+m; for( ; i<=p; i++,j--) { t=*i; *i=*j; *j=t; } }
*Longest word
Write a function, input a line of characters, and output the longest word in this string
#include<stdio.h> #include<string.h> int main() { int alphabetic(char); int longest(char []); int i; char line[100]; printf("input one line:\n"); gets(line); printf("The longest word is:"); for(i=longest(line); alphabetic(line[i]); i++) { printf("%c", line[i]); } return 0; } int alphabetic(char c) { if((c>='a'&&c<='z')||(c>='A'&&c<='Z')) { return 1; } else { return 0; } } int longest(char string[]) { int len=0,i,length=0,flag=1,place=0,point; for(i=0; i<=strlen(string); i++) { if(alphabetic(string[i])) { if(flag) { //point indicates the starting position of the current word (indicated by subscript) point=i; //flag=1 indicates the beginning of the word, and 0 indicates the beginning of the word flag=0; } else { //len indicates the accumulated number of letters in the current word len++; } } else { flag=1; if(len>=length) { //Length indicates the length of the longest word in the previous word //place indicates the starting position of the longest word length=len; place=point; len=0; } } } return place; }
*Dichotomy
Find the heel of the following equation between (- 10,10) by dichotomy:
[the external link image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-0MhjuBRO-1622860361151)(file://D:\Study\blogImg\image-20210526154956234.png?lastModify=1622017241)]
#include<stdio.h> #include<math.h> int main() { float x0,x1,x2,fx0,fx1,fx2; do { printf("enter x1 & x2:"); scanf("%f%f", &x1, &x2); fx1=x1*((2*x1-4)*x1+3)-6; fx2=x2*((2*x2-4)*x2+3)-6; }while(fx1*fx2>0); do { x0=(x1+x2)/2; fx0=x0*((2*x0-4)*x0+3)-6; if(fx0*fx1<0) { x2=x0; fx2=fx0; } else { x1=x0; fx1=fx0; } }while(fabs(fx0)>=1e-5); printf("x=%6.2f\n", x0); return 0; }
*Magic cube array
#include<stdio.h> int main() { int a[20][20]={0},n,i,j,k; scanf("%d", &n); //1 in the middle column of the first row j=n/2+1; a[1][j]=1; i=1; for(k=2; k<=n*n; k++) { //From 2 to n*n, each number shall be stored according to the following rules: //The number of rows stored in each number is 1 less than that of the previous number, and the number of columns is 1 more i=i-1; j=j+1; if(i<1&&j>n) { i=i+2; j=j-1; } else { //If the number of rows of the previous number is 1, the number of rows of the next number is n (referring to the lowest row) if(i<1) { i=n; } //When the number of columns of the previous number is n, the number of columns of the next number should be 1 if(j>n) { j=1; } } if(a[i][j]==0) { a[i][j]=k; } else { i=i+2; j=j-1; a[i][j]=k; } } for(i=1; i<=n; i++) { for(j=1; j<=n; j++) { printf("%5d", a[i][j]); } printf("\n"); } return 0; }
*Iterative method (3)
Find x=sqrt(a) by iterative method. The iterative formula for finding the square root is
It is required that the absolute value of the difference of x calculated twice before and after is less than 1e-5.
#include<stdio.h> #include<math.h> int main() { float a,x0,x1; scanf("%f", &a); x0=a/2; x1=(x0+a/x0)/2; do { x0=x1; x1=(x0+a/x0)/2; }while(fabs(x0-x1)>=1e-5); printf("The square root of %5.2f is %8.5f\n", a, x1); return 0; }
Use Newton iterative method to find the root of the following equation near 1.5:
Newton's iterative formula is:
#include<stdio.h> #include<math.h> int main() { double x1,x0,f,f1; x1=1.5; do { x0=x1; f=((2*x0-4)*x0+3)*x0-6; f1=(6*x0-8)*x0+3; x1=x0-f/f1; }while(fabs(x0-x1)>=1e-5); printf("The root of equation is %5.2f\n", x1); return 0; }
Newton iterative method is used to find the root. Equation is
, the values of coefficients a, b, c and d are 1, 2, 3 and 4, which are input by the main function. Find a real root of x near 1. After finding the root, it is output by the main function.
#include<stdio.h> #include<math.h> int main() { float solut(float a,float b,float c,float d); float a,b,c,d; scanf("%f%f%f%f", &a, &b, &c, &d); printf("x=%10.7f\n", solut(a,b,c,d)); return 0; } float solut(float a,float b,float c,float d) { float x=1,x0,f,f1; do { x0=x; f=x0*((a*x0+b)*x0+c)+d; f1=x0*(3*a*x0+2*b)+c; x=x0-f/f1; }while(fabs(x-x0)>=1e-3); return x; }
*Legendre polynomials of order n
#include<stdio.h> #include<math.h> int main() { float p(int n,int x); int n,x; scanf("%d%d", &n, &x); printf("n=%d,x=%d\n", n, x); printf("P%d(%d)=%6.2f\n", n, x, p(n,x)); return 0; } float p(int n,int x) { if(n==0) { return 1; } else if(n==1) { return x; } else { return (2*n-1)*x*p(n-1,x)-(n-1)*p(n-2,x)/n; } }
Hanoi
#include<stdio.h> int main() { void hanoi(int n,char one,char two,char three); int m; scanf("%d", &m); hanoi(m,'A','B','C'); return 0; } void hanoi(int n,char one,char two,char three) { void move(char x,char y); if(n==1) { move(one,three); } else { hanoi(n-1,one,three,two); move(one,three); hanoi(n-1,two,one,three); } } void move(char x,char y) { printf("%c-->%c\n", x, y); }
Number of palindromes (2)
Method 1:
#include<stdio.h> #include<string.h> int main( ) { char a[100]; int i,j; gets(a); j=strlen(a)-1; for(i=0; i<j; i++,j--) { if(a[i]!=a[j]) { break; } } if(i>=j) { printf("The string is a palindrome\n"); } else { printf("no\n"); } return 0; }
Pointer: usage method 2
#include<stdio.h> #include<string.h> int main( ) { char a[100],*p1,*p2; gets(a); p1=a; p2=a+strlen(a)-1; for( ; p1<p2; p1++,p2--) { if(*p1!=*p2) { break; } } if(p1>=p2) { printf("The string is a palindrome\n"); } else { printf("no\n"); } return 0; }
perfect number
Output completion within 1000
#include<stdio.h> int main() { int i,j,s; for(i=2; i<1000; i++) { s=0; for(j=1; j<i; j++) { if(i%j==0) { s+=j; } } if(i==s) { printf("%d, its factors are ", i); for(j=1; j<i; j++) { if(i%j==0) { printf("%d ", j); } } printf("\n"); } } return 0; }
Prime (4)
Enter an integer n greater than 3 to determine whether it is prime
#include<stdio.h> int main() { int n,i; scanf("%d", &n); for(i=2; i<=n-1; i++) { if(n%i==0) { break; } } //If n can be divided by an integer between 2~(n-1), the break statement must lead to the early end of the loop //That is, if i does not reach the value of n, the loop terminates if(i<n) { printf("%d is not a prime.\n", n); } else { printf("%d is a prime.\n", n); } }
#include<stdio.h> #include<math.h> int main() { int n,i; scanf("%d", &n); int k=sqrt(n); for(i=2; i<=k; i++) { if(n%i==0) { break; } } //If n cannot be divided by any integer between 2 and K, i will add 1 after the last cycle if(i<=k) { printf("%d is not a prime.\n", n); } else { printf("%d is a prime.\n", n); } }
Find all prime numbers between 100 and 200
#include<stdio.h> #include<math.h> int main() { int n,i,k,m=0; for(n=101; n<=200; n+=2) { k=sqrt(n); for(i=2; i<=k; i++) { if(n%i==0) { break; } } //If I > = K + 1, it means that n has not been divided if(i>=k+1) { printf("%5d", n); m++; } if(m%10==0) { printf("\n"); } } return 0; }
Finding prime numbers within 100 by deletion method
#include<stdio.h> #include<math.h> int main() { int a[101]; int i,j,n; for(i=1; i<101; i++) { a[i]=i; } a[1]=0;//Dig out 1 first, because 1 is not prime for(i=2; i<sqrt(100); i++) { for(j=i+1; j<=100; j++) { if(a[i]!=0&&a[j]!=0) { if(a[j]%a[i]==0) { a[j]=0; } } } } printf("\n"); for(i=2,n=0; i<=100; i++) { if(a[i]!=0) { printf("%5d", a[i]); n++; } if(n==10) { printf("\n"); n=0; } } printf("\n"); return 0; }
recursion
Calculate e = 1 + 1 / 1+ 1/2! + 1/3! +…+ 1/n! And output (set n=20)
#include<stdio.h> int main() { long f(int n); double s=1.0; for(int i=1; i<=20; i++) { s+=1.0/f(i); } printf("%f\n", s); return 0; } long f(int n) { if(n==1) { return 1; } else { return n*f(n-1); } }
Pointer variables and one-dimensional arrays
The five elements in an integer array are input by the keyboard. After each element is doubled, it is output in turn. Please program with pointer
tips:
- Pointer + i = & array name [a number + i] (special properties of one-dimensional array)
- Pointer = & array name [number]
- *Pointer = the value of the variable specified by the pointer
- &Array name [number] is equivalent to array name + number. For ex amp le: (P = & A [0] can be changed to p=a+0)
- Array name [number] is equivalent to * (array name + number). For example: (a[i]=a[i]* 2 can be changed to * (a+i)=* (a+i)*2)
#include<stdio.h> int main() { int i,a[5],*p; p=&a[0];//p=a+0 for(i=0; i<5; i++) { scanf("%d", p+i);//&a[i] *(p+i)=*(p+i)*2;//a[i]=a[i]*2 } for(i=0; i<5; i++) { printf("%d\t", *(p+i));//a[i] } return 0; }
Pointer variable and one-dimensional array correlation function
There are two groups, five students and ten students respectively. Please program and input the scores of these students, and call an aver function to calculate the average score of the two groups (both formal parameters and arguments are required to use pointer variables)
tips:
- Function declaration: change array name [] to * p
- Function explanation: change the array name [i] to * (p+i)
- Function usage: change the array name to Q (Association is required before use)
#include<stdio.h> int main() { float avg(float *p, int n);//Function declaration a [] changed to * p float a[5],b[10],*q1,*q2; int i; //Associate before use q1=&a[0]; q2=&b[0]; for(i=0; i<5; i++) { scanf("%f", &a[i]); } printf("\n"); for(i=0; i<10; i++) { scanf("%f", &b[i]); } printf("\n"); printf("Results of the first group:%f\n", avg(q1,5));//The function is changed from a to q1 printf("Group 2:%f\n", avg(q2,10));//Change the function from b to q2 return 0; } float avg(float *p, int n)//Function definition a [] changed to * p { //Function interpretation float sum=0; for(int i=0; i<n; i++) { sum+=*(p+i);//Change a[i] to * (p+i) } return sum/n; }
Pointer variables and two-dimensional arrays
Known integer two-dimensional array a[3] [4]={1,2,3,4,5,6,6,5,4,3,2,1}. Please use pointer variable to output the value of each element of two-dimensional array
tips:
- Pointer + i = & (the ith element after array name [number a] [number b] (special properties of two-dimensional array)
- Pointer = & array name [number a] [number b]
- *Pointer = the value of the variable specified by the pointer
- &Array name [number a] [number b] is equivalent to array name [number a] + number B, and also equivalent to array name [0]+a * number of columns + B
- Array name [number a] [number b] is equivalent to * (array name [number a] + number b)
For example, & a[0] [0] can be written as a[0]+0. If 0 is omitted, it will be a[0]& A [2] [3] can be written as a[0] + 2 * 4 + 3 (the two-dimensional array has 4 columns). Calculate the second half, and the result is a[0]+11
#include<stdio.h> int main() { int a[3][4]={1,2,3,4,5,6,6,5,4,3,2,1},*p; for(p=&a[0][0]; p<=&a[2][3]; p++) { if((p-&a[0][0])%4==0) { printf("\n"); } printf("%d\t", *p); } return 0; }
Pointer variable and two-dimensional array correlation function
1: There are three students studying four courses, and the scores of student one are 65, 67, 70 and 60 respectively. The scores of students 2 are 80, 87, 90 and 81 respectively. The scores of students three are 90, 99, 93 and 98 respectively. Input the above scores into the two-dimensional array, and output the total average score of the three through the function
Method 1 (all formal parameters and arguments are array names):
#include<stdio.h> int main() { float shuchu(float a[][4], int n); float fenshu[3][4]={65,67,70,60,80,87,90,81,90,99,93,98}; shuchu(fenshu,12); return 0; } float shuchu(float a[][4], int n) { float sum=0; int i,j; for(i=0; i<3; i++) { for(j=0; j<4; j++) { sum+=a[i][j]; } } printf("The total average score of the three is:%f\n", sum/n); }
Method 2 (formal parameter is pointer variable, and actual parameter is array name):
tips:
- Function declaration: the array name [] [a number] is changed to * p
- Function explanation: change the array name [i] [j] to * (p+i * number of columns + j)
- Function usage: change the array name to * array name
#include<stdio.h> int main() { void shuchu(float *p,int n); float fenshu[3][4]={65,67,70,60,80,87,90,81,90,99,93,98}; shuchu(*fenshu,12); return 0; } void shuchu(float *p,int n) { float sum=0; int i; //When using the pointer, the original double loop is unnecessary. The original double loop means from 0 to 11. //Because it was a two-dimensional array, it had to tamper with rows and columns, so a double for loop was used. //When we convert it into a pointer, the pointer does not care about rows and columns. It is added back in line, so a circular statement is enough. for(i=0;i<=11;i++) { sum+=*(p+i); } //for(i=0;i<=2;i++) //{ //for(j=0;j<=3;j++) //{ //sum+=*(p+i*4+j); //} //} printf("The total average score of the three is%f\n",sum/n); }
Method 3: if the title requires that our formal parameter is a pointer variable and the actual parameter is also a pointer variable, we can modify it according to the following three steps (the first two steps remain unchanged)
- Function declaration: the array name [] [a number] is changed to * p
- Function explanation: change the array name [i] [j] to * (p+i * number of columns + j)
- Function usage: change the array name to Q (associate before use)
#include<stdio.h> int main() { void shuchu(float *p,int n); float fenshu[3][4]={65,67,70,60,80,87,90,81,90,99,93,98}; int *q; q=&fenshu[0][0]; shuchu(q,12);//The pointer q needs to be defined before using and is associated with the first element of the array return 0; } void shuchu(float *p,int n) { float sum=0; int i; for(i=0;i<=11;i++) { sum+=*(p+i); } printf("The total average score of the three is%f\n",sum/n); }
Use the pointer to the row variable of the two-dimensional array as the formal parameter
1: There are three students studying four courses, and the scores of student one are 65, 67, 70 and 60 respectively. The scores of students 2 are 80, 87, 90 and 81 respectively. The scores of students three are 90, 99, 93 and 98 respectively. Input the above scores into the two-dimensional array, and output the scores of each subject of the second student through the function
tips:
- Function declaration: change the array name [] [a number] to (* p) [a number]
- Function explanation: change the array name [i] [j] to * (* p+i)+j)
#include<stdio.h> int main() { void shuchu(float (*p)[4],int n);//Originally float a[][4] float fenshu[3][4]={{65,67,70,60},{80,87,90,81},{90,99,93,98}}; shuchu(fenshu,2); return 0; } void shuchu(float (*p)[4],int n)//Originally float a[][4] { int i; printf("The first%d What are the scores of students in all subjects\n",n); for(i=0;i<=3;i++) { printf("%f\t",*(*(p+n-1)+i));//Originally a[n-1][i] } printf("\n"); }
Pointer variables and strings
And pointers are used exactly the same as one-dimensional arrays
1: Define string a as "I am shuaibi!", Assign it to string b and output string b
#include<stdio.h> int main() { int i; char a[100]={'I',' ','a','m',' ','s','h','u','a','i','b','i','!'}; char b[100],*p,*q; p=a; q=b; for(i=0; *(p+i)!='\0'; i++) { *(q+i)=*(p+i); } *(q+i+1)='\0'; printf("character string b: "); for(i=0; *(q+i)!='\0'; i++) { printf("%c", *(q+i)); } return 0; }
Numerical relationship between pointer and one-dimensional and two-dimensional array
Generally speaking, we all know the following two points:
- Pointer = & value
- Value = * pointer
But sometimes, it is not necessarily written in this way, but some simple writing methods are adopted. Don't use & and [], which are easy to write. But it's hard to read the program at this time. In fact, the rules of simple writing are the following two:
- &a[i]=a+i
- a[i]=*(a+i)
Swap two numbers
Enter two integers a and b, and output a and b in the order of first large and then small. (requirements: the values of a and b remain unchanged, and the values of p1 and p2 of pointer variables are exchanged)
#include<stdio.h> int main() { int a,b,*p1,*p2,*p; scanf("%d%d", &a, &b); p1=&a; p2=&b; if(a<b) { //It can also be changed to P1 = & B; p2=&a; The program is more concise p=p1; p1=p2; p2=p; } printf("max=%d min=%d\n", *p1, *p2); return 0; }
Use function processing, and use pointer type data as function parameters
#include<stdio.h> int main() { void swap(int *p1,int *p2); int a,b,*p1,*p2; scanf("%d%d", &a, &b); p1=&a; p2=&b; if(a<b) { swap(p1,p2); } printf("max=%d min=%d\n", a, b); return 0; } void swap(int *p1,int *p2) { int t; t=*p1; *p1=*p2; *p2=t; }