实验3 C语言函数应用编程

实验一

#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不会跳出循环,直接执行edfault的语句

实验二

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

​​

实验四

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

int is_prime(int n){
    if (n<=1){
        return 0;
    }
    for (int i=2;i*i<=n;i++)
        if (n%i==0){
            return 0;
        }return 1;
}

实验五

 

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

实验六

迭代

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

递归

#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;
    if(m==0) return 1;
    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("Enter n: ");
    scanf("%d", &n);
    print_charman(n); // 函数调用
    return 0;
}
void print_charman(int n){
    int m = ((n*2)-1) ,count=0;
while(m!=-1){
    
        for(int x=0;x<count;x++){
            printf("       ");
        }
        for(int i=0; i<m ;i++){
                printf(" O     ");
        }
         printf("\n");
    
        
        for(int x=0;x<count;x++){
            printf("       ");
        }
        for(int a=0; a<m ;a++){
                printf("<H>    ");
        }
         printf("\n");
    
    
        for(int x=0;x<count;x++){
            printf("       ");
        }
        for(int b=0; b<m ;b++){
                 printf("I I    ");
        }
         printf("\n");
    
    
        count++;
        m=m-2;
}
}

 

posted @ 2024-10-24 17:23  zjcblogs  阅读(28)  评论(0编辑  收藏  举报