实验1:
1 #include<stdio.h> 2 #include<stdlib.h> 3 char score_to_grade(int score); 4 int main(){ 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 system("pause"); 12 return 0; 13 } 14 char score_to_grade(int score){ 15 char ans; 16 switch(score/10){ 17 case 10: 18 case 9:ans='A';break; 19 case 8:ans='B';break; 20 case 7:ans='C';break; 21 case 6:ans='D';break; 22 default:ans='E';break; 23 } 24 return ans; 25 }
问题1:将成绩转化成等第。整型。字符型。
问题2:ABCD要用单引号,没break会忽略case,继续向下执行语句
实验2:
#include<stdio.h> #include<stdlib.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); } system("pause"); return 0; } int sum_digits(int n){ int ans=0; while(n!=0){ ans+=n%10; n/=10; } return ans; }
问题1:算出所有位上的数字之和
问题2:可以,前一个是迭代,后一个是递归
实验3:
1 #include<stdio.h> 2 #include<stdlib.h> 3 int power(int x,int n); 4 int main(){ 5 int x,n; 6 int ans; 7 while(printf("Enter x and n:"),scanf("%d%d",&x,&n)!=EOF){ 8 ans=power(x,n); 9 printf("n=%d,ans=%d\n\n",n,ans); 10 } 11 system("pause"); 12 return 0; 13 } 14 int power(int x,int n){ 15 int t; 16 if(n==0) 17 return 1; 18 else if(n%2) 19 return x*power(x,n-1); 20 else{ 21 t=power(x,n/2); 22 return t*t; 23 } 24 }
问题1:算出x的n次方
问题2:是
实验4:
1 #include<stdio.h> 2 #include<stdlib.h> 3 int is_prime(int n); 4 int main(){ 5 int n,t,a,b; 6 int sum=0; 7 printf("100以内的孪生素数:\n"); 8 for(n=2;n<=98;n++){ 9 a=is_prime(n); 10 t=n+2; 11 b=is_prime(t); 12 if(a&&b){ 13 printf("%d %d\n",n,t); 14 sum++; 15 } 16 } 17 printf("100以内的孪生素数共有%d个\n",sum); 18 system("pause"); 19 return 0; 20 } 21 int is_prime(int n){ 22 int i; 23 for(i=2;i<=n/2;i++) 24 if(n%i==0) 25 return 0; 26 return 1; 27 }
实验5:
1 #include<stdio.h> 2 #include<stdlib.h> 3 void hanoi(int n,char A,char B,char C); 4 void move(int n,char from,char to); 5 int sum=0; 6 int main(){ 7 int n; 8 while(scanf("%d",&n)!=EOF){ 9 hanoi(n,'A','B','C'); 10 printf("\n一共移动了%d次\n\n",sum); 11 sum=0; 12 } 13 system("pause"); 14 return 0; 15 } 16 void hanoi(int n,char A,char B,char C){ 17 if(n==1) 18 move(n,A,C); 19 else{ 20 hanoi(n-1,A,C,B); 21 move(n,A,C); 22 hanoi(n-1,B,A,C); 23 } 24 } 25 void move(int n,char from,char to){ 26 printf("%d:%c-->%c\n",n,from,to); 27 sum++; 28 }
实验6:
代码1:
1 #include<stdio.h> 2 #include<stdlib.h> 3 int func(int n,int m); 4 int main(){ 5 int n,m; 6 int ans; 7 while(scanf("%d%d",&n,&m)!=EOF){ 8 ans=func(n,m); 9 printf("n=%d,m=%d,ans=%d\n\n",n,m,ans); 10 } 11 system("pause"); 12 return 0; 13 } 14 int func(int n,int m){ 15 int up=1,down=1,i; 16 if(n<m) 17 return 0; 18 else if(n==m||m==0) 19 return 1; 20 else 21 for(i=1;i<=m;i++) 22 down*=i; 23 for(i=1;i<=m;i++){ 24 up*=n; 25 n--; 26 } 27 return up/down; 28 }
代码2:
1 #define _CRT_SECURE_NO_WARNINGS 2 #include<stdio.h> 3 #include<stdlib.h> 4 int func(int n,int m); 5 int main(){ 6 int n,m; 7 int ans; 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 system("pause"); 13 return 0; 14 } 15 int func(int n,int m){ 16 int result; 17 if(n<m) 18 return 0; 19 else if(m==0||n==m) 20 return 1; 21 else 22 return result=func(n-1,m)+func(n-1,m-1);; 23 }
实验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 system("pause"); 10 return 0; 11 } 12 void print_charman(int n){ 13 int i,j,a; 14 for(i=n;i>=1;i--){ 15 for(a=n-i;a>0;a--) 16 printf("\t"); 17 for(j=2*i-1;j>=1;j--) 18 printf(" O\t"); 19 printf("\n"); 20 for(a=n-i;a>0;a--) 21 printf("\t"); 22 for(j=2*i-1;j>=1;j--) 23 printf("<H>\t"); 24 printf("\n"); 25 for(a=n-i;a>0;a--) 26 printf("\t"); 27 for(j=2*i-1;j>=1;j--) 28 printf("I I\t"); 29 printf("\n"); 30 printf("\n"); 31 printf("\n"); 32 } 33 }
标签:return,int,ans,char,实验,printf,include From: https://www.cnblogs.com/liuseki/p/18504255