1 //一个数如果恰好等于它的因子(自身除外)之和,则称该数为完全数。如6=1+2+3,则6为完全数。 2 #include <stdio.h> 3 4 int main(void) 5 { 6 int i, sum, a; 7 a = 2; 8 do 9 { 10 i = 1; 11 sum = 0; 12 do 13 { 14 if(a%i==0) 15 sum += i; 16 i++; 17 }while(i <= a/2); 18 if(sum == a) 19 printf("%4d",a); 20 a++; 21 }while(a<500); 22 putchar('\n'); 23 return 0; 24 }
#include <stdio.h> int main() { int i, j, num[4][6]; printf("Enter 24 integers:\n"); for(i=0; i < 4; i++) for(j=0; j < 6; j++) scanf("%d",&num[i][j]); for(i=0; i < 4; i++) for(j=0; j < 6; j++) if(num[i][j] < 0) goto found; printf("Not found !\n"); goto done; found: printf("minus number num[%d][%d]: %d\n", i, j, num[i][j]); done: return 0; }
//li14天数判断 #include <stdio.h> int main() { int day, month, year, sum, leap; printf("Please input year, month, day\n"); scanf("%d,%d,%d", &year, &month, &day); switch(month) { case 1: sum = 0; break; case 2: sum = 31; break; case 3: sum = 59; break; case 4: sum = 90; break; case 5: sum = 120; break; case 6: sum = 151; break; case 7: sum = 181; break; case 8: sum = 212; break; case 9: sum = 243; break; case 10: sum = 273; break; case 11: sum = 304; break; case 12: sum = 334; break; default: printf("data error !"); } sum += day; if(year%400==0||(year%4==0&&year%100!=0)) leap = 1; else leap = 0; if(leap==1&&month>2) sum ++; printf("It is the %dth day.\n", sum); return 0; }
1 //li15猜数字 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <ctype.h> 5 #include <time.h> 6 7 int main() 8 { 9 int count, number,guess; 10 char yes = 'Y'; 11 printf("Now let us play the game.\nGuess the number:\n"); 12 while(toupper(yes)=='Y') 13 { 14 count = 0; 15 srand(time(0)); 16 number = rand()/100+1; 17 do 18 { 19 do 20 { 21 printf("Input an interger number(1-100):\n"); 22 scanf("%d", &guess); 23 }while(!(guess>=1&&guess<=100)); 24 if(guess<number) 25 printf("Your answer is LOW , try again.\n"); 26 if(guess>number) 27 printf("Yuor answer is HIGH , try again.\n"); 28 count++; 29 if(count == 15) 30 { 31 printf("This is the %d times ! Think it hard next!\n", count); 32 exit(0); 33 } 34 }while(!(guess == number)); 35 if(count <= 7) 36 { 37 printf("You have got it in %d times.\n", count); 38 printf("Congretulations!\n"); 39 } 40 else 41 { 42 printf("You got it in %d times.\n",count); 43 printf("I bet you can do it better!\n"); 44 } 45 printf("NEXT?(Y/N):\n"); 46 scanf(" %c", &yes); 47 } 48 return 0; 49 }
1 //li16按位排序 2 #include <stdio.h> 3 4 int main() 5 { 6 long int x, y; 7 int i, a[5]; 8 scanf("%ld", &x); 9 if((x<10000)||(x>99999)) 10 { 11 printf("This data is error !\n"); 12 exit(0); 13 } 14 for(i = 0; i<5; ++i) 15 { 16 a[i] = x%10; 17 x = x/10; 18 } 19 fun(a); 20 y =0; 21 for(i = 0; i < 5; ++i) 22 y=y*10+a[i]; 23 printf("%ld", y); 24 printf("\n"); 25 return 0; 26 } 27 fun(int a[]) 28 { 29 int i, j, k; 30 for(i = 0; i < 5; ++i) 31 for(j = i; j < 5; ++j) 32 if(a[j] < a[i]) 33 { 34 k = a[i]; 35 a[i] = a[j]; 36 a[j] = k; 37 } 38 }
1 //li18数组逆转 2 #include <stdio.h> 3 #define N 20 4 5 int main() 6 { 7 int a[N] = {9,6,5,4,1,23,21,22,26,3,2,0,8,7,67,68,34,2,88,34}; 8 int i, temp; 9 printf("original array: \n"); 10 for(i =0; i < N; i++) 11 printf("%4d", a[i]); 12 for(i = 0; i < N/2; i++) 13 { 14 temp = a[i]; 15 a[i] = a[N-i-1]; 16 a[N-i-1] = temp; 17 } 18 printf("sorted array: \n"); 19 for(i = 0; i < N; i ++) 20 printf("%4d", a[i]); 21 printf("\n"); 22 return 0; 23 }
1 //li19折半查找 2 #include <stdio.h> 3 4 void selectsort (int a[], int n) 5 { 6 int i, j, k, num; 7 for(i = 0; i < n-1; i ++) 8 { 9 k = i; 10 for(j = i+1; j < n; j ++) 11 if(a[j]<a[k]) 12 k = j; 13 if(k!=i) 14 { 15 num = a[k]; 16 a[k] = a[i]; 17 a[i] = num; 18 } 19 } 20 printf("The sorted numbers are:"); 21 for(i = 0; i < n; i ++) 22 printf("%7d", a[i]); 23 putchar('\n'); 24 } 25 void halfind(int a[], int n) 26 { 27 int k, find = 0, first = 0, last = (n-1), half; 28 printf("Input the number to look for: "); 29 scanf("%d", &k); 30 do 31 { 32 half = (first + last)/2; 33 if(k == a[half]) 34 { 35 printf("Find %d, it is a[%d].\n", k, half); 36 find = 1; 37 } 38 else 39 if(k >a[half]) 40 first = half + 1; 41 else 42 last = half - 1; 43 }while((first<=last)&&(find==0)); 44 if(find==0) 45 printf("%d not been found!\n", k); 46 } 47 int main() 48 { 49 int i,n, a[100]; 50 printf("Input the total number[1~100]:"); 51 scanf("%d", &n); 52 printf("Input %d numbers: \n", n); 53 for(i = 0; i < n; i ++) 54 scanf("%d", &a[i]); 55 printf("The original orders are: "); 56 for(i = 0; i < n; i ++) 57 printf("%7d", a[i]); 58 printf("\n"); 59 selectsort(a, n); 60 halfind(a, n); 61 //printf("\n"); 62 return 0; 63 }
1 //li22魔方阵 2 #include <stdio.h> 3 #define N 9 4 5 int main() 6 { 7 int i, j, k, a[N][N]; 8 for(i=0; i<N; i++) 9 for(j=0; j<N; j++) 10 a[i][j] = 0; 11 j = N/2; 12 a[0][j] = 1; 13 for(k=2; k<=N*N; k++) 14 { 15 i--; 16 j++; 17 if(i<0) 18 i=N-1; 19 else 20 if(j>N-1) 21 j=0; 22 if(a[i][j]==0) 23 a[i][j]=k; 24 else 25 { 26 i=(i+2)%N; 27 j=(j-1+N)%N; 28 a[i][j]=k; 29 } 30 } 31 printf("\n"); 32 for(i=0; i<N; i++) 33 { 34 printf("\t"); 35 for(j=0; j<N; j++) 36 printf("%4d", a[i][j]); 37 printf("\n"); 38 } 39 return 0; 40 }
1 //li25成绩统计 2 #define N 8 3 #include <stdio.h> 4 5 int main() 6 { 7 int x, num[N+1]; 8 float st[N+1], ave, sum=0; 9 for(x=1; x<=N; x++) 10 scanf("%d%f",&num[x], &st[x]); 11 for(x=1; x<=N; x++) 12 sum+=st[x]; 13 ave=sum/N; 14 printf("average is: %f\n", ave); 15 for(x=1; x<=N; x++) 16 if(st[x]>=ave) 17 printf("%d\t%f\n", num[x], st[x]); 18 return 0; 19 }
1 //li26二维数组每行最大值提取 2 #include <stdio.h> 3 4 int main() 5 { 6 int x, y, p, t; 7 int a[3][4]={{9,8,7,11},{4,6,2,1},{25,3,19,10}}; 8 //ÌáȡÿÐÐ×î´óÖµ 9 for(x=0; x<=2; x++) 10 { 11 p=0; 12 for(y=1; y<=3; y++) 13 if(a[x][y]>a[x][p]) 14 p=y; 15 t=a[x][0]; 16 a[x][0]=a[x][p]; 17 a[x][p]=t; 18 } 19 for(x=0; x<=2; x++) 20 { 21 for(y=0; y<=3; y++) 22 printf("%d\t", a[x][y]); 23 printf("\n"); 24 } 25 return 0; 26 }
1 //li27字符排序 2 #include <stdio.h> 3 4 hanio(int n, char x, char y, char z) 5 { 6 if(n==1) 7 printf("%c->%c\n", x, z); 8 else 9 { 10 hanio(n-1, x, z, y); 11 printf("%c->%c\n", x, z); 12 hanio(n-1, y, x, z); 13 } 14 } 15 16 17 int main() 18 { 19 int m; 20 printf("input the number of diskes:"); 21 scanf("%d", &m); 22 printf("The step to moving %3d diskes:", m); 23 hanio(m, 'a', 'b', 'c'); 24 return 0; 25 }
1 //li29学生成绩检查 2 #include <stdio.h> 3 4 int main() 5 { 6 void search(float(*p)[4], int m); 7 static float score[5][4]={{65,80,78,90},{98,89,100,83},{92,56,78,70},{78,63,80,70},{64,55,70,81}}; 8 search(score, 5); 9 return 0; 10 } 11 void search( float (*p)[4], int m) 12 { 13 int i, j, flag; 14 for(i=0; i<m; i++) 15 { 16 flag = 0; 17 for(j=0; j<4; j++) 18 { 19 if( *( *(p+i)+j )<60 ) 20 flag=1; 21 if(flag==1) 22 { 23 printf("Np.%d is flunked, score are:\n", i+1); 24 for(j=0; j<4; j++) 25 printf("%5.1f", *(*(p+i)+j) ); 26 printf("\n"); 27 } 28 } 29 } 30 }
1 //li30奇偶函数调用 2 #include <stdio.h> 3 4 int main() 5 { 6 float peven(), podd(), dcall(); 7 float sum; 8 int n; 9 while(1) 10 { 11 scanf("%d", &n); 12 if(n>1) break; 13 } 14 if(n%2==0) 15 { 16 printf("Even=");//1/2+1/4+1/6+... 17 sum=dcall(peven, n); 18 } 19 else 20 { 21 printf("Odd="); 22 sum=dcall(podd, n);//1/1+1/3+1/5+... 23 } 24 printf("%f\n", sum); 25 return 0; 26 } 27 28 float peven(int n) 29 { 30 float s; 31 int i; 32 s=1; 33 for(i=2; i<=n; i+=2) 34 s+=1/(float)i; 35 return (s); 36 } 37 38 float podd(n) 39 int n; 40 { 41 float s; 42 int i; 43 s=0; 44 for(i=1; i<=n; i+=2) 45 s+=1/(float)i; 46 return (s); 47 } 48 49 float dcall(fp, n) 50 float (*fp)(); 51 int n; 52 { 53 float s; 54 s=(*fp)(n); 55 return (s); 56 }
li31万年历
1 //li32汉诺塔 2 #include <stdio.h> 3 4 hanio(int n, char x, char y, char z) 5 { 6 if(n==1) 7 printf("%c->%c\n", x, z); 8 else 9 { 10 hanio(n-1, x, z, y); 11 printf("%c->%c\n", x, z); 12 hanio(n-1, y, x, z); 13 } 14 } 15 16 17 int main() 18 { 19 int m; 20 printf("input the number of diskes:"); 21 scanf("%d", &m); 22 printf("The step to moving %3d diskes:", m); 23 hanio(m, 'a', 'b', 'c'); 24 return 0; 25 }
1 //li33求最小值 2 #include <stdio.h> 3 4 int main() 5 { 6 int a, b, c, m; 7 int min3(); 8 printf("input three intergers:"); 9 scanf("%d %d %d", &a, &b, &c); 10 m=min3(a,b,c); 11 printf("min is %d\n", m); 12 return 0; 13 } 14 15 int min2(int x, int y) 16 { 17 int z; 18 z=x<y?x:y; 19 return z; 20 } 21 int min3(int x, int y, int z) 22 { 23 int w; 24 w=min2(x, y); 25 w=min2(w, z); 26 return w; 27 }
1 //li34求三角函数 2 #include <stdio.h> 3 #include <math.h> 4 #include <string.h> 5 6 int main() 7 { 8 char ch[20]; 9 int i, j=1; 10 double(*f) (); 11 printf("input sin or cos or tan: "); 12 scanf("%s", ch); 13 if(!strcmp(ch, "sin")) 14 f=sin; 15 else 16 if(!strcmp(ch, "cos")) 17 f=cos; 18 else 19 if(!strcmp(ch, "tan")) 20 f=tan; 21 else j=0; 22 if(j==0) 23 printf("input error!"); 24 else 25 for(i=0; i<5; i++) 26 printf("\n%3d%8.3f", (i+1)*10, (*f)(3.1415*(i+1)/18)); 27 printf("\n"); 28 return 0; 29 }
1 //li35求阶乘倒数之和 2 #include <stdio.h> 3 4 int main() 5 { 6 int n; 7 float e, p; 8 e=p=1.0; 9 for(n=1; n<=10; n++) 10 { 11 p*=n; 12 e+=1.0/p; 13 } 14 printf("e=%10.7f\n", e); 15 return 0; 16 }
1 //li36求一元二次方程的根 2 #include <stdio.h> 3 #include <math.h> 4 5 int main() 6 { 7 float a, b, c, p, x1, x2, t1, t2; 8 scanf("%f,%f,%f", &a, &b, &c); 9 if(a==0&&b==0) 10 printf("unsolvable.\n"); 11 else 12 if(a==0&&b!=0) 13 printf("the single root is %f\n", -c/b); 14 else 15 if(a!=0) 16 { 17 p=b*b-4*a*c; 18 t1=-b/(2*a); 19 t2=sqrt(fabs(p))/(2*a); 20 if(p<0) 21 { 22 printf("complex root:\n"); 23 printf("%8.4f+%8.4fi\n", t1, t2); 24 printf("%8.4f-%8.4fi\n", t1, t2); 25 } 26 else 27 { 28 x1=t1+t2; 29 x2=t1-t2; 30 printf("realroot:\n"); 31 printf("%8.4f and %8.4f\n", x1, x2); 32 } 33 } 34 return 0; 35 }
1 //li37加法练习 2 #include <stdio.h> 3 #include <stdlib.h> 4 5 int main() 6 { 7 int a, b, c, x=0; 8 while(x<5)//和在100以内的加法,连对5次通过 9 { 10 a=rand( )%100; 11 b=rand( )%100; 12 if((a+b)>100) 13 continue; 14 printf("\n%d+%d=? ", a, b); 15 scanf("%d", &c); 16 if(c==a+b) 17 { 18 printf("The answer is right "); 19 x++; 20 } 21 else 22 { 23 printf("The answer is wrong !"); 24 x=0; 25 } 26 } 27 printf("\nProgram is over.\n"); 28 return 0; 29 }
1 //li38检查系统鼠标 2 #include <stdio.h> 3 #include <stdlib.h>//包含库函数exit() 4 #include <string.h> 5 void loadmous(void); 6 7 int main(void) 8 { 9 loadmous(); 10 return 0; 11 } 12 13 void loadmous() 14 { 15 char *p; 16 if((p=getenv("MOUSE"))!=NULL)//调用系统变量函数判断有无鼠标 17 { 18 if(!strcmp(p, "YES")) 19 printf("Mouse is OK\n"); 20 } 21 else 22 { 23 printf("\n No mouse\n"); 24 } 25 }
1 //li40转置矩阵 2 #include <stdio.h> 3 #define N 5 4 5 void transp(int a[][N], int n) 6 { 7 int i, j, k; 8 for(i=0; i<n; i++) 9 for(j=0; j<i; j++) 10 { 11 k=a[i][j]; 12 a[i][j]= a[j][i]; 13 a[j][i]=k; 14 } 15 } 16 int main() 17 { 18 int n, i, j; 19 int x[N][N]; 20 printf("input n:"); 21 scanf("%d", &n); 22 for(i=0; i<n; i++) 23 for(j=0; j<n; j++) 24 scanf("%d", &x[i][j]); 25 transp(x,n); 26 for(i=0; i<n; i++) 27 { 28 for(j=0; j<n; j++) 29 printf(" %d", x[i][j]); 30 printf("\n"); 31 } 32 return 0; 33 }
1 #include <stdio.h> 2 #include <conio.h> 3 #include <ctype.h> 4 #include <stdlib.h> 5 6 void main() 7 { 8 char letter; 9 do 10 { 11 printf("A Display directory listing \n"); 12 printf("B Display disk information\n"); 13 printf("C Change system date\n"); 14 printf("Q Quit\n"); 15 printf("Chioce:"); 16 letter=getchar(); 17 letter=toupper(letter); 18 switch(letter) 19 { 20 case'A': system("DIR"); break; 21 case'B': system("CHKDSK"); break; 22 case'C': system("DATE"); break; 23 } 24 }while(letter!='Q'); 25 }
1 //li42正整数个数统计 2 #include <stdio.h> 3 #include <conio.h> 4 #define MAX 100 5 int xx[MAX]; 6 int totnum=0; 7 int totcnt=0; 8 double totpjz=0.0; 9 int readdat(void); 10 void writedat(void); 11 void calvalue(void) 12 { 13 int i; 14 int j; 15 long val=0; 16 for(i=0; i<MAX; i++) 17 if(xx[i]) totnum++; //正整数个数统计 18 for(i=0; i<totnum; i++) 19 { 20 j=(xx[i]>>1); //所有数右移一位 21 if(j%2==0) 22 { 23 totcnt++; //经处理后其中偶数个数统计 24 val+=xx[i]; 25 } 26 } 27 totpjz=(double)val/totcnt; //求这些偶数平均值 28 } 29 30 int main(void) 31 { 32 if(readdat()) 33 { 34 printf("the file cann't open!\007\n"); 35 return 0; 36 } 37 calvalue(); 38 printf("the totnum is %d\n", totnum); 39 printf("the totcnt is %d\n", totcnt); 40 printf("the totpjz is %.2lf\n", totpjz); 41 writedat(); 42 return 0; 43 } 44 45 int readdat(void) 46 { 47 FILE *fp; 48 int i=0; 49 if((fp=fopen("in.txt", "r"))==NULL) 50 return 1; 51 while(!feof(fp)) 52 { 53 fscanf(fp, "%d", &xx[i++]); 54 } 55 fclose(fp); 56 return 0; 57 } 58 59 void writedat(void) 60 { 61 FILE *fp; 62 fp=fopen("my4.txt", "w"); 63 fprintf(fp, "%d\n%d\n%.2lf\n", totnum, totcnt, totpjz); 64 fclose(fp); 65 }
1 //li43电报处理 2 #include <stdio.h> 3 #define CHANGE 1 4 5 int main() 6 { 7 char str[100]; 8 char c; 9 int i=0; 10 printf("please input some word:"); 11 scanf("%s", &str); 12 while((c=str[i])!='\0') 13 { 14 i++; 15 #if CHANGE 16 if((c>='A'&&c<'Z')||c>='a'&&c<'z') 17 c++; 18 if((c=='Z')||(c=='z')) 19 c=c-25; 20 #endif 21 printf("%c", c); 22 } 23 printf("\n"); 24 return 0; 25 }
1 //li44字符串统计 2 #include <stdio.h> 3 #define N 100 4 5 int main() 6 { 7 int up=0, low=0, digit=0, space=0, other=0; 8 char *p, s[N]; 9 10 printf("the string:\n"); 11 p=s; 12 gets(p); 13 while(*p!='\0') 14 { 15 if((*p>='A')&&(*p<='Z')) 16 up++; 17 else if((*p>='a')&&(*p<='z')) 18 low++; 19 else if(*p==' ') 20 space++; 21 else if((*p>='0')&&(*p<='9')) 22 digit++; 23 else 24 other++; 25 p++; 26 } 27 printf("up letter:%d\nlow letter:%d\nspace letter:%d\nother letter:%d\n", up, low, space, other); 28 return 0; 29 }
1 //li45数字排序 2 #include <stdio.h> 3 #define N 20 4 5 int main() 6 { 7 int i, j; 8 float a[N], temp, *p; 9 p=a; 10 printf("Input %d numbers\n", N); 11 12 for(i=0; i<N; i++) 13 scanf("%f", p+i); 14 for(i=0; i<N-1; i++) 15 for(j=i+1; j<N; j++) 16 if(*(p+i)<*(p+j)) 17 { 18 temp=*(p+i); 19 *(p+i)=*(p+j); 20 *(p+j)=temp; 21 } 22 for(i=0; i<N; i++) 23 { 24 if(i%5==0) 25 printf("\n"); 26 printf("%8.3f", *(p+i)); 27 } 28 printf("\n"); 29 return 0; 30 }
1 //li46数字插入 2 #include <stdio.h> 3 #define N 10 4 5 int main() 6 { 7 int a[12], p, x, *t; 8 for(t=a+1; t<=a+N; t++) 9 scanf("%d", t); 10 scanf("%d", &x); 11 t=a; 12 p=1; 13 while(x>*(t+p)&&p<=N) 14 p++; 15 for(t=a+N; t>=a+p; t--) 16 *(t+1)=*t; 17 t=a+p; 18 *t=x; 19 for(t=a+1; t<=a+N+1; t++) 20 printf("%5d",*t); 21 printf("\n"); 22 return 0; 23 }
1 //li47求矩阵最值 2 #include <stdio.h> 3 #define N1 3 4 #define N2 4 5 6 int main() 7 { 8 int a[N1][N2], max, min, (*ap)[N2]; 9 int i, j, row1, row2, column1, column2; 10 ap=a; 11 row1=row2=column1=column2=0; 12 13 for(i=0; i<N1; i++) 14 for(j=0; j<N2; j++) 15 scanf("%d", *(ap+i)+j); 16 max=min=a[0][0]; 17 for(i=0; i<N1; i++) 18 for(j=0; j<N2; j++) 19 { 20 if(max<*(*(ap+i)+j)) 21 { 22 max=*(*(ap+i)+j); 23 row1=i; 24 column1=j; 25 } 26 if(min>*(*(ap+i)+j)) 27 { 28 min=*(*(ap+i)+j); 29 row2=i; 30 column2=j; 31 } 32 } 33 printf("max=%d\trow1=%4d column1=%4d\n", max, row1+1, column1+1); 34 printf("min=%d\trow2=%4d column2=%4d\n", min, row2+1, column2+1); 35 return 0; 36 }
1 //li48优秀学生统计 2 #include <stdio.h> 3 #define N 4 //科目数 4 #define M 5 //学生数 5 void search(int (*p)[N]) 6 { 7 int i, j, flag; 8 for(i=0; i<M; i++) 9 for(j=0; j<N; j++) 10 { 11 flag=0; 12 if(*(*(p+i)+j)>=90) 13 flag=1; 14 if(flag==1) 15 { 16 printf("No.%d is excellence, score are:", (i+1)); 17 for(j=0; j<N; j++) 18 printf("%5.1d", (*(*(p+i)+j))); 19 printf("\n"); 20 } 21 } 22 } 23 24 int main() 25 { 26 int i, j; 27 int score[M][N]; 28 for(i=0; i<M; i++) 29 { 30 printf("Input %d score of No.%d:\n", N, i+1); 31 for(j=0; j<N; j++) 32 scanf("%d", &score[i][j]); 33 } 34 search(score); 35 return 0; 36 }
1 //li49字符串排序 2 #include <stdio.h> 3 #include <string.h> 4 #define LENGTH 30 5 #define N 10 6 void sort(char **p) 7 { 8 int i, j; 9 char *pstr; 10 11 for(i=0; i<N; i++) 12 for(j=i+1; j<N; j++) 13 if(strcmp(*(p+i),*(p+j))>0) 14 { 15 pstr=*(p+j); 16 *(p+j)=*(p+i); 17 *(p+i)=pstr; 18 } 19 } 20 21 int main() 22 { 23 int i; 24 char *pstr[N]; 25 char s[N][LENGTH]; 26 for(i=0; i<N; i++) 27 pstr[i]=s[i]; 28 printf("Input %d strings:\n", N); 29 for(i=0; i<N; i++) 30 gets(pstr[i]); 31 sort(pstr); 32 printf("The sorted string are:\n"); 33 for(i=0; i<N; i++) 34 puts(pstr[i]); 35 return 0; 36 }
1 #include <stdio.h> 2 #define N 10000 3 4 int main() 5 { 6 float y; 7 float fbds(float); 8 float jifen(float, float, float(*fun)(float)); 9 y=jifen(0.0, 2.0, fbds); 10 printf("y= %6.2f\n", y); 11 return 0; 12 } 13 14 float fbds(float x) 15 { 16 return (1.0+x+x*x*x); 17 } 18 float jifen(float a, float b, float(*fun)(float)) 19 { 20 int i; 21 float h, s; 22 h=(b-a)/N; 23 s=((*fun)(a)+(*fun)(b))/2.0; 24 for(i=0; i<N; i++) 25 s+=(*fun)(a+i*h); 26 return (s*h); 27 }
1 //li53师生信息储存 2 #include <stdio.h> 3 #define N 5 4 #define M 20 5 6 int main() 7 { 8 struct 9 { 10 char name[M]; 11 int age; 12 char sex; 13 char job; 14 union 15 { 16 int class; 17 char office[M]; 18 }depart; 19 }info[N]; 20 int i; 21 for(i=0; i<N; i++) 22 { 23 printf("Input name:\n"); 24 gets(info[i].name); 25 printf("Input age:\n"); 26 scanf("%d", &info[i].age); 27 getchar(); 28 printf("Input sex(m/w):"); 29 info[i].sex=getchar(); 30 getchar(); 31 printf("Input jobs(s/t):"); 32 info[i].job=getchar(); 33 getchar(); 34 if(info[i].job=='s') 35 { 36 printf("Input class:\n"); 37 scanf("%d", &info[i].depart.class); 38 getchar(); 39 } 40 else if(info[i].job=='t') 41 { 42 printf("Input office:"); 43 gets(info[i].depart.office); 44 } 45 else 46 { 47 printf("Input wrong job, return!"); 48 i--; 49 } 50 } 51 printf("\n name \t\t age \t sex \t job \tdepart "); 52 for(i=0; i<N; i++) 53 { 54 if(info[i].job=='s') 55 printf("\n %-15s %-3d \t %c \t %-6c %-20d", info[i].name, info[i].age, 56 info[i].sex, info[i].job, info[i].depart.class); 57 else 58 printf("\n %-15s %-3d \t %c \t %-6c %-20s", info[i].name, info[i].age, 59 info[i].sex, info[i].job, info[i].depart.office); 60 } 61 printf("\n"); 62 return 0; 63 }
1 //li54创建链表 2 #include <stdio.h> 3 #include <string.h> 4 #include <stdlib.h> 5 #define LEN sizeof(struct grade) 6 struct grade 7 { 8 char no[7]; 9 int score; 10 struct grade *next; 11 }; 12 struct grade *create(void) 13 { 14 struct grade *head=NULL, *new, *tail; 15 int count=0; 16 for(;;) 17 { 18 new=(struct grade *)malloc(LEN); 19 printf("Input the number of student No.%d(6 bytes): ", count+1); 20 scanf("%6s", new->no); 21 if(strcmp(new->no, "000000")==0) 22 { 23 free(new); 24 break; 25 } 26 printf("Input the score of the student No.%d: ", count+1); 27 scanf("%d", &new->score); 28 count++; 29 new->next=NULL; 30 if(count==1) 31 head=new; 32 else 33 tail->next=new; 34 tail=new; 35 } 36 return (head); 37 } 38 39 int main() 40 { 41 struct grade *head; 42 head=create(); 43 return 0; 44 }
1 //li55整数统计 2 #include <stdio.h> 3 #include <stdlib.h> 4 #define LENGTH sizeof(struct number) 5 struct number 6 { 7 long key; 8 int count; 9 struct number *next; 10 }; 11 struct number *add(long k, struct number *head) 12 { 13 int flag=1; 14 struct number *p=head; 15 while(p!=NULL&&flag==1) 16 { 17 if(p->key==k) 18 flag=0; 19 else 20 p=p->next; 21 } 22 if(flag==0) 23 p->count++; 24 else 25 { 26 p=head; 27 head=(struct number *)malloc(LENGTH); 28 head->key=k; 29 head->count=1; 30 head->next=p; 31 } 32 return(head); 33 } 34 void list(struct number *p) 35 { 36 printf("The result are:\n"); 37 while(p!=NULL) 38 { 39 printf("%16ld %6d\n", p->key, p->count); 40 p=p->next; 41 } 42 } 43 44 int main() 45 { 46 struct number *head=NULL; 47 long k; 48 printf("Input natural number,-1 to end:\n"); 49 scanf("%ld", &k); 50 while(k>=0) 51 { 52 head=add(k, head); 53 scanf("%ld", &k); 54 } 55 list(head); 56 return 0; 57 }
1 //报数游戏 2 #define N 18 3 #define M 3 4 #include <stdio.h> 5 6 int main() 7 { 8 int a[N], k=0, i, j; 9 printf("The original orders are: \n"); 10 for(i=0; i<N; i++) 11 { 12 a[i] = i+1; 13 printf("%3d", a[i]); 14 } 15 printf("\n"); 16 for(i=0; k<N*M; i++) 17 { 18 if(a[i%N]!=-1) 19 { 20 k++; 21 if(k%3==0) 22 { 23 a[i%N]=-1; 24 printf("\n%2d is out!", (i%N+1)); 25 if(k!=N*3) 26 printf("The new order is:"); 27 else 28 printf("The game is over!"); 29 for(j=0; j<N; j++) 30 { 31 if(a[j]!=-1) 32 printf("%3d", a[j]); 33 } 34 } 35 } 36 } 37 return 0; 38 }
1 //猜座位 2 //猜座位 3 # include <stdio.h> 4 5 int main(void) 6 { 7 int i,j,k,m,p; 8 char xwei[5],side[10]; 9 char ch[10]={'B','A','C','E','A','B','D','C','E','D'}; 10 xwei[0]='A'; 11 for(i=1;i<=5;i++) 12 { 13 xwei[i]='B'; 14 for(j=1;j<5;j++) 15 if(j!=i) 16 { 17 xwei[j] = 'C'; 18 for(k=1;k<5;k++) 19 if(k!=i&&k!=j) 20 { 21 xwei[k]='D'; 22 xwei[10-i-j-k]='E'; 23 for(p=0;p<5;p++) 24 { 25 side[2*p]=xwei[(p+1)%5]; 26 side[2*p+1]=xwei[(p+4)%5]; 27 } 28 for(p=0,m=0;p<10;p+=2) 29 { 30 if((side[p]==ch[p])+(side[p+1]==ch[p+1])==1) 31 m++; 32 } 33 if(m==5) 34 { 35 printf("The order is:"); 36 for(p=0;p<5;p++) 37 printf("%2d %c",(p+1),xwei[p]); 38 } 39 } 40 41 } 42 } 43 printf("\n"); 44 return 0; 45 }
1 //打印菱形图案 2 #include <stdio.h> 3 4 int main(void) 5 { 6 int i,j; 7 for(i=0;i<4;i++) 8 { 9 for(j=0;j<4-i;j++) 10 printf(" "); 11 for(j=0;j<i;j++) 12 printf("*"); 13 printf("\n"); 14 } 15 for(i=0;i<4;++i) 16 { 17 for(j=0;j<i;j++) 18 printf(" "); 19 for(j=0;j<4-i;j++) 20 printf("*"); 21 printf("\n"); 22 } 23 return 0; 24 }
1 //打印孪生素数 2 #include <stdio.h> 3 4 int main(void) 5 { 6 int k,n,isprime(int); 7 n=0; 8 k=2; 9 while(n<15) 10 { 11 if(isprime(k)&&isprime(k+2)) 12 { 13 n=n+1; 14 printf("%d,%d\n",k,k+2); 15 } 16 k=k+1; 17 } 18 return 0; 19 } 20 int isprime (int a) 21 { 22 int k,j; 23 j=1; 24 k=2; 25 while((k<=a/2)&&j) 26 { 27 if(a%k==0) 28 j=0; 29 else 30 k=k+1; 31 } 32 return(j); 33 }
1 //个人所得税计算 2 #include <stdio.h> 3 4 int main(void) 5 { 6 float a,b; 7 int i; 8 printf("input one number:"); 9 scanf("%f",&a); 10 if(a>=4000) 11 i=4; 12 else 13 i=a/1000; 14 switch(i) 15 { 16 case 0:b=a*0.0;break; 17 case 1:b=a*0.05;break; 18 case 2:b=a*0.10;break; 19 case 3:b=a*0.15;break; 20 case 4:b=a*0.20;break; 21 } 22 printf("%f\n",b); 23 return 0; 24 }
1 //奖金发放问题 2 #include <stdio.h> 3 4 int main(void) 5 { 6 long int i; 7 int bonus1,bonus2,bonus4,bonus6,bonus10,bonus; 8 printf("Please input company's income: "); 9 scanf("%ld",&i); 10 bonus1=100000*0.1; 11 bonus2=bonus1+100000*0.075; 12 bonus4=bonus2+200000*0.05; 13 bonus6=bonus4+200000*0.03; 14 bonus10=bonus6+400000*0.015; 15 if(i<=100000) 16 bonus=i*0.1; 17 else 18 if(i<=200000) 19 bonus=bonus1+(i-100000)*0.075; 20 else 21 if(i<=400000) 22 bonus=bonus2+(i-200000)*0.05; 23 else 24 if(i<=600000) 25 bonus=bonus4+(i-400000)*0.03; 26 else 27 if(i<=1000000) 28 bonus=bonus6+(i-600000)*0.015; 29 else 30 bonus=bonus10+(i-1000000)*0.01; 31 printf("bonus=%d\n",bonus); 32 return 0; 33 }
1 //欧几里得(Euclid)算法 2 #include <stdio.h> 3 4 int main(void) 5 { 6 int a,b,x,y,gcd,r,t; 7 scanf("%d,%d",&a,&b); 8 if(a<b) 9 { 10 t=a; 11 a=b; 12 b=t; 13 } 14 x=a; 15 y=b; 16 if(y==0) gcd=x; 17 else 18 { 19 do 20 { 21 r=x%y; 22 x=y; 23 y=r; 24 } 25 while(y!=0); 26 gcd=x; 27 } 28 printf("%d,%d的最大公约数为%d\n",a,b,gcd); 29 return 0; 30 }
1 //求素数 2 #include <stdio.h> 3 #include <math.h> 4 #define N 101 5 6 int main(void) 7 { 8 int i,j,line,a[N]; 9 for(i=2;i<N;i++) 10 a[i]=i; 11 for(i=2;i<sqrt(N);i++) 12 for(j=i+1;j<N;j++) 13 { 14 if(a[i]!=0&&a[j]!=0) 15 if(a[j]%a[i]==0) 16 a[j]=0; 17 } 18 printf("\n"); 19 for(i=2,line=0;i<N;i++) 20 { 21 if(a[i]!=0) 22 { 23 printf("%5d",a[i]); 24 line++; 25 } 26 if(line==10) 27 {printf("\n"); 28 line=0; 29 } 30 } 31 printf("\n"); 32 return 0; 33 }
1 //数据加密 2 #include <stdio.h> 3 4 int main(void) 5 { 6 int a,i,t,aa[4]; 7 scanf("%d",&a); 8 aa[0]=a%10; 9 aa[1]=a%100/10; 10 aa[2]=a%1000/100; 11 aa[3]=a/1000; 12 for(i=0;i<=3;i++) 13 { 14 aa[i]+=5; 15 aa[i]%=10; 16 } 17 for(i=0;i<=3/2;i++) 18 { 19 t=aa[i]; 20 aa[i]=aa[3-i]; 21 aa[3-i]=t; 22 } 23 for(i=3;i>=0;i--) 24 printf("%d",aa[i]); 25 printf("\n"); 26 return 0; 27 }
1 //数据解密 2 #include <stdio.h> 3 4 int main(void) 5 { 6 int a,i,t,aa[4]; 7 scanf("%d",&a); 8 aa[0]=a%10; 9 aa[1]=a%100/10; 10 aa[2]=a%1000/100; 11 aa[3]=a/1000; 12 for(i=0;i<=3/2;i++) 13 { 14 t=aa[i]; 15 aa[i]=aa[3-i]; 16 aa[3-i]=t; 17 } 18 for(i=0;i<=3;i++) 19 { 20 aa[i]+=10; 21 aa[i]-=5; 22 } 23 for(i=3;i>=0;i--) 24 printf("%d",aa[i]); 25 printf("\n"); 26 return 0; 27 }
1 //数据转换 2 #include <stdio.h> 3 4 int main(void) 5 { 6 int a = 0, b = 8, k = 1; 7 long xa = 0, xb = 0, x1 = 0, x2 = 0; 8 printf("Please input the number: \n"); 9 scanf("%ld", &xa); 10 x1 = xa; 11 while(x1 != 0) 12 { 13 x2 += (x1 % 10) * k; 14 x1 /= 10; 15 k *= a; 16 } 17 k = 1; 18 x1 = x2; 19 while(x1 != 0) 20 { 21 xb += (x1 % b) * k; 22 x1 /= b; 23 k *= 10; 24 } 25 printf("%ld(%d)=%ld(%d)=%ld(%d)\n", xa,a,x2,10,xb,b); 26 return 0; 27 }
1 //水仙花数是指一个三位数,其各位数字的立方和等于该数本身。 2 #include <stdio.h> 3 4 int main(void) 5 { 6 int i,j,k,n; 7 printf("water flower'number is:"); 8 for(n=100;n<1000;n++) 9 { 10 i=n/100; 11 j=n/10%10; 12 k=n%10; 13 if(i*100+j*10+k==i*i*i+j*j*j+k*k*k) 14 { 15 printf("%-5d",n); 16 } 17 //printf("\n"); 18 } 19 printf("\n"); 20 return 0; 21 }
1 //随机数 2 #include <stdio.h> 3 #define N 10 4 5 static unsigned long k = -1; 6 void startnum(unsigned int seed) 7 { 8 k = seed; 9 } 10 unsigned randnum(long n) 11 { 12 k = ((k * 159 + 23) % n) + 1; 13 return k; 14 } 15 16 int main(void) 17 { 18 register unsigned int i; 19 long n = 0; 20 while(!(n>0&&(k>0&&k<65536))) 21 { 22 printf("Please input two number between 1 and 65535\n"); 23 scanf("%ld%ld", &k, &n); 24 if(n<=0) 25 printf("wrong seed!\n"); 26 if(k<=0||k>=65536) 27 printf("wrong max random!\n"); 28 } 29 startnum(n); 30 for(i=0;i<N;i++) 31 printf("%6u\n",randnum(n)); 32 return 0; 33 }
1 //杨辉三角 2 #include <stdio.h> 3 #define N 15 4 5 int main() 6 { 7 int i, j, n, a[N][N]; 8 do 9 { 10 printf("Input n[1-12]:"); 11 scanf("%d", &n); 12 }while(n<1||n>12); 13 for(i=0; i<=n; i++) 14 { 15 a[i][0] = 1; 16 a[i][i] = 1; 17 } 18 for(i=2; i<=n; i++) 19 for(j=1; j<i; j++) 20 a[i][j]=a[i-1][j-1]+a[i-1][j]; 21 printf("\n"); 22 for(i=0; i<=n; i++) 23 { 24 for(j=0; j<(70-6*i)/2; j++) 25 printf(" "); 26 for(j=0; j<=i; j++) 27 printf("%6d", a[i][j]); 28 printf("\n"); 29 } 30 printf("\n"); 31 return 0; 32 }
1 //最大公约数 2 #include <stdio.h> 3 4 int main(void) 5 { 6 int x,y,z; 7 int gcd(int,int); 8 printf("input two integers:\n"); 9 scanf("%d,%d",&x,&y); 10 z=gcd(x,y); 11 printf("greatest common divisor is %d\n",z); 12 return 0; 13 } 14 int gcd(int a,int b) 15 { 16 int t,c; 17 if(a<b) 18 { 19 t=a; 20 a=b; 21 b=t; 22 } 23 c=a%b; 24 while(c!=0) 25 { 26 a=b; 27 b=c; 28 c=a%b; 29 } 30 return b; 31 }
1 //li31万年历 2 #include <stdio.h> 3 4 int IsLeapYear(int); 5 int main() 6 { 7 int i, day, year, temp,temp_i; 8 long int Year_days=0; 9 int Year_Start=1; 10 int Per_Year_Days; 11 int month_day[]={31,28,31,30,31,30,31,31,30,31,30,31,29}; 12 printf("Please enter the year: "); 13 scanf("%d", &year); 14 while(Year_Start < year) 15 { 16 if(IsLeapYear(Year_Start)) 17 Per_Year_Days=366; 18 else 19 Per_Year_Days=365; 20 //输入年份的起始天数,用于确定第一天是星期几 21 Year_days=Year_days+Per_Year_Days; 22 Year_Start++; 23 } 24 for(temp=1; temp<=12; temp++) 25 { 26 switch( temp ) 27 { 28 case 1: printf(" February(%d)\n", year); break; 29 case 2: printf(" February(%d)\n", year); break; 30 case 3: printf(" March(%d)\n", year); break; 31 case 4: printf(" April(%d)\n", year); break; 32 case 5: printf(" May(%d)\n", year); break; 33 case 6: printf(" June(%d)\n", year); break; 34 case 7: printf(" July(%d)\n", year); break; 35 case 8: printf(" August(%d)\n", year); break; 36 case 9: printf(" September(%d)\n", year); break; 37 case 10: printf(" October(%d)\n", year); break; 38 case 11: printf(" November(%d)\n", year); break; 39 case 12: printf(" December(%d)\n", year); break; 40 } 41 i=Year_days%7; 42 printf("Mon Tue Wed Thu Fri Sat Sun\n"); 43 if(i!=0) 44 for(temp_i=0; temp_i<i; temp_i++) 45 printf(" "); 46 day=1; 47 if(IsLeapYear(year)&&temp==2) 48 while(day<=month_day[12]) 49 { 50 if( day > 1 ) 51 if(Year_days%7==0) 52 printf("\n"); 53 if(day >= 10) 54 printf("%d ", day); 55 else 56 printf("%d ", day); 57 Year_days++; 58 day++; 59 } 60 else 61 while(day<=month_day[temp-1]) 62 { 63 if(day>1) 64 if(Year_days%7==0) 65 printf("\n"); 66 if(day>=10) 67 printf("%d ", day); 68 else 69 printf("%d ", day); 70 Year_days++; 71 day++; 72 } 73 printf("\n"); 74 if(getch()=='q') 75 exit(0); 76 } 77 return 0; 78 } 79 80 int IsLeapYear(int year) 81 { 82 if(((year%4==0)&&(year%100!=0))||(year%400==0)) 83 return 1; 84 else 85 return 0; 86 }