实验3

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

#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';
    case 8:   ans = 'B';
    case 7:   ans = 'C';
    case 6:   ans = 'D';
    default:  ans = 'E';
    }

    return ans;
}

 

 

 

问题1:将分数分为“A”,“B”,“C”,“D”,“E”五个等级。 整数型 字符型

问题2:有问题,无论输入数字为什么,均会输出“E”。

 

 

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

#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) {
   if(n<10)
           return n;
   
   
   return sum_digits(n/10) + n%10;
    }

 

 

 

问题1:将输入数的各位相加输出和

问题2:效果相同,算法1直接通过递归算出各位数字之和;算法2先判断n与10的大小,小于10直接输出,大于10同算法1.

 

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

问题2:

 

 

task.4

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

int is_prime(int n); 
int main()
{
    int i, count=0;
    for(i = 2;i <=100;i++)
    {
        if(is_prime(i)&&is_prime(i+2))
            printf("%d,%d\n",i, i+2);
        count++;
    }
    printf("100以内的孪生素数共有%d",count);
    return 0;
}
    int is_prime(int n)
    {
        int i;
        for(i = 2;i<=sqrt(n);i++)
            if(n%i==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 count=0;
int main()
{
    unsigned int n;
    while(scanf("%u",&n)!=EOF)
{ 
    hanoi(n,'A','B','C');
    printf("一共移动了%d次\n\n",count);
    count=0;
}

    system("pause");
    return 0;
}
void hanoi(unsigned int n,char from,char temp,char to)
{
    if(n == 1)
    {
        moveplate(n,from,to);
        count++;
}
    else
    {
        hanoi(n-1,from,to,temp);
        moveplate(n,from,to);
        count++;
        hanoi(n-1,temp,from,to);
    }
}
void moveplate(unsigned int n,char from,char to)
{
    printf("%u:%c-->%c\n\n",n,from,to);
}

 

 

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

迭代:

#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, j,up, down;
     for(i=n;i>=(m-1);i--)
     {
         up=up*i;
     }
    for(j=m;j>=1;j--)
    {
        down=down*j;
    }
    return up/down;
 }

task.7

#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,i,x,b,a;
 while(m!=-1){    
 for(x=0;x<count;x++){
   printf("\t");
  }
 for(i=0; i<m ;i++){
    printf(" O\t");
 }
  printf("\n");        
for(x=0;x<count;x++){
   printf("\t");
 }
 for(a=0; a<m ;a++){
     printf("<H>\t");
 }
 printf("\n");
for(x=0;x<count;x++){
     printf("\t");
 }
 for(b=0; b<m ;b++){
     printf("I I\t");
 }
 printf("\n");
count++;
m=m-2;
}
}

 

posted @ 2024-10-26 19:14  凌司书  阅读(5)  评论(0编辑  收藏  举报