实验三

实验任务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;
}

运行截图:
image
Q1:该函数的作用是通过分数来确定对应的等级,形参类型是整数,返回值类型是字符。
Q2:错误①case6.7.8.9四个分支后面缺少了break,会导致程序在运行完其中一行之后继续运行下面的分支,导致ans的赋值出错。
错误②""应改为'',因为此处的A是单个字符,而双引号是用来表示字符串的。

实验任务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;
}

运行截图:
image
Q1:该函数是用来计算一个数字各位数字的和。
Q2:可以实现。第一种方法是迭代思维,第二种方法是递归思维。

实验任务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;
	}
}

运行截图:
image
Q1:用来计算x的n次方。
Q2:是递归函数。power(x,n)=
{1,n=0
x*power(x,n-1),n%2=1
power(x,n/2)², n%2=0}

实验任务4

实验代码:

#include<stdio.h>

int is_prime(int n);
#define N 100
int main(){
	int s=0;
	int i;
	printf("100以内的孪生素数:\n");
	for(i=2;i<=N;++i){
		if(is_prime(i)==1 && is_prime(i+2)==1){
			printf("%d %d\n",i,i+2);
			s+=1;}
	}
	printf("100以内的孪生素数共有%d个\n",s);

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

运行截图:
image

实验任务5

实验代码:

#include<stdio.h>
int s=0;
void hanoi(unsigned int n,char from,char temp,char to);
void moveplate(unsigned int n,char from,char to);
int main(){ 
   
	unsigned int n;
	while(scanf("%u",&n)!=EOF){ 
	    s=0;
		hanoi(n,'A','B','C');
		printf("\n");
		printf("一共移动了%d次\n",s);
	}

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

运行截图:
image

实验任务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){
    int ans=0,s=n-m;
	int a=1,b=1,c=1;
	while(s>0){
	a*=s;
	s-=1;
	}
    for(n;n>0;--n){
    b*=n;
	}
	for(m;m>0;--m){
	c*=m;
	}
	ans=b/(a*c);
	return ans;

}

实验代码②:

#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 ans=0;
	while(m>n)
		return 0;
	while(m==0||n==m)
		return 1;
	ans=func(n-1,m)+func(n-1,m-1);
	return ans;
}

运行截图①:
image
运行截图②:
image

实验任务7

实验代码:

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

	return 0;
}

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

	}return 1;
}

运行截图:
image

posted @ 2025-04-04 21:40  子春拾捌  阅读(41)  评论(0)    收藏  举报