试验任务1
1
1 #include <stdio.h> 2 char score_to_grade(int score); 3 int main() 4 { 5 int score; 6 char grade; 7 while(scanf("%d",&score) !=EOF){ 8 grade=score_to_grade(score); 9 printf("分数:%d,等级:%c\n\n",score,grade); 10 } 11 return 0; 12 } 13 char score_to_grade(int score){ 14 char ans; 15 switch(score/10){ 16 case 10: 17 case 9: ans='A';break; 18 case 8: ans='B'; break; 19 case 7: ans='C';break; 20 case 6: ans='D';break; 21 default: ans='E'; 22 } 23 return ans; 24 }

2
(1)score_to_grade 函数的功能是将分数转换成等级;形参是整数型;返回值是字符型。
(2)首先没有定义ans;接着case后没有使用break;最后没有返回值。
试验任务2
1
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 }

2
(1)sum_diguts的功能是将整数的各位相加。
(2)能输出相同的结果;第一种用的是迭代思想;第二种用的是递归思想。
实验任务3
1
#include <stdio.h>int power(int x, int n); // 函数声明int main() {int x, n;int ans;while(printf("Enter x and n: "), scanf("%d%d", &x, &n) != EOF) {ans = power(x, n); // 函数调用printf("n = %d, ans = %d\n\n", n, ans);}return 0; }// 函数定义 int power(int x, int n) {int t;if(n == 0)return 1;else if(n % 2)return x * power(x, n-1);else {t = power(x, n/2);return t*t;} }

2
(1)power的功能是计算x的n次幂。
(2)是的;
实验任务4
1 #include <stdio.h> 2 int is_prime(int n); 3 int main() 4 { 5 int i,sum=0; 6 printf("100以内的孪生素数:\n"); 7 for(i=2;i<=100;i++) 8 { 9 if(is_prime(i)&&is_prime(i+2)) 10 { 11 sum=sum+1; 12 printf("%d %d\n",i,i+2); 13 } 14 } 15 printf("100以内的孪生素数共有%d个",sum); 16 return 0; 17 18 } 19 int is_prime(int n){ 20 int m; 21 for(m=2;m<n;m++) 22 { 23 if(n%m==0){ 24 return 0; 25 } 26 } 27 return 1; 28 }

试验任务5
1
1 /****************************************************************************** 2 3 Welcome to GDB Online. 4 GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby, 5 C#, OCaml, VB, Perl, Swift, Prolog, Javascript, Pascal, COBOL, HTML, CSS, JS 6 Code, Compile, Run and Debug online from anywhere in world. 7 8 *******************************************************************************/ 9 #include <stdio.h> 10 int func(int n, int m); // 函数声明 11 12 int main() { 13 int n, m; 14 int ans; 15 16 while (scanf("%d%d", &n, &m) != EOF) { 17 ans = func(n, m); // 函数调用 18 printf("n = %d, m = %d, ans = %d\n\n", n, m, ans); 19 } 20 21 return 0; 22 } 23 24 // 函数定义 25 // 待补足。。。(分别用迭代和递归实现) 26 int func(int n, int m) 27 { 28 int a, shang, xia, sum; 29 xia=1; 30 shang=1; 31 if(m!=0) 32 {for (a = 1; a <= m; a++) 33 { 34 xia *= a; 35 shang*=(n-m+a); 36 } 37 38 sum = shang / xia; 39 return sum; 40 } 41 else 42 { 43 return 1; 44 } 45 46 47 }

2
1 /****************************************************************************** 2 3 Welcome to GDB Online. 4 GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby, 5 C#, OCaml, VB, Perl, Swift, Prolog, Javascript, Pascal, COBOL, HTML, CSS, JS 6 Code, Compile, Run and Debug online from anywhere in world. 7 8 *******************************************************************************/ 9 #include <stdio.h> 10 int func(int n, int m); // 函数声明 11 12 int main() { 13 int n, m; 14 int ans; 15 16 while (scanf("%d%d", &n, &m) != EOF) { 17 ans = func(n, m); // 函数调用 18 printf("n = %d, m = %d, ans = %d\n\n", n, m, ans); 19 } 20 21 return 0; 22 } 23 24 // 函数定义 25 // 待补足。。。(分别用迭代和递归实现) 26 int func(int n, int m) 27 { 28 int sum; 29 if(n<m) 30 { 31 return 0; 32 } 33 else if(m==0) 34 { 35 return 1; 36 } 37 else 38 { 39 sum=func(n-1,m)+func(n-1,m-1); 40 return sum; 41 } 42 43 44 }

试验任务6
1 /****************************************************************************** 2 3 Welcome to GDB Online. 4 GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby, 5 C#, OCaml, VB, Perl, Swift, Prolog, Javascript, Pascal, COBOL, HTML, CSS, JS 6 Code, Compile, Run and Debug online from anywhere in world. 7 8 *******************************************************************************/ 9 #include <stdio.h> 10 #include <math.h> 11 12 // 函数声明 13 // 待补足.... 14 int gcd(int a, int b, int c); 15 16 17 int main() { 18 int a, b, c; 19 int ans; 20 21 while (scanf("%d%d%d", &a, &b, &c) != EOF) { 22 ans = gcd(a, b, c); // 函数调用 23 printf("最大公约数: %d\n\n", ans); 24 } 25 26 return 0; 27 } 28 29 30 // 函数定义 31 // 待补足... 32 int gcd(int a, int b, int c) 33 { 34 int i; 35 i =fmin(fmin(a, b),c); 36 for (; i > 0; i--) 37 { 38 if (a % i == 0 && b % i == 0 && c % i == 0) 39 { 40 return i; 41 } 42 } 43 }

试验任务7
1 /****************************************************************************** 2 3 Welcome to GDB Online. 4 GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby, 5 C#, OCaml, VB, Perl, Swift, Prolog, Javascript, Pascal, COBOL, HTML, CSS, JS 6 Code, Compile, Run and Debug online from anywhere in world. 7 8 *******************************************************************************/ 9 #include <stdio.h> 10 #include <stdlib.h> 11 12 // 函数声明 13 // 待补足 14 // xxx 15 void print_charman(int n); 16 17 int main() { 18 int n; 19 20 printf("Enter n: "); 21 scanf("%d", &n); 22 print_charman(n); // 函数调用 23 24 return 0; 25 } 26 27 // 函数print_charman定义 28 // 待补足 29 // xxx 30 void print_charman(int n) 31 { 32 int i,l,m,x,y; 33 for (i = n; i > 0; i--) 34 { 35 for (y = i; y <n; y++) 36 { 37 printf("\t"); 38 } 39 for (l = 2*i-1; l > 0; l--) 40 { 41 printf(" 0\t"); 42 } 43 printf("\n"); 44 for (y = i; y <n; y++) 45 { 46 printf("\t"); 47 } 48 for (m = 2*i-1; m > 0; m--) 49 { 50 printf("<H>\t"); 51 } 52 printf("\n"); 53 for (y = i; y <n; y++) 54 { 55 printf("\t"); 56 } 57 for (x = 2*i-1; x > 0; x--) 58 { 59 printf("I I\t"); 60 } 61 printf("\n"); 62 63 } 64 }


