实验3.c

task.1

程序:

#include <stdio.h>
char score_to_grade(int score);

int main(){
    int score;
    char grade;
    while(scanf("%d",&score)!=EOF){
        grade=score_to_grade(score);
        printf("分数:%d,等级:%c\n\n",score,grade); 
    }
    return 0;
}

char score_to_grade(int score){
    char ans;
    switch(score/10){
        case 10:
        case 9: ans='A';break;
        case 8: ans='B';break;
        case 7: ans='C';break;
        case 6: ans='D';break;
        default: ans='E';
    }
    return ans;
}

运行:

问题1:功能是判断学生成绩对应的等级。形参类型是整形,返回值类型是字符型。

问题2:有问题。在判断一个case 成立后并不会跳出程序,而是继续执行下面的case ,输出对应的等级。

task.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;
}

运行:

问题1:计算一个整数各个位上数的和。

问题2:能实现相同效果。第一种是迭代法,第二种是递归法。

task.3

程序:

#include <stdio.h>
int power(int x,int n);
int main(){
    int x,n;
    int ans;
    while(printf("Enter x and n:"),scanf("%d%d",&x,&n)!=EOF){
        ans=power(x,n);
        printf("n=%d,ans=%d\n\n",n,ans); 
    }
    return 0;
} 

int power(int x,int n){
    int t;
    if(n==0)
       return 1;
    else if(n%2)
       return x*power(x,n-1);
    else{
        t=power(x,n/2);
        return t*t;
    }
}

运行:

问题1:是计算x 的n次方ans

问题2:n=0,ans=1

             n为奇数,x^n=x^(n-1)*x

             n为偶数,x^n=x^(n/2)*x^(n/2)

task.4

程序:

#include <stdio.h>
int is_prime(int x);
int main(){
    int n,i=0;
    printf("100以内的孪生素数:\n");
    for(n=2;n<=98;++n){
        if(is_prime(n)==1&&is_prime(n+2)==1){
           printf("%d %d\n",n,n+2);
           ++i;}
           }
           printf("100以内的孪生素数共有%d个",i);
}
int is_prime(int x){
    int m;
    if(x==2)
       return 1;
    for(m=2;m<=x/2;++m){
        if(x%m==0)
           return 0;
    }
    return 1;        
}

运行:

 

task.5

程序:

#include <stdio.h>
#include <stdlib.h>
void hanoi(unsigned int n,char from,char temp,char to);
void moveplate(unsigned int n,char from,char to);
int i=0;
int main(){
    unsigned int n;
    while(1){
    scanf("%u",&n);
    if(n==-1)
       break;
    hanoi(n,'A','B','C');
    printf("一共移动了%d次",i);
    printf("\n\n");
i=0;} system(
"pause"); return 0; } void hanoi(unsigned int n,char from,char temp,char to){ if(n==1) moveplate(n,from,to); else{ hanoi(n-1,from,to,temp); moveplate(n,from,to); hanoi(n-1,temp,from,to); } } void moveplate(unsigned int n,char from,char to){ printf("%u:%c-->%c\n",n,from,to); ++i; }

运行:

 

task.6

程序:

#include <stdio.h>
int func(int n,int m);

int main(){
    int n,m;
    int ans;
    
    while(scanf("%d%d",&n,&m)!=EOF){
        ans=func(n,m);
        printf("n=%d,m=%d,ans=%d\n\n",n,m,ans);
    }
    return 0;
} 

int func(int n,int m){
    int i,a=1,b=1,c=1,ans;
    for(i=1;i<=n;++i)
        a*=i;
    for(i=1;i<=m;++i)
        b*=i;
    for(i=1;i<=n-m;++i)
        c*=i;
    ans=a/(b*c);
    return ans;
}

 

#include <stdio.h>
int func(int n,int m);

int main(){
    int n,m;
    int ans;
    
    while(scanf("%d%d",&n,&m)!=EOF){
        ans=func(n,m);
        printf("n=%d,m=%d,ans=%d\n\n",n,m,ans);
    }
    return 0;
} 

int func(int n,int m){
    int ans;
if(n<m)
return 0;
if(m==0) return 1; if(m==n) return 1; ans=func(n-1,m)+func(n-1,m-1); return ans; }

运行:

 

 

task.7

程序:

#include <stdio.h>
#include <stdlib.h>
void printf_charman(int n);
int main(){
    int n;
    
    printf("Enter n:");
    scanf("%d",&n);
    printf_charman(n);
    
    return 0;
}

void printf_charman(int n){
    int x,i=1,z=1;
    for(x=1;x<=n;++x){
        
        if(x>1)
               for(i=2;i<=x;++i)
                   printf("     "); 
        for(i=z;i<=2*n-1;++i)
            printf(" o   ");
        printf("\n");
        
        
        if(x>1)
               for(i=2;i<=x;++i)
                   printf("     "); 
        for(i=z;i<=2*n-1;++i)    
            printf("<H>  ");
        printf("\n");
        
        
        
        if(x>1)
               for(i=2;i<=x;++i)
                   printf("     ");     
        for(i=z;i<=2*n-1;++i)    
            printf("I I  ");
        printf("\n");
        z+=2;
            
    }
}

运行:

 

posted @ 2024-10-28 20:21  5dgx36  阅读(5)  评论(0编辑  收藏  举报