任务1
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 }
函数score_to_grade的功能是根据输入的分数返回对应的等级。形参是整形,返回值是字符型。
有问题,没有break,满足条件的代码和后续代码都会执行。
任务2
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 21 while(n != 0) { 22 ans += n % 10; 23 n /= 10; 24 } 25 26 return ans; 27 }
函数sum_digits的功能是计算输入值的各个位数之和。
能实现等同的效果。第一种算法是迭代,第二种算法是递归。
任务3
1 #include <stdio.h> 2 3 int power(int x, int n); 4 5 int main() { 6 int x, n; 7 int ans; 8 9 while(printf("Enter x and n: "), scanf("%d%d", &x, &n) != EOF) { 10 ans = power(x, n); 11 printf("n = %d, ans = %d\n\n", n, ans); 12 } 13 14 return 0; 15 } 16 17 18 int power(int x, int n) { 19 int t; 20 21 if(n == 0) 22 return 1; 23 else if(n % 2) 24 return x * power(x, n-1); 25 else { 26 t = power(x, n/2); 27 return t*t; 28 } 29 }
函数power的功能是计算x的n次方。
是递归函数,
任务4
1 #include<stdio.h> 2 #include<math.h> 3 int is_prime(int n); 4 int main(){ 5 int i,count=0; 6 printf("100以内的孪生素数:\n"); 7 for(i=2;i<=100;++i){ 8 if(is_prime(i)&&is_prime(i+2)){ 9 printf("%d %d\n",i,i+2); 10 count++; 11 } 12 } 13 printf("100以内的孪生素数共有%d个\n",count); 14 return 0; 15 16 } 17 int is_prime(int n){ 18 int j; 19 for(j=2;j<=sqrt(n);++j){ 20 if(n%j==0) 21 return 0; 22 } 23 return 1; 24 }
任务5
1 #include<stdio.h> 2 int cnt=0; 3 void hanoi(unsigned int n,char from,char temp,char to); 4 void moveplate(unsigned int n,char from,char to); 5 6 int main() 7 { 8 unsigned int n; 9 while(scanf("%d",&n) !=EOF){ 10 11 cnt=0; 12 hanoi(n,'A','B','C'); 13 printf("\n"); 14 printf("一共移动了%d次\n",cnt); 15 } 16 17 return 0; 18 } 19 20 void hanoi(unsigned int n,char from,char temp,char to) 21 { 22 if(n==1) 23 moveplate(n,from ,to); 24 else 25 { 26 hanoi(n-1,from,to,temp); 27 moveplate(n,from,to); 28 hanoi(n-1,temp,from,to); 29 } 30 } 31 32 void moveplate(unsigned int n,char from,char to) 33 { 34 printf("%u:%c-->%c\n",n,from,to); 35 cnt++; 36 }
任务6
1 #include <stdio.h> 2 int func(int n, int m); 3 4 int main() { 5 int n, m; 6 int ans; 7 8 while(scanf("%d%d", &n, &m) != EOF) { 9 ans = func(n, m); 10 printf("n = %d, m = %d, ans = %d\n\n", n, m, ans); 11 } 12 13 return 0; 14 } 15 int func(int n,int m){ 16 int a,b=1,c,d=1,ans; 17 for(a=n;a>=n-m+1;--a) 18 b=b*a; 19 for(c=m;c>=1;--c) 20 d=d*c; 21 ans=b/d; 22 return ans; 23 }
1 #include <stdio.h> 2 int func(int n, int m); 3 4 int main() { 5 int n, m; 6 int ans; 7 8 while(scanf("%d%d", &n, &m) != EOF) { 9 ans = func(n, m); 10 printf("n = %d, m = %d, ans = %d\n\n", n, m, ans); 11 } 12 13 return 0; 14 } 15 int func(int n,int m){ 16 if(m==n||m==0) 17 return 1; 18 if(m>n) 19 return 0; 20 if(n!=0&&m!=0) 21 return func(n-1,m)+func(n-1,m-1); 22 }
任务7
1 int main() { 2 int n; 3 4 printf("Enter n: "); 5 scanf("%d", &n); 6 print_charman(n); 7 8 return 0; 9 } 10 11 void print_charman(int n){ 12 13 int i; 14 int j; 15 for(i = n;i >= 1;i --) 16 { 17 int a; 18 for(a = 1;a <= n-i;a ++) 19 printf("\t"); 20 21 for(j = 1; j <= 2*i-1;j ++) 22 printf(" O \t"); 23 printf("\n"); 24 for(a = 1;a <= n-i;a ++) 25 printf("\t"); 26 27 for(j = 1; j <= 2*i-1;j ++) 28 printf("<H>\t"); 29 printf("\n"); 30 for(a = 1;a <= n-i;a ++) 31 printf("\t"); 32 33 for(j = 1; j <= 2*i-1;j ++) 34 printf("I I\t"); 35 printf("\n"); 36 } 37 }
标签:return,int,ans,char,score,实验,printf From: https://www.cnblogs.com/0420ppp/p/18499399