实验3

实验任务1

代码

#include 

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.没有break,如果数字大于等于6,它会把下面的部分也执行。例如输入10将会输出“ABCDE”。

实验任务2

代码

#include 

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.计算数字n的各位数字之和。

2.可以。原来的是迭代,改后的是递归。

实验任务3

代码

#include 

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.函数power的作用是计算x的n次方。

2.是。

n = 0时,power(x,n)=1;

n%2=0时,power(x,n)=power(x,n/2)*power(x,n/2)

n%2≠0时,power(x,n)=x*power(x,n-1);

实验任务4

代码

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

int prime_is(int n);

int main(){
    int i = 0;
    int count = 0;
    for (i = 3; i <= 98; i++){
		if(prime_is(i)==1&&prime_is(i+2)==1){
			printf("%d %d\n",i,i+2);
			count+=1;}
	}
	printf("100以内的孪生质数总数为%d",count);

	system("pause");
	
    return 0;
}


int prime_is(int n){
    int j = 0;
	for (j = 2; j < n; j++)
        {
            if (n%j == 0)
            {
                return 0;
            }
        }
		return 1;

}

截图

实验任务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 s = 0;
int main() {

    unsigned int n;
    while(scanf("%u",&n) != EOF){
	s=0;
    hanoi(n,'A','B','C');
    printf("一共移动了%d次\n",s); 
    }    

    system("pause");
    return 0;
}


void hanoi(unsigned int n,char from,char temp,char to)
{
    if(n==1)
        moveplate(n,from,to);
    else
    {
        hanoi(n-1,from,to,temp);
        moveplate(n,from,to);
        hanoi(n-1,temp,from,to);
    }
}


void moveplate(unsigned int n,char from,char to)
{
    printf("%u:%c-->%c\n",n,from,to);
    s++; 
}

图片

实验任务6

代码

迭代

#include <stdio.h>
#include <stdlib.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);
    }

    system("pause");
    return 0;
}

int func(int n, int m){
	if(m>n)
		return 0;
	if (m==0||n==0)
		return 1;
	int fenzi = 1;
	int fenmu = 1;
	for(int i=1;i<=m;++i){
		fenzi = fenzi*(n-i);
		fenmu = fenmu*(m-i);
	}
	return fenmu/fenzi;
}
// 函数定义
// 待补足。。。(分别用迭代和递归实现)

递归

#include <stdio.h>
#include <stdlib.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);
    }

    system("pause");
    return 0;
}

int func(int n, int m) {
    if (m > n) {
        return 0;
    }

    if (m == 0 || m == n) {
        return 1;
    }

    return func(n - 1, m) + func(n - 1, m - 1);
}
// 函数定义
// 待补足。。。(分别用迭代和递归实现)

图片

实验任务7

代码

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

int gcd(int a,int b,int c);// 函数声明
// 待补足....

int main(){
    int a, b, c;
    int ans;

    while(scanf("%d%d%d", &a, &b, &c) != EOF) {
        ans = gcd(a, b, c);     // 函数调用
        printf("最大公约数: %d\n\n", ans);
    }

	system("pause");

    return 0;
}

int gcd(int a,int b,int c){
	int min;
	if(a<=b)
		min = a;
	else min = b;
	if(min>=c)
		min = c;
	for(int i = min;i>=1;i-=1){
		if (a%i==0&&b%i==0&&c%i==0)
			return i;
	}
}

// 函数定义
// 待补足...

图片

 

实验总结

1.练习了递归和迭代函数,认识到了这两种的不同。

2.能够定义函数并使用,增强了代码的简易性和可读性。

posted @ 2025-04-05 23:00  理塘小马守护者丁真  阅读(13)  评论(0)    收藏  举报