作业

task1:

 1 #include<stdio.h>
 2 char score_to_grade(int score);
 3 int main(){
 4     int score;
 5     char grade;
 6     
 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 
14 char score_to_grade(int score){
15     char ans;
16     
17     switch(score/10){
18         case 10:
19         case 9:  ans = 'A';break;    
20         case 8:  ans = 'B';break;
21         case 7: ans = 'C';break;
22         case 6:  ans = 'D';break;
23         default:  ans = 'E';
24             
25     }
26     return ans;
27 }

问题1:功能是将分数转化为相应的等级。形参类型是整形,返回值类型是字符型。

问题2:有问题,更改后输入一个分数会输出许多等级。

task2:

 1 #include<stdio.h>
 2 int sum_digits(int n);
 3 int main(){
 4     int n;
 5     int ans;
 6     
 7     while(printf("Enter n:"),scanf("%d", &n)!=EOF){
 8         ans = sum_digits(n);
 9         printf("n = %d,ans = %d\n\n",n,ans);
10     }
11     return 0;
12     
13 }
14 
15 int sum_digits(int n){
16     int ans = 0;
17     while(n!=0){
18     ans+= n % 10;
19     n /=10;
20     }
21     return ans;
22 
23 }

问题一:sum_digits是自己写的函数,为了把输入的n每个数位上的数字提出来加在一起赋值给ans。

问题二:可以实现,第一种是利用迭代方式把n每个数字提出来加给ans。第二种是利用递归方式实现把n的每个数位的数字提出来给ans。

task3:

 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     }
11     return 0;
12 
13 }
14 int power(int x,int n){
15     int t;
16     
17     if(n == 0)
18     return 1;
19     else if(n%2)
20     return x*power(x,n-1);
21     else{
22         t = power(x,n/2);
23         return t*t;
24     }
25 }

问题一:通过递归实现x的n次方的计算。

问题二:power是递归函数。n==0是递归的边界,n为奇数时,可以表示为x*x的n-1次幂;n为偶数时,可以表示为x乘以x的一半次幂的平方。

 task4:

 1 #include<stdio.h>
 2 #include<math.h>
 3 int is_prime(int num){
 4     if (num<=1)
 5     return 0;
 6     int i=2;
 7     for(i;i<=sqrt(num);i++){
 8     if(num%i==0)
 9     return 0;
10     }
11     return 1;
12 
13 }
14 void double_prime(int x){
15     int i;
16     for(i=2;i<=x-2;i++){
17         if(is_prime(i)&&is_prime(i+2)){
18             printf("%d %d\n",i,i+2);
19         }
20     }
21 }
22 int main(){
23     int x=100;
24     double_prime(x);
25     return 0;
26 }

实验五:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 unsigned int move_count = 0; 
 5 
 6 void hanoi(unsigned int n, char from, char temp, char to);
 7 void moveplate(unsigned int n, char from, char to);
 8 
 9 int main() {
10     unsigned int n;
11     while (scanf("%u", &n)!= EOF) {
12         move_count = 0;  
13         hanoi(n, 'A', 'B', 'C');
14         printf("一共需要移动 %u 次\n",  move_count);
15     }
16     return 0;
17 }
18 
19 void hanoi(unsigned int n, char from, char temp, char to) {
20     if (n == 1) {
21         moveplate(n, from, to);
22     }
23     else {
24         hanoi(n - 1, from, to, temp);
25         moveplate(n, from, to);
26         hanoi(n - 1, temp, from, to);
27     }
28 }
29 
30 void moveplate(unsigned int n, char from, char to) {
31     move_count++;  
32     printf("%u:%c-->%c\n", n, from, to);
33 }

实验六:

递归:

 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     return 0;
13     
14 }
15 int fac(int num){
16     int i,p = 1;
17     for(i = 1;i<=num;i++){
18         p*=i;
19     }
20     return p;
21 }
22 int func(int n,int m){
23     if(n<m)
24     return 0;
25     if(n==m||m==0)
26     return 1;
27     else{
28         return fac(n)/(fac(m)*fac(n-m));
29     }
30 
31 }

迭代:

 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     return 0;
13     
14 }
15 int func(int n,int m){
16     if(n<m)
17     return 0;
18     if(n==m||m==0)
19     return 1;
20     int result = 1;
21     int i;
22     int min_m_nm = (m<(n-m))?m:(n-m);
23     for(i=0;i<min_m_nm;i++){
24         result*=(n-i);
25         result/=(i+1);
26         
27     }
28     return result;
29     
30 
31 }

实验七:

 

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 // 函数声明
 5 void print_charman(int n, int j);
 6 
 7 int main() {
 8     int n;
 9 
10     printf("Enter n: ");
11     scanf("%d", &n);
12     print_charman(n, 0);
13 
14     return 0;
15 }
16 
17 void print_charman(int n, int j)
18 {
19     if (n <= 0) {
20         return;
21     }
22     int i;
23      for (i = 0; i < j; i++) {
24         printf(" ");
25     }
26     for (i = 0; i < 2 * n - 1; i++) {
27         printf(" o     ");
28     }
29     printf("\n");
30      for (i = 0; i < j; i++) {
31         printf(" ");
32     }
33     for (i = 0; i < 2 * n - 1; i++) {
34         printf("<H>    ");
35     }
36     printf("\n");
37      for (i = 0; i < j; i++) {
38         printf(" ");
39     }
40     for (i = 0; i < 2 * n - 1; i++) {
41         printf("I I    ");
42     }
43     printf("\n");
44     print_charman(n - 1, j + 7);
45 }

 

posted @ 2024-10-27 19:19  落半月尘  阅读(1)  评论(0编辑  收藏  举报