實驗3 C語言函式應用程式設計

井灏扬發表於2024-10-29

#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:判斷完之後不會終止,而是執行下一行,最終永遠輸出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;
}

問題1:將輸入數的每一位數字相加

問題2:能,一種是迭代計算答案,一種是遞迴計算答案

#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次方

問題2:是

#include<stdio.h>
int is_prime(int n){
    int i;
    for(i=2;n>=i;i++){
        if(n%i==0&&n>i)
            return 0;
        else if(n%i==0&&n==i)
            return 1;
        
    }
    
}
int main(){
    printf("100以內孿生素數:\n");
    int num=2,a=0;
    while(num<100){
        if(is_prime(num)==1&&is_prime(num+2)==1){
            printf("%d %d\n",num,num+2);
            a++;
            
        }
        num++;
    
    }
    printf("100以內孿生素數共%d個",a);    
        
    return 0;    
}

#include<stdio.h>
int m=0;
int hanoi(int n,char from,char by,char to){
    if (n==2){
        printf("1:%c-->%c\n",from,by);
        printf("2:%c-->%c\n",from,to);
        printf("1:%c-->%c\n",by,to);
        
        m+=3;
        return 0;
    }

    hanoi(n-1,from,to,by);
    printf("%d:%c-->%c\n",n,from,to);
    hanoi(n-1,by,from,to);
    
    m++;
    return 0;
}

int main(){
    int a;
    while(scanf("%d",&a)!=EOF){
        char from='A',to='C',by='B';
        hanoi(a,from,by,to);
        printf("一共移動了%d次\n",m);
        m=0;
    }
    return 0;
}

#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 up=n,down=m,i=m,j=n,l=n-m;
    if(n<m)
        return 0;
    else if(l==0||m==0)
        return 1;
    while(i!=1){
        down=down*(i-1);
        i--;
    }
    while(j!=1){
        up=up*(j-1);
        j--;
    }
    while(l!=0){
        down=down*l;
        l--;
    }
    return up/down;
}

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

#include <stdio.h>
#include <stdlib.h>

void print_1(int a,int b){
    int i=1,j=b;    
    while(j-1>0){
        printf("    ");
        --j;    
    }
    while(i<=(a*2-1-(b-1)*2)){
        i++;    
        printf(" O      ");
    }
    printf("\n");    
    
    i=1;j=b; 
    while(j-1>0){
        printf("    ");
        --j;    
    }
    while(i<=(a*2-1-(b-1)*2)){
        i++;
        printf("<H>     ");
    }
    printf("\n");
    
    i=1;j=b;
    while(j-1>0){
        printf("    ");
        --j;    
    }    
    while(i<=(a*2-1-(b-1)*2)){
        i++;
        printf("I I     ");
    }
    printf("\n");
    
    
    
}
void print_charman(int n){
    int i=1;
    while(i<=n){
        print_1(n,i);    
        i++;
        
    }
    
}


int main() {
    int n;
    
    printf("Enter n: ");
    scanf("%d", &n);
    print_charman(n); 
    
    return 0;
}

相關文章