实验一:
1 #include <stdio.h> 2 3 char score_to_grade (int score); 4 5 int main() { 6 int score; 7 char grade; 8 9 while(scanf("%d", &score) != EOF) { 10 grade = score_to_grade(score); 11 printf("分数: %d, 等级: %c\n\n", score, grade); 12 } 13 14 return 0; 15 } 16 17 18 char score_to_grade(int score) { 19 char ans; 20 21 switch(score/10) { 22 case 10: 23 case 9: ans = 'A'; break; 24 case 8: ans = 'B'; break; 25 case 7: ans = 'C'; break; 26 case 6: ans = 'D'; break; 27 default: ans = 'E'; 28 } 29 30 return ans; 31 }
实验二:
1 #include<stdio.h> 2 3 int sum_digits(int n); 4 5 int main() { 6 int n; 7 int ans; 8 9 while(printf("Enter n: "), scanf("%d", &n) != EOF) { 10 ans = sum_digits(n); 11 printf("n = %d, ans = %d\n\n", n, ans); 12 } 13 14 return 0; 15 } 16 17 18 int sum_digits(int n) { 19 int ans = 0; 20 while(n !=0) { 21 ans += n % 10; 22 n /= 10; 23 } 24 25 return ans; 26 }
实验3
power是计算x的n次放
power是递归函数
实验4:
1 #include<stdio.h> 2 #include<math.h> 3 #include<time.h> 4 #include<stdlib.h> 5 int is_prime(int n); 6 int main() 7 { 8 int i,sum=0; 9 printf("100以内的孪生素数:\n"); 10 for(i=2;i<=98;++i) 11 { 12 if(is_prime(i)&&is_prime(i+2)) 13 { 14 printf("%d %d\n",i,i+2); 15 sum+=1; 16 } 17 else 18 { 19 continue; 20 } 21 22 23 } 24 printf("100以内的孪生素数共有%d组\n",sum); 25 26 return 0; 27 } 28 29 30 int is_prime(int n) { 31 if (n <= 1) { 32 return 0; 33 } 34 for (int i=2; i<=sqrt(1.0*n); i++) { 35 if (n % i == 0) { 36 return 0; 37 } 38 } 39 return 1; 40 }
实验五:
1 #include<stdio.h> 2 #include<math.h> 3 #include<time.h> 4 #include<stdlib.h> 5 unsigned int move_count = 0; 6 void hanoi(unsigned int n, char from, char temp, char to); 7 void moveplate(unsigned int n, char from, char to); 8 int main() 9 { 10 unsigned int n; 11 while(scanf("%u",&n)!=EOF) 12 { 13 move_count = 0; 14 hanoi(n, 'A','B','C'); 15 printf("一共移动了: %u次\n", move_count); 16 getchar(); 17 } 18 19 return 0; 20 } 21 22 23 void hanoi(unsigned int n, char from, char temp, char to) 24 { 25 26 if(n==1) 27 moveplate(n, from, to); 28 else 29 { 30 hanoi(n-1,from, to, temp); 31 moveplate(n, from, to); 32 hanoi(n-1,temp,from, to); 33 } 34 } 35 36 37 void moveplate(unsigned int n, char from, char to) 38 { 39 printf(" %u:%c-->%c\n", n, from, to); 40 41 move_count++; 42 }
实验六:
1 1 #include<stdio.h> 2 2 #include<math.h> 3 3 #include<time.h> 4 4 #include<stdlib.h> 5 5 int func(int n,int m); 6 6 7 7 int main() 8 8 { 9 9 int n,m; 10 10 int ans; 11 11 while(scanf("%d%d",&n,&m)!=EOF) 12 12 { 13 13 ans=func(n,m); 14 14 printf("n=%d,m=%d,ans=%d\n\n",n,m,ans); 15 15 16 16 } 17 17 18 18 return 0; 19 19 } 20 20 21 21 22 22 int func(int n,int m) 23 23 { 24 24 25 if(m==0||m==n) 25 26 return 1; 26 27 else if(n<m) 27 28 return 0; 28 29 else 29 30 return func( n-1, m)+func( n-1,m-1); 30 31 31 32 32 33 33 34 }
实验7:
1 #include<stdio.h> 2 #include<math.h> 3 #include<time.h> 4 #include<stdlib.h> 5 void print_charman(int n); 6 int k; 7 int main() 8 { 9 int n; 10 printf("Enter n:"); 11 scanf("%d",&n); 12 print_charman(n); 13 14 15 16 return 0; 17 } 18 19 20 void print_charman(int n) 21 { 22 int i,j; 23 if (n <= 0) 24 return ; 25 for(i = 0;i<k;i++) 26 printf("\t"); 27 for (j = 0;j<2*n-1;j++) 28 printf(" O "); 29 30 31 printf("\n"); 32 33 for(i = 0;i<k;i++) 34 printf("\t"); 35 for(i=1;i<=2*n-1;++i) 36 { 37 printf("<H>"); 38 printf("\t"); 39 40 } 41 printf("\n"); 42 43 for(i = 0;i<k;i++) 44 printf("\t"); 45 for(i=1;i<=2*n-1;++i) 46 { 47 printf("I I"); 48 printf("\t"); 49 50 51 } 52 ++k; 53 printf("\n"); 54 print_charman(n-1); 55 56 }
标签:10,int,printf,char,实验,ans,include From: https://www.cnblogs.com/cxc666/p/18513768