任务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 15 return 0; 16 } 17 18 19 char score_to_grade(int score) { 20 char ans; 21 22 switch(score/10) { 23 case 10: 24 case 9: ans = 'A'; break; 25 case 8: ans = 'B'; break; 26 case 7: ans = 'C'; break; 27 case 6: ans = 'D'; break; 28 default: ans = 'E'; 29 } 30 31 return ans; 32 }
问题1 根据分数判断等第 形参类型为int 返回值类型为char
问题2 有问题 在判断出分数所属的等第后程序不会进入下一个循环 而是接着执行相对应
任务2:
1 #include <stdio.h> 2 3 int sum_digits(int n); 4 5 int main() { 6 int n; 7 int ans; 8 9 10 while(printf("Enter n: "), scanf("%d", &n) != EOF) { 11 ans = sum_digits(n); 12 printf("n = %d, ans = %d\n\n", n, ans); 13 } 14 15 16 return 0; 17 } 18 19 20 int sum_digits(int n) { 21 if(n<10) 22 return n; 23 24 25 return sum_digits(n/10)+n%10; 26 }
问题1 根据输入的数字,算出各个位数上的数字的和
问题2 能 第一种方法是迭代
任务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 int power(int x, int n) { 17 int t; 18 19 if(n == 0) 20 return 1; 21 else if(n % 2) 22 return x * power(x, n-1); 23 else { 24 t = power(x, n/2); 25 return t*t; 26 } 27 }
问题1 求 n的x次幂、
问题2
任务4:
1 #include<stdio.h> 2 int is_prime(int n); 3 int main(){ 4 int n; 5 int ans=0; 6 printf("100以内的孪生素数:\n"); 7 for(n=2;n<=100;n++){ 8 if(is_prime(n)&&is_prime(n+2)){ 9 10 11 printf("%d %d\n",n,n+2); 12 ans+=1; 13 } 14 } 15 16 printf("100以内的孪生素数共有%d个",ans); 17 return 0; 18 } 19 int is_prime(int n) 20 { 21 int k; 22 for(k=2;k<=n/2;k++) 23 if(n%k==0) 24 return 0; 25 return 1; 26 27 }
任务5:
1 #include <stdio.h> 2 #include <stdlib.h> 3 void hanoi(unsigned int n,char from,char temp,char to); 4 void moveplate(unsigned int n,char from,char to); 5 int ans=0; 6 int main(){ 7 8 unsigned int n; 9 while(scanf("%u",&n)!=EOF){ 10 11 hanoi(n,'A','B','C'); 12 13 printf("一共移动了%d次\n",ans); 14 system("pause"); 15 16 } 17 return 0; 18 19 } 20 void hanoi(unsigned int n,char from,char temp,char to) 21 { 22 23 if(n==1){ 24 ans+=1; 25 moveplate(n,from,to); 26 27 } 28 else 29 { 30 31 hanoi(n-1,from,to,temp); 32 ans+=1; 33 moveplate(n,from,to); 34 35 hanoi(n-1,temp,from,to); 36 37 38 } 39 40 } 41 void moveplate(unsigned int n,char from,char to) 42 {int ans=0; 43 printf("%u:%c-->%c\n",n,from,to); 44 ans+=1; 45 }
任务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 { 17 18 if(m==0||n==m) 19 return 1; 20 if(m>n) 21 return 0; 22 return func(n-1,m)+func(n-1,m-1); 23 24 }
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 { 17 int i,a=1,b=1; 18 if (m>n) 19 return 0; 20 for(i=0;i<m;i++) 21 { 22 a=a*n--; 23 a=a/b++; 24 } 25 return a; 26 }
任务7:
1 #include <stdio.h> 2 #include <stdlib.h> 3 void print_charman(int n); 4 int i; 5 int main() { 6 int n,m; 7 8 printf("Enter n: "); 9 scanf("%d", &n); 10 print_charman(n); // 函数调用 11 12 return 0; 13 } 14 15 16 17 void print_charman(int n) { 18 int i,j; 19 20 for (j=0;j<n;j++){ 21 22 for(i=1;i<=j;i++) 23 printf(" \t"); 24 for (i=1;i<=2*(n-j)-1;i++) 25 printf(" O \t"); 26 printf("\n"); 27 for(i=1;i<=j;i++) 28 printf(" \t"); 29 for (i=1;i<=2*(n-j)-1;i++) 30 printf("<H>\t"); 31 printf("\n"); 32 for(i=1;i<=j;i++) 33 printf(" \t"); 34 for (i=1;i<=2*(n-j)-1;i++) 35 printf("I I\t"); 36 printf("\n"); 37 } 38 }
标签:return,int,ans,char,实验,printf,include From: https://www.cnblogs.com/qjj1004/p/18510238