实验3
源代码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 char score_to_grade(int score) { 18 char ans; 19 20 switch(score/10) { 21 case 10: 22 case 9: ans = 'A'; break; 23 case 8: ans = 'B'; break; 24 case 7: ans = 'C'; break; 25 case 6: ans = 'D'; break; 26 default: ans = 'E'; 27 } 28 29 return ans; 30 }
截图1:
问题1.1:
作用:将分数转换为等级;
型参类型:整型
返回值类型:字符型
问题1.2:缺少break跳出循环,导致一直向下运行,输出E
源代码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 int sum_digits(int n){ 18 int ans = 0; 19 while(n !=0){ 20 ans+=n%10; 21 n/=10; 22 } 23 24 return ans; 25 }
截图2:
问题2.1:
作用:把输入数的每个位上的数字加起来求和
问题2.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 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 30 }
截图3:
问题3.1:
计算x的n次方
问题3.2:
公式:y=x^n
n=0时,y=1
n为偶数时,y=(x^n/2)^2
n为奇数时,y=x^(n-1)*x
源代码4:
1 #include<stdio.h> 2 #include<math.h> 3 #include<stdlib.h> 4 5 int is_prime(int n); 6 7 int main(){ 8 int n=1; 9 int total=0; 10 int i; 11 printf("100以内的孪生素数\n"); 12 for(n=1;n<=97;n++){ 13 if(is_prime(n)&&is_prime(n+2)){ 14 printf("%d %d\n",n,n+2); 15 total+=1; 16 } 17 18 } 19 printf("100以内的孪生素数共有%d个",total); 20 21 return 0; 22 23 24 25 } 26 27 int is_prime(int n){ 28 int i,m; 29 m=sqrt(1.0*n); 30 if (n<=1){ 31 return 0; 32 } 33 34 for(i=2;i<=m;i++) 35 if(n%i==0){ 36 return 0; 37 } 38 return 1; 39 40 41 42 } 43
截图4:
源代码5:
1 #include <stdio.h> 2 #include<stdlib.h> 3 #include<math.h> 4 5 void hanoi(int n,char from,char temp,char to); 6 void moveplate(int n,char from,char to); 7 8 int main(){ 9 int n,m; 10 while(scanf("%d",&n)!=EOF){ 11 hanoi(n,'A','B','C'); 12 m=2^n-1; 13 printf("一共移动了%d次\n",m); 14 15 } 16 17 system("pause"); 18 return 0; 19 20 } 21 22 void hanoi(int n,char from,char temp,char to){ 23 if(n==1) 24 moveplate(n,from,to); 25 else{ 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(int n,char from,char to){ 33 printf("%d:%c-->%c\n",n,from,to); 34 }
截图5:
源代码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 //迭代写法 16 int func(int n,int m){ 17 int a,b,x,y,ans; 18 a=1,b=1; 19 for(x=1;x<=m;++x){ 20 a*=x; 21 } 22 for(y=n-m+1;y<=n;++y){ 23 b*=y; 24 } 25 ans=(b*1.0)/(a*1.0); 26 27 return ans; 28 29 }
递归:
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 16 int func(int n,int m){ 17 if(n<m){ 18 return 0; 19 } 20 else if(m==0||n==m){ 21 return 1; 22 23 } 24 else{ 25 return func( n-1, m)+func(n-1,(m-1)); 26 } 27 28 29 } 30
截图6:
源代码7:
1 #include <stdio.h> 2 #include <stdlib.h> 3 void print_charman(int n); 4 int main() { 5 int n; 6 printf("Enter n: "); 7 scanf("%d", &n); 8 print_charman(n); 9 return 0; 10 } 11 void print_charman(int n) 12 { 13 int x, y; 14 for (x = 0; x< n; x++) 15 { 16 for (y= 0; y < x; y++){ 17 printf("\t"); 18 } 19 for (y = 0; y < n - x; y++){ 20 printf(" O \t"); 21 } 22 for (y = 0; y < n - x - 1; y++){ 23 printf(" O \t"); 24 } 25 printf("\n"); 26 27 for (y = 0; y < x; y++){ 28 printf("\t"); 29 } 30 for (y = 0; y < n - x; y++){ 31 printf("<H>\t"); 32 } 33 for (y = 0; y < n - x - 1; y++){ 34 printf("<H>\t"); 35 } 36 printf("\n"); 37 38 for (y = 0; y < x; y++){ 39 printf("\t"); 40 } 41 for (y = 0; y< n - x; y++){ 42 printf("I I\t"); 43 } 44 for (y = 0; y < n - x - 1; y++){ 45 printf("I I\t"); 46 } 47 printf("\n\n"); 48 49 } 50 51 }
截图7: