首页 > 其他分享 >3

3

时间:2024-10-29 21:30:47浏览次数:1  
标签: return int ans char score printf

`#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.有问题,这样计算机接收的不再是ASCLL编码,而是ABCD的字符

`#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.具有同等效果,原题中是对n取每一位数加上ans,后面那个是先判断是否是个位数再进行操作。

`#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.功能:乘方函数
2.是。使用的是

`#include<stdio.h>
int is_prime(int n){
int i;
if(n<=1){

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

}

int main()
{
int n,x=0,i=2;
printf("100以内的素数是\n");
for(i=2;i<100;i++){
if(is_prime(i)&&is_prime(i+2)){
printf("%d %d\n",i,i+2);
x++;
}
}
printf("100以内的孪生素数的总数为:%d\n",x);
return 0;
}![](/i/l/?n=24&i=blog/3526497/202410/3526497-20241029150703257-161096064.png)

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 i=0;
int main()
{
unsigned int n;
while(scanf("%u",&n)!=EOF){
hanoi(n,'A','B','C');
printf("一共运行了%d次\n",i);
}

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);
i+=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 ans=1,i=1;
for(i=n;i>=(n-m+1);i--)
ans*=i;
for(i=1;i<=m;i++)
ans/=i;
return ans;
}![](/i/l/?n=24&i=blog/3526497/202410/3526497-20241029213330206-898254226.png)#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(m0||nm)
return 1;
if(m>n)
return 0;
return func(n-1,m)+func(n-1,m-1);
}![](/i/l/?n=24&i=blog/3526497/202410/3526497-20241029213535279-282800620.png)#include <stdio.h>

include <stdlib.h>

char print_charman(int n);
int main() {
int n;
printf("Enter n: ");
scanf("%d", &n);
print_charman(n);
return 0;
}
char print_charman(int n)
{
int t = 0;
for (int i = n; i >= 1; i--)
{
for (int j = 0; j < t; j++)
printf("\t");
for (int j = 0; j < 2 * i - 1; j++)
printf(" o\t");
printf("\n");
for (int j = 0; j < t; j++)
printf("\t");
for (int j = 0; j < 2 * i - 1; j++)
printf("\t");
printf("\n"); for (int j = 0; j < t; j++)
printf("\t");
for (int j = 0; j < 2 * i - 1; j++)
printf("I I\t");
printf("\n");
t++;
}
}`

标签:,return,int,ans,char,score,printf
From: https://www.cnblogs.com/xkzm/p/18497848

相关文章