实验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 return ans; 29 }
问题1
根据输入的分数输出等级 整型 字符型
问题2
有,无论输入的成绩是多少都输出同一等级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 18 int sum_digits(int n){ 19 int ans=0; 20 while(n!=0){ 21 ans+=n%10; 22 n/=10; 23 } 24 return ans; 25 }
问题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("Entet 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 return 0; 14 } 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 }
问题1
计算输入值x的n次方
问题2
是
任务4
1 #include <stdio.h> 2 int is_prime(int a){ 3 int i; 4 for(i=2;i<=a/2;i++) 5 { 6 if(a%i==0) 7 break; 8 } 9 if(i<=a/2) 10 return 0; 11 else 12 return 1; 13 14 } 15 int main(){ 16 int a,count=0; 17 printf("100以内的孪生素数:\n"); 18 for(a=2;a<=100;a++){ 19 if(is_prime(a)&&is_prime(a+2)){ 20 count++; 21 printf("%d %d\n",a,a+2); 22 23 } 24 25 } 26 printf("100以内的孪生素数有%d个",count); 27 return 0; 28 }
任务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 a=0; 6 int main() 7 { 8 unsigned int n; 9 while(scanf("%u",&n)!=EOF){ 10 hanoi(n,'A','B','C'); 11 printf("一共移动了%d次\n",a); 12 a=0; 13 } 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 ++a; 31 }
任务6
1
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 ans=0,a=1,b=1,c=1; 18 int i; 19 for(i=1;i<=n;++i) 20 a=a*i; 21 for(i=1;i<=m;++i) 22 b=b*i; 23 for(i=1;i<=(n-m);++i) 24 c=c*i; 25 ans=a/(b*c); 26 }
2
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 ans=0; 18 if(m==0) 19 return 1; 20 else if(n==0) 21 return 0; 22 else 23 ans=func(n-1,m)+func(n-1,m-1); 24 25 }
任务7
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 void 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 16 void print_charman(int n){ 17 int i,m,j,a; 18 for(j=0;j<n;++j){ 19 m=n*2-1-j*2; 20 for(a=0;a<j;++a) 21 printf("\t"); 22 for(i=0;i<m;++i) 23 printf(" O \t"); 24 printf("\n"); 25 for(a=0;a<j;++a) 26 printf("\t"); 27 for(i=0;i<m;++i) 28 printf("<H>\t"); 29 printf("\n"); 30 for(a=0;a<j;++a) 31 printf("\t"); 32 for(i=0;i<m;++i) 33 printf("I I\t"); 34 printf("\n"); 35 } 36 37 }