实验3

task1

点击查看代码
#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;
}
![](https://img2024.cnblogs.com/blog/3527145/202410/3527145-20241023164040625-619238169.png)

将分数转化为等级
型参是整形,返回字符
没加break

=============================================================================================
task2

点击查看代码
#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;
}
![](https://img2024.cnblogs.com/blog/3527145/202410/3527145-20241023164627177-882108506.png)

求各位数的和
能,是递归的思想

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

求x的n次幂

============================================================================
task 4

点击查看代码
#include<stdio.h>

bool is_prime(int);

int main()
{
	for(int i = 1 ; i <= 100 ; i++)
	{
		if(is_prime(i) && is_prime(i+2))
			printf("%d %d\n",i,i+2);
	}
	
	return 0;
}

bool is_prime(int x)
{
	if(x==1)
		return 0;
	
	for(int i=2;i<=x/2;i++)
	{
		if(x % i == 0)
			return 0;
	}
	
	return 1;
}

![](https://img2024.cnblogs.com/blog/3527145/202410/3527145-20241023165609276-2051006316.png) =============================================================================================== task 5
点击查看代码
#include<stdio.h>

void Hanoi(int n , char from , char to ,char temp);

int cnt;

int main()
{
	int n;
	
	scanf("%d",&n);
	
	Hanoi(n , 'A' , 'C' , 'B');
	printf("%d",cnt);
	
	return 0;
}

void Hanoi(int n , char from , char to ,char temp)
{
	if (n==1)
	{
		printf("%d : %c --> %c\n",n , from , to);
		cnt++;
	}
	else
	{
		cnt++;
		Hanoi(n-1,from,temp,to);
		printf("%d : %c --> %c\n",n , from , to);
		Hanoi(n-1,temp,to,from);
	}
}
![](https://img2024.cnblogs.com/blog/3527145/202410/3527145-20241023172655200-78132481.png) ============================================================================================== task 6.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 mu=1,zi=1;
	int i;
	for( i=1; i<= m ;i++)
    {
		zi*=n-i+1;
		mu*=i;
	}
	return zi/mu;
}
![](https://img2024.cnblogs.com/blog/3527145/202410/3527145-20241023173320240-1638783367.png)

task 6.2

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

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 i,j;
	for( i = 0 ; i<n ; i++)
	{
		for( j = 0 ; j<i ; j++)
		{
			printf("\t");
		}
		for( j = 0 ; j<n-i ; j++)
		{
			printf(" O \t");
		}
		for( j = 0 ; j<n-i-1 ; j++)
		{
			printf(" O \t");
		}
		printf("\n");
		
		for( j = 0 ; j<i ; j++)
		{
			printf("\t");
		}
		for( j = 0 ; j<n-i ; j++)
		{
			printf("<H>\t");
		}
		for( j = 0 ; j<n-i-1 ; j++)
		{
			printf("<H>\t");
		}
		printf("\n");
		
		for( j = 0 ; j<i ; j++)
		{
			printf("\t");
		}
		for( j = 0 ; j<n-i ; j++)
		{
			printf("I I\t");
		}
		for( j = 0 ; j<n-i-1 ; j++)
		{
			printf("I I\t");
		}
		printf("\n\n");
	}
	
}
![](https://img2024.cnblogs.com/blog/3527145/202410/3527145-20241023192921275-2021059443.png)
posted @ 2024-10-23 19:30  yuanzhan  阅读(22)  评论(0编辑  收藏  举报