实验3

#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:int类型char 类型


3:有问题,在switch语句中没有break的情况下,如果某个case的条件满足,程序会执行case以及之后的所有case语句。


 

#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:是,f(n)=f(n\2)*f(n\2\2);

#include<stdio.h>
#include<math.h>
int is_prime(int x)
{
    int i, k;
    k = sqrt(x);
    for(i=2;i<=k;i++)
    if(x%i==0)
        return 0;
    if(i>k)
        return 1;
}
int main(){
    printf("100以内的孪生素数:\n");

    int n=100;
    int m=0;
    int j;
    for(j=2;j<=n;j++){
    if(is_prime(j)&&is_prime(j+2)){
    m++;
    printf("%d %d\n",j,j+2);
        }
    }
    printf("100以内的孪生素数共有%d个\n",m);
    
   
    
    return 0;
}

#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 total=0
;
int
 main()
{
    unsigned 
int
 n;
    while (scanf("%u",&n)!=
EOF){
    total 
= 0
;
    hanoi(n, 
'A', 'B', 'C'
);
    printf(
"一共移动%d次"
,total);
    }
    
 return 0
;
}

 void hanoi(unsigned int n, char from,char temp, char
 to)
{
 if(n==1
)
{
    moveplate(n, 
from
, to);
    total
++
;
}
    else
    {
    hanoi(n
-1, from
, to, temp);
    moveplate(n, 
from
, to);
    hanoi(n
-1,temp, from
,to);
    total
++
;
    }
}
 void moveplate(unsigned 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;
}
func(int n, int m)
{
    int i, j;
    int p=1, q=1;
    for(i=n-m+1;i<=n;i++){
    p*=i;}
    for(j=1;j<=m;j++){
    q*=j;}
    return p/q;
}

#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;
}
func(int n, int m)
{
    if(m>n)
    return 0;
    else if(m==n||m==0)
    return 1;
    else
    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 i,j,k;
    for(i=n;i>0;i--){
        for(k=1;k<=n-i;k++){
            printf("   \t");
        }
            j=2*i-1;
            for(j;j>0;j--){
                printf(" O \t");
            }
            printf("\n");
        
        for(k=1;k<=n-i;k++){
            printf("   \t");
        }
            j=2*i-1;
            for(j;j>0;j--){
                printf("<H>\t");
            }
            printf("\n");
        for(k=1;k<=n-i;k++){
            printf("   \t");
        }
            j=2*i-1;
            for(j;j>0;j--){
                printf("I I\t");
            }printf("\n");
    }
}

 

posted @ 2024-10-28 13:22  你の老施  阅读(6)  评论(0编辑  收藏  举报