实验3
任务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 } 24 return ans; 25 }
功能是将成绩转化为ABCDE,形参是整型,返回值是字符型
不能修改,原因是没有break,程序会一直运行到default
任务2
1 #include <stdio.h> 2 int sum_digits(int n); 3 int main() { 4 int n; 5 int ans; 6 while(printf("Enter n: "), scanf("%d", &n) != EOF) { 7 ans = sum_digits(n); 8 printf("n = %d, ans = %d\n\n", n, ans); 9 } 10 return 0; 11 } 12 int sum_digits(int n) { 13 int ans = 0; 14 while(n != 0) { 15 ans += n % 10; 16 n /= 10; 17 } 18 return ans; 19 }
问题1:函数的功能是计算一个数字每位数字之和
问题2:可以实现同等效果,一个是迭代,一个是递归
任务3
1 #include <stdio.h> 2 int power(int x, int n); 3 int main() { 4 int x, n; 5 int ans; 6 while(printf("Enter x and n: "), scanf("%d%d", &x, &n) != EOF) { 7 ans = power(x, n); 8 printf("n = %d, ans = %d\n\n", n, ans); 9 } 10 return 0; 11 } 12 int power(int x, int n) { 13 int t; 14 if(n == 0) 15 return 1; 16 else if(n % 2) 17 return x * power(x, n-1); 18 else { 19 t = power(x, n/2); 20 return t*t; 21 } 22 }
问题1:函数功能是实现次方的运算
问题2:是递归函数 数学模型是:当n为奇数x^n=x*x^(n-1) 当n为偶数时,x^n=[x^(n/2)]^2
任务4
1 #include<stdio.h> 2 int is_prime(int x) 3 { 4 for(int i=2;i*i<=x;++i) 5 { 6 if(x%i==0) 7 return 0; 8 } 9 return 1; 10 } 11 int main() 12 { 13 int a,b,count=0; 14 printf("100以内的孪生素数:\n"); 15 for(int n=2;n<=100;n++) 16 { 17 a=is_prime(n); 18 b=is_prime(n+2); 19 if(a&&b) 20 { 21 printf("%d %d\n",n,n+2); 22 count++; 23 } 24 } 25 printf("100以内的孪生素数共有%d个\n",count); 26 return 0; 27 }
任务5
1 #include <stdio.h> 2 #include <stdlib.h> 3 int count=0; 4 void hanoi(unsigned int n, char from, char temp, char to); 5 void moveplate(unsigned int n, char from, char to); 6 int main() { 7 unsigned int n; 8 while(scanf("%u", &n) !=EOF) 9 { 10 count=0; 11 hanoi(n, 'A', 'B', 'C'); 12 printf("\n一共移动了%d次\n",count); 13 } 14 system("pause"); 15 return 0; 16 } 17 void hanoi(unsigned int n, char from, char temp, char to) { 18 if (n == 1) 19 {; 20 moveplate(n, from, to); 21 } 22 else 23 { 24 hanoi(n - 1, from, to, temp); 25 moveplate(n, from, to); 26 hanoi(n - 1, temp, from, to); 27 } 28 } 29 void moveplate(unsigned int n, char from, char to) { 30 count++; 31 printf("%u: %c-->%c\n", n, from, to); 32 }
任务6
迭代:
1 #include <stdio.h> 2 int func(int n, int m) 3 { 4 int a=1,b=1,result; 5 int i=0,j=0; 6 if(m==0) 7 result=1; 8 for(i=1;i<=m;i++) 9 { 10 a=a*n; 11 n=n-1; 12 } 13 for(j=1;j<=m;j++) 14 { 15 b=b*j; 16 } 17 result=a/b; 18 return result; 19 } 20 int main() { 21 int n, m; 22 int ans; 23 while(scanf("%d%d", &n, &m) != EOF) { 24 ans = func(n, m); 25 printf("n = %d, m = %d, ans = %d\n\n", n, m, ans); 26 } 27 return 0; 28 }
递归:
1 #include <stdio.h> 2 int func(int n, int m) 3 { 4 if(m==0||m>=n) 5 return 1; 6 else 7 return func(n-1,m-1)+func(n-1,m); 8 } 9 int main() { 10 int n, m; 11 int ans; 12 while(scanf("%d%d", &n, &m) != EOF) { 13 ans = func(n, m); 14 printf("n = %d, m = %d, ans = %d\n\n", n, m, ans); 15 } 16 return 0; 17 }
任务7
1 #include<stdio.h> 2 #include<stdlib.h> 3 void print_charman(int n) 4 { 5 int i,j,a; 6 for(i=n;i>=1;i--) 7 { 8 for(a=n-i;a>0;a--) 9 printf("\t"); 10 for(j=2*i-1;j>=1;j--) 11 printf(" O\t"); 12 printf("\n"); 13 for(a=n-i;a>0;a--) 14 printf("\t"); 15 for(j=2*i-1;j>=1;j--) 16 printf("<H>\t"); 17 printf("\n"); 18 for(a=n-i;a>0;a--) 19 printf("\t"); 20 for(j=2*i-1;j>=1;j--) 21 printf("I I\t"); 22 printf("\n"); 23 printf("\n"); 24 printf("\n"); 25 } 26 } 27 28 int main(){ 29 int n; 30 printf("Enter n:"); 31 scanf("%d",&n); 32 print_charman(n); 33 system("pause"); 34 return 0; 35 }