首页 > 其他分享 >实验三

实验三

时间:2024-10-29 20:42:21浏览次数:2  
标签:count return int ans char 实验 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; }
运行结果:

答案一:设计一个独立的可复用函数,根据输入的数字来评判成绩等级
答案二:有,在后续情况函数中没有【break】的情况下,最后的结果会遍历,导致所有的结果都是E

实验任务二:
源代码:
#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 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 tt;
}
}`
运行结果:

答案一:算出给定数字x的n平方
答案二:
数学公式模型为
power(x, 0)= 1;
当n为奇数时,power(x, n)= x
power(x, n -1);
当n为偶数时,power(x, n)= power(x, n/2)* power(x, n/2)。

实验任务四:
源代码:
`#include <stdio.h>

include <math.h>

int is_prime(int num){
if (num <= 1) return 0;
for (int i = 2; i <= sqrt(num); i++) {
if (num % i == 0) return 0;
}
return 1;
}
int main() {
int count = 0;
printf("100以内的孪生素数: \n");
for (int i = 2; i < 100; i++) {
if (is_prime(i)&&is_prime(i+2)) {
printf("%d %d\n", i, i+2);
count++;
}
}
printf("100以内的孪生素数共有 %d个\n", count);
return 0;
}`
运行结果:

实验任务五:
源代码:
`#include <stdio.h>

include <stdlib.h>

void hanoi(unsigned int n, char from, char temp, char to, int *count);
void moveplate(unsigned int n, char from, char to, int *count);

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

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

void moveplate(unsigned int n, char from, char to, int count) {
printf("%u:%c-->%c\n", n, from, to);
(
count)++;
}`
运行结果:

实验任务六:
迭代法源代码:
#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 x=1; if(n<m) x=0; if(m==0) x=1; if(n>=m&&m!=0){ int a=1,b=1,c=1; for (int i = 2; i <= n; i++) { a*= i; } for (int j = 2; j <= m; j++) { b*= j; } for (int k = 2; k <= n-m; k++) { c*= k; } x=(a/b)/c; } return x; }
运行结果:

递归法源代码:
`#include <stdio.h>

int func_recursive(int n, int m) {
if (m > n || m < 0) return 0;
if (m == 0 || m == n) return 1;
return func_recursive(n - 1, m - 1) + func_recursive(n - 1, m);
}

int main() {
int n, m;
int ans;
while (scanf("%d%d", &n, &m)!= EOF) {
ans = func_recursive(n, m);
printf("n = %d, m = %d, ans = %d\n\n", n, m, ans);
}
return 0;
}`
运行结果:

实验任务七:
源代码:
`#include <stdio.h>

include <stdlib.h>

int print_charman(int n);

int main() {
int n;
printf("Enter n: ");
scanf("%d", &n);
print_charman(n);
return 0;
}

int print_charman(int n){
for(int t=0;t<n;t++){
int i=(n-t)2-1;
int j=t;
for(j;j>0;j--){
printf(" ");
}
for(i;i>0;i--){
printf(" O ");
}
printf("\n");
j=t;
for(j;j>0;j--){
printf(" ");
}
i=(n-t)
2-1;
for(i;i>0;i--){
printf(" ");
}
printf("\n");
j=t;
for(j;j>0;j--){
printf(" ");
}
i=(n-t)*2-1;
for(i;i>0;i--){
printf("I I ");
}
printf("\n");
}
return 0;
}`
运行结果:

标签:count,return,int,ans,char,实验,printf
From: https://www.cnblogs.com/hcjinniuzuo/p/18514428

相关文章

  • 实验二
    task1:t.h:点击查看代码#pragmaonce#include<string>classT{public:T(intx=0,inty=0);T(constT&t);T(T&&t);~T();voidadjust(intradio);voiddisplay()const;private:intm1,m2;public:static......
  • 实验3
    task_1#include<stdio.h>charscore_to_grade(intscore){charanswer;switch(score/10){case10:answer='A';break;case9:answer='A';break;case8:answer='B';break;......
  • 实验二
    task1main.cpp#include"t.h"#include<iostream>usingstd::cout;usingstd::endl;voidtest();intmain(){test();cout<<"\nmain:\n";cout<<"Tobjects'currentcount:"<......
  • 实验二 类和对象
    任务1代码:t.h:1#pragmaonce2#include<string>34classT{5public:6T(intx=0,inty=0);7T(constT&t);8T(T&&t);9~T();10voidadjust(intratio);11voiddisplay()const;12private:1......
  • 实验二
    任务一:t.cpp//类T:实现//普通函数实现#include"t.h"#include<iostream>#include<string>usingstd::cout;usingstd::endl;usingstd::string;//static成员数据类外初始化conststd::stringT::doc{"asimpleclasssample"};constintT:......
  • 实验三
    实验任务1:源代码:1#include<stdio.h>2charscore_to_grade(intscore);3intmain(){4intscore;5chargrade;67while(scanf("%d",&score)!=EOF){8grade=score_to_grade(score);9printf("分数:%d,等级:%c\n\n&quo......
  • 实验2 类和对象_基础编程1
    实验1task1.cppt.h:#pragmaonce#include<string>//类T:声明classT{//对象属性、方法public:T(intx=0,inty=0);//普通构造函数T(constT&t);//复制构造函数T(T&&t);//移动构造函数~T();//析构函数......
  • 实验三
    任务1 源代码#include<stdio.h>charscore_to_grade(intscore);intmain(){intscore;chargrade;while(scanf("%d",&score)!=EOF){grade=score_to_grade(score);printf("分数:%d,等级:%c\n\n",score,grade);......
  • 实验3 C语言函数应用编程
    1.实验任务1#include<stdio.h>charscore_to_grade(intscore);intmain(){intscore;chargrade;while(scanf("%d",&score)!=EOF){grade=score_to_grade(score);printf("分数:%d,等级:%c\n\n",score,grad......
  • 实验三
     task1score_to_grade作用:将输入的分数进行分等级输出形参类型:整形返回值类型:字符串如果改变函数定义,去掉line21-line28的break,程序将在每一此输入数值后继续进行输出,不能及时跳出switchtask2sum_digits的作用:将输入数据的每位上的数字加起来输出改变之后依然能够实......