首页 > 其他分享 >实验三

实验三

时间:2024-10-29 14:45:17浏览次数:3  
标签:return int printf rod 实验 ans include

实验一:

源代码:

 1 #include <stdio.h>
 2 char score_to_grade(int score);  
 3 int main() {
 4     int score;
 5     char grade;
 6 
 7     while(scanf("%d", &score) != EOF) {
 8         grade = score_to_grade(score); 
 9         printf("分数: %d, 等级: %c\n\n", score, grade);
10     }
11     return 0;
12 }
13 char score_to_grade(int score) {
14     char ans;
15     switch(score/10) {
16     case 10:
17     case 9:   ans = 'A'; break;
18     case 8:   ans = 'B'; break;
19     case 7:   ans = 'C'; break;
20     case 6:   ans = 'D'; break;
21     default:  ans = 'E';
22     }
23     return ans;
24 }

 

运行结果:

问题:函数功能是将分数与等级挂钩,进行转换。形参是int,返回值是char。

实验二:

源代码:

 1 #include<stdio.h>
 2 int sum_digits(int n);
 3 int main(){
 4     int n;
 5     int ans;
 6     while(printf("Enter n:"),scanf("%d",&n)!=EOF)
 7     {
 8         ans=sum_digits(n);
 9         printf("n=%d,ans=%d\n\n",n,ans);
10     }
11     return 0;
12 }
13 int sum_digits(int n){
14     int ans=0;
15     while(n!=0){
16         ans+=n%10;
17         n/=10;
18     }
19     return ans;
20 }

运行结果:

问题:函数是计算所有数字之和

可以实现,改后是利用递归的方式计算

 

实验三:

源代码:

 1 #include<stdio.h>
 2 int power(int x,int n);
 3 int main()
 4 {
 5     int x,n;
 6     int ans;
 7     while(printf("Enter x and n"),scanf("%d%d",&x,&n)!=EOF){
 8         ans=power(x,n);
 9         printf("n=%d,ans=%d%d\n\n",n,ans);
10     }
11     return 0;
12 }
13 int power(int x,int n){
14     int t;
15     if(n==0)
16     return 1;
17     else if(n%2)
18     return x*power(x,n-1);
19     else{
20         t=power(x,n/2);
21         return t*t;
22     }
23 }
24 实验7:#include<stdio.h>
25 #include<stdlib.h>
26 int print_char man(int n);
27 int main(){
28     it n;
29     printf("Enter n:");
30     scanf("%d",&n);
31     print_charman(n);
32     return 0;
33 }
34 int printf_charman(int n)
35 {
36     int i=0;
37     for(;i<n;i++)
38     {
39         printf(" O");
40         printf("<H>");
41         printf("I I");
42     }
43    
44 }

 

运算结果:

问题:函数用于计算一个数的n次方

实验四:

源代码:

 1 #include <stdio.h>
 2 #include <math.h>
 3 int is_prime(int n);
 4 int main() {
 5     int count = 0; 
 6     printf("100以内的孪生素数:\n");
 7     for (int i = 2; i <= 98; i++) { 
 8         if (is_prime(i) && is_prime(i + 2)) { 
 9             printf("%d %d\n", i, i + 2);
10             count++; 
11         }
12     }
13     printf("100以内的孪生素数共有%d个\n", count);
14     return 0;
15 }
16 int is_prime(int n) {
17     if (n <= 1) return 0; 
18     if (n <= 3) return 1; 
19     if (n % 2 == 0 || n % 3 == 0) return 0; 
20     for (int i = 5; i <= sqrt(n); i += 6) { 
21         if (n % i == 0 || n % (i + 2) == 0) return 0;
22     }
23     return 1;
24 }

运算结果:

实验五:

源代码:

 1 #include <stdio.h>
 2 void hanoi(int n, char from_rod, char to_rod, char aux_rod);
 3 int main() {
 4     int n;
 5     while (scanf("%d", &n) != EOF) { 
 6         printf("%d\n", n);
 7         int moves = 0; 
 8         hanoi(n, 'A', 'C', 'B');
 9         printf("一共移动了%d次.\n", (1 << n) - 1);
10     }
11     return 0;
12 }
13 void hanoi(int n, char from_rod, char to_rod, char aux_rod) {
14     if (n == 1) {
15         printf("1:%c --> %c\n", from_rod, to_rod);
16         return;
17     }
18     hanoi(n - 1, from_rod, aux_rod, to_rod); 
19     printf("1:%c --> %c\n", from_rod, to_rod); 
20     hanoi(n - 1, aux_rod, to_rod, from_rod); 
21 }

运行结果:

实验六:

源代码:

 1 #include <stdio.h>
 2 int func_iterative(int n, int m); 
 3 int func_recursive(int n, int m); 
 4 
 5 int main() {
 6     int n, m;
 7     int ans;
 8     while (scanf("%d %d", &n, &m) != EOF) {
 9         ans = func_iterative(n, m);
10         printf("n=%d, m=%d, ans=%d\n\n", n, m, ans);
11     }
12     return 0;
13 }
14 int func_iterative(int n, int m) {
15     if (m > n) return 0; 
16     int result = 1;
17     for (int i = 1; i <= m; ++i) {
18         result *= (n - i + 1); 
19         result /= i;
20     }
21     return result;
22 }
23 int func_recursive(int n, int m) {
24     if (m == 0 || m == n) return 1; 
25     if (m > n) return 0; 
26     return func_recursive(n - 1, m - 1) + func_recursive(n - 1, m); 
27 }

 

运行结果:

实验七:

源代码:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 char print_charman(int n);
 4 int main() {
 5   int n;
 6   printf("Enter n: ");
 7    scanf("%d", &n);
 8     print_charman(n); 
 9     return 0;
10  }
11 char print_charman(int n) 
12     {
13         int t = 0;
14         for (int i = n; i >= 1; i--)
15         {
16             for (int j = 0; j < t; j++)
17                 printf("\t");
18             for (int j = 0; j < 2 * i - 1; j++)
19                 printf(" o\t");
20             printf("\n");
21             for (int j = 0; j < t; j++)
22                 printf("\t");
23            for (int j = 0; j < 2 * i - 1; j++)
24                 printf("<H>\t");
25            printf("\n");             for (int j = 0; j < t; j++)
26                 printf("\t");
27             for (int j = 0; j < 2 * i - 1; j++)
28                 printf("I I\t");
29             printf("\n");
30             t++;
31          }
32     }

 

运行结果:

 

标签:return,int,printf,rod,实验,ans,include
From: https://www.cnblogs.com/1970779615zmh/p/18513208

相关文章

  • 《DNK210使用指南 -CanMV版 V1.0》第三十四章 image图像滤波实验
    第三十四章image图像滤波实验1)实验平台:正点原子DNK210开发板2)章节摘自【正点原子】DNK210使用指南-CanMV版V1.03)购买链接:https://detail.tmall.com/item.htm?&id=7828013987504)全套实验源码+手册+视频下载地址:http://www.openedv.com/docs/boards/k210/ATK-DNK210.html5)......
  • 实验3(2.0)
    任务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,g......
  • 实验3
    任务一#include<stdio.h>charscore_to_grade(intscore);//函数声明intmain(){intscore;chargrade;while(scanf("%d",&score)!=EOF){grade=score_to_grade(score);//函数调用printf("分数:%d,等级:%......
  • 实验3
    任务11#include<stdio.h>23charscore_to_grade(intscore);//函数声明45intmain(){6intscore;7chargrade;89while(scanf("%d",&score)!=EOF){10grade=score_to_grade(score);//函数调用11......
  • HNU-操作系统实验lab6-2022级
    实验目的任务调度是操作系统的核心功能之一。UniProton实现的是一个单进程支持多线程的操作系统。在UniProton中,一个任务表示一个线程。UniProton中的任务为抢占式调度机制,而非时间片轮转调度方式。高优先级的任务可打断低优先级任务,低优先级任务必须在高优先级任务挂起或......
  • 实验3
    task1#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",s......
  • 实验2 类与对象
    实验任务一t.h1//类T:声明2classT{3//对象属性、方法4public:5T(intx=0,inty=0);//普通构造函数6T(constT&t);//复制构造函数7T(T&&t);//移动构造函数8~T();//析构函数910voidadj......
  • 实验3
    任务1:源代码:1#include<stdio.h>2charscore_to_grade(intscore);3intmain(){4intscore;5chargrade;67while(scanf("%d",&score)!=EOF){8grade=score_to_grade(score);9printf("分数:%d......
  • 实验3
    任务11#include<stdio.h>2charscore_to_grade(intscore);3intmain()4{5intscore;6chargrade;7while(scanf("%d",&score)!=EOF){8grade=score_to_grade(score);9printf("分数:%d,等级;%c\n\n&quo......
  • 实验3
    task1源代码:#include<stdio.h>charscore_to_grade(intscore);//函数声明intmain(){intscore;chargrade;while(scanf("%d",&score)!=EOF){grade=score_to_grade(score);//函数调用printf("分数:%d,......