实验三

#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;
}

score_to_grade功能是将分数转变成等级,形参类型为整形,返回类型为字符型

没有break,输出一直为e

#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;
}

函数sum_digits的功能是计算各位数的和

可以,原题是迭代,这个是递归

#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;
    }
}

函数power的功能是算出x的n次方

是,如果n=0就返回1,如果n为奇数则x^n=x*x^(x-1),如果n为偶数则x^n=x^n/2*x^n/2

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

#include<stdio.h>
void hanio(int n,int *count,char from,char temp,char to);
void move(int n,char from,char to);

int main(){
    int n,count=0;
    while(scanf("%u",&n)!=EOF){
        hanio(n,&count,'A','B','c');
        printf("一共移动了%d次\n",count);
        count=0;
    }
    return 0;
}
void hanio(int n,int *count,char from,char temp,char to){
    if(n==1){
        move(1,from,to);
        (*count)++;
        }
    else{
        hanio(n-1,count,from,to,temp);
        move(n,from,to);
        (*count)++;
        hanio(n-1,count,temp,from,to);
    }
}
void move(int n,char from,char to){
    printf("%u:%c-->%c\n",n,from,to);
}

#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){
    if (m > n || m < 0) return 0;
    int a = 1;
    int b= 1;
    int i;
    for (i=n; i > n - m; i--) {
        a *= i;
    }
    for (i = 1; i <= m; i++) {
        b *= i;
    }
    return a / b;
}

 

#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){
    if (m == 0 || m == n) return 1;
    if (m > n || m < 0) return 0;
    return func(n - 1, m) + func(n - 1, m - 1);
}

#include<stdio.h>
#include<stdlib.h>
void print_charman(int n);
int main(){
    int n;
    printf("Eenter n:");
    scanf("%d",&n);
    print_charman(n);

    return 0;
}
void print_charman(int n){
    int i,j,m;
    for(i=0; i<n;i++){
        for(m=0;m<i;m++)
            printf("   ");
        for (j=0;j<n-i;j++)
            printf("  o  ");
        printf("\n");
        for(m=0;m<i;m++)
            printf("   ");
        for(j=0;j<n-i;j++)
            printf(" <H> ");
        printf("\n");
        for(m=0;m<i;m++)
            printf("   ");
        for(j=0;j<n-i;j++)
            printf(" I I ");
        printf("\n");
    }
}

 

posted @ 2024-10-27 20:05  朱宁苇  阅读(6)  评论(0编辑  收藏  举报