实验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 15 return 0; 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 default: ans='E'; 27 } 28 29 return ans; 30 }
图片
判断等级 int char
有,没有break语句会继续执行下面指令
任务2
源代码
#include<stdio.h> int sum_digits(int n); int main(){ int n; int ans; while(printf("Enter n:"),scanf("%d",&n)!=EOF){ ans=sum_digits(n); printf("n=%d,ans=%d\n\n",n,ans); } return 0; } int sum_digits(int n){ int ans=0; while(n!=0){ ans+=n%10; n/=10; } return ans; } //迭代 /*int sum_digits(int n){ if(n<10) return n; return sum_digits(n/10)+n%10; }//递归 */
图片
将各个位上的数相加求和
可以,迭代和递归的区别
任务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 int power(int x,int n){ 18 int t; 19 20 if(n==0) 21 return 1; 22 else if(n%2) 23 return x*power(x,n-1); 24 else{ 25 t=power(x,n/2); 26 return t*t; 27 } 28 }
图片
x的n次方
任务4
源代码
1 #include<stdio.h> 2 int is_prime(int n); 3 4 int main(){ 5 int n,i; 6 i=0; 7 for(n=2;n<=100;n++) 8 { 9 if(is_prime(n)==1&&is_prime(n+2)==1){ 10 printf("%d %d\n",n,n+2); 11 i+=1; 12 } 13 } 14 printf("100以内的孪生素数共有%d个",i); 15 16 return 0; 17 } 18 19 is_prime(int n){ 20 int m,ans; 21 for(m=2;m<=n/2;m++) 22 if(n%m==0) 23 break; 24 if(m>n/2) 25 ans=1; 26 else 27 ans=0; 28 29 return ans; 30 31 }
图片
任务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 static int count=0; 6 int main(){ 7 unsigned int n; 8 while(scanf("%u",&n)!=EOF){ 9 count=0; 10 hanoi(n,'A','B','C'); 11 printf("一共移动了%d次\n",count); 12 } 13 system("pause"); 14 return 0; 15 } 16 void hanoi(unsigned int n,char from,char temp,char to) 17 { 18 if(n==1) 19 moveplate(n,from,to); 20 else 21 { 22 hanoi(n-1,from,to,temp); 23 moveplate(n,from,to); 24 hanoi(n-1,temp,from,to); 25 } 26 } 27 void moveplate(unsigned int n,char from,char to) 28 { 29 printf("%u:%c-->%c\n",n,from,to); 30 count=count+1; 31 }
图片
任务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,c,d,ans; 17 b=d=1; 18 for(a=0;a<=m-1;a++) 19 b=b*(n-a); 20 for(c=1;c<=m;c++) 21 d=d*c; 22 ans=b/d; 23 return ans; 24 }//迭代 */ 25 int func(int n,int m){ 26 if(n<m) 27 return 0; 28 if(m==0) 29 return 1; 30 else if(m==1) 31 return n; 32 else 33 return func(n-1,m)+func(n-1,m-1); 34 }//递归
图片
迭代
源代码
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,c,d,ans; 17 b=d=1; 18 for(a=0;a<=m-1;a++) 19 b=b*(n-a); 20 for(c=1;c<=m;c++) 21 d=d*c; 22 ans=b/d; 23 return ans; 24 }//迭代 25 /*int func(int n,int m){ 26 if(n<m) 27 return 0; 28 if(m==0) 29 return 1; 30 else if(m==1) 31 return n; 32 else 33 return func(n-1,m)+func(n-1,m-1); 34 }//递归 */
图片
任务7
源代码
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 char print_charman(int n); 5 6 int main() { 7 int n; 8 9 printf("Enter n: "); 10 scanf("%d", &n); 11 print_charman(n); 12 13 return 0; 14 } 15 char print_charman(int n) 16 { 17 int i,a,b,c,d,e; 18 e=n; 19 for(i=1;i<=e;i++) 20 { 21 for(d=1;d<=e-n;d++) 22 printf(" "); 23 for(a=1;a<=(2*n-1);a++) 24 printf(" O "); 25 printf("\n"); 26 for(d=1;d<=e-n;d++) 27 printf(" "); 28 for(b=1;b<=(2*n-1);b++) 29 printf(" <H> "); 30 printf("\n"); 31 for(d=1;d<=e-n;d++) 32 printf(" "); 33 for(c=1;c<=(2*n-1);c++) 34 printf(" I I "); 35 printf("\n"); 36 n--; 37 } 38 39 }
图片