首页 > 其他分享 >实验3

实验3

时间:2024-04-22 23:00:11浏览次数:21  
标签:return int long 实验 func printf include

task1

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 #include <windows.h>
 5 #define N 80
 6 
 7 void print_text(int line, int col, char text[]);  // 函数声明 
 8 void print_spaces(int n);  // 函数声明 
 9 void print_blank_lines(int n); // 函数声明 
10 
11 int main() {
12     int line, col, i;
13     char text[N] = "hi, April~";
14     
15     srand(time(0)); // 以当前系统时间作为随机种子
16     
17     for(i = 1; i <= 10; ++i) {
18         line = rand() % 25;
19         col =  rand() % 80;
20         print_text(line, col, text);
21         Sleep(1000);  // 暂停1000ms
22     }
23     
24     return 0; 
25 }
26 
27 // 打印n个空格 
28 void print_spaces(int n) {
29     int i;
30     
31     for(i = 1; i <= n; ++i)
32         printf(" ");
33 }
34 
35 // 打印n行空白行
36 void print_blank_lines(int n) {
37     int i;
38     
39     for(i = 1; i <= n; ++i)
40         printf("\n");
41  } 
42 
43 // 在第line行第col列打印一段文本 
44 void print_text(int line, int col, char text[]) {
45     print_blank_lines(line-1);      // 打印(line-1)行空行 
46     print_spaces(col-1);            // 打印(col-1)列空格
47     printf("%s", text);         // 在第line行、col列输出text中字符串
48 }

程序每隔一秒打印hi,April~,27、35行下面的函数分别控制空格数和空行数

task2

 1 #include <stdio.h>
 2 #include<stdlib.h>
 3 long long fac(int n); // 函数声明
 4 
 5 int main() {
 6     int i, n;
 7 
 8     printf("Enter n: ");
 9     scanf("%d", &n);
10 
11     for (i = 1; i <= n; ++i)
12         printf("%d! = %lld\n", i, fac(i));
13     system("pause");
14     return 0;
15     
16 }
17 
18 // 函数定义
19 long long fac(int n) {
20     static long long p = 1;
21     p = p * n;
22 
23     return p;
24     
25 }

 1 #include <stdio.h>
 2 #include<stdlib.h>
 3 long long fac(int n); // 函数声明
 4 
 5 int main() {
 6     int i, n;
 7 
 8     printf("Enter n: ");
 9     scanf("%d", &n);
10 
11     for (i = 1; i <= n; ++i)
12         printf("%d! = %lld\n", i, fac(i));
13     system("pause");
14     return 0;
15     
16 }
17 
18 // 函数定义
19 long long fac(int n) {
20     static long long p = 1;
21     printf("p=%11d\n",p);
22     p = p * n;
23 
24     return p;
25     
26 }

即使函数执行完毕后,局部static变量值也会保存下来

task3

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 long long func(int n); // 函数声明
 4 
 5 int main() {
 6     int n;
 7     long long f;
 8 
 9     while (scanf("%d", &n) != EOF) {
10         f = func(n); // 函数调用
11         printf("n = %d, f = %lld\n", n, f);
12     }
13 
14     system("pause");
15     return 0;
16 }
17 long long func(int n)
18 {
19     if(n==0)
20     {
21         return 0;
22     }
23     else
24     {
25         return 2*func(n-1)+1;
26     }
27 }

task4

 1 #include <stdio.h>
 2 #include<stdlib.h>
 3 int func(int n, int m);
 4 
 5 int main() {
 6     int n, m;
 7 
 8     while(scanf("%d%d", &n, &m) != EOF)
 9         printf("n = %d, m = %d, ans = %d\n", n, m, func(n, m));
10     system("pause");
11     return 0;
12 }
13 
14 // 函数定义
15 int func(int n,int m)
16 {
17     int s=1;
18     int i;
19     int k=1;
20     if(m>n)    
21     {
22         return 0;
23     }
24     else if(m==0||m==n)
25     {
26         return 1;
27     }
28     else
29     {
30           for(i=1;i<=m;++i)
31       {
32 
33           s=s*(n-i+1);
34          k*=i;
35       }
36           
37           return s/k;
38     }
39     
40 }

递归

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

task5

 1 #include <stdio.h>
 2 
 3 
 4 int hanoi(int n, char from, char to, char temp) {
 5     if (n == 0) {
 6         return 0; 
 7     }
 8     int moves1 = hanoi(n - 1, from, temp, to);
 9     printf("%d : %c --> %c\n", n, from, to);
10     moves1++; 
11     int moves2 = hanoi(n - 1, temp, to, from);
12     return moves1 + moves2;
13 }
14 
15 int main() {
16     int n, move;
17     printf("Enter the number of disks: ");
18     while(scanf("%d", &n) != EOF)
19     {
20     move= hanoi(n, 'A', 'C', 'B'); 
21     printf("一共移动了%d次\n", move); 
22     }
23     return 0;
24 }

task6

#include <stdio.h>
#include <math.h>
#include<stdlib.h>
long func(long s);   // 函数声明

int main()
{

    long s, t;

    printf("Enter a number: ");
    while (scanf("%ld", &s) != EOF) {
        t = func(s); // 函数调用
        printf("new number is: %ld\n\n", t);
        printf("Enter a number: ");
    }
    system("pause");
    return 0;
}

// 函数定义
long func(long s)
{
    long ans;
    long t,k;
    ans=0;
    t=1;
    while(s!=0)
    {
        k=s%10;
        if(k%2!=0)
        {
            ans+=t*k;
            t*=10;
        }
        s/=10;
    }
    return ans;
}

 

   

标签:return,int,long,实验,func,printf,include
From: https://www.cnblogs.com/hxt1/p/18151778

相关文章

  • 实验3 C语言函数应用编程
    //task1.c#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);//函数声明voidprint_spaces(intn);//函数声明voidprint_blank_lines(intn);//函数声明in......
  • 实验三
    点击查看代码#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);//函数声明voidprint_spaces(intn);//函数声明voidprint_blank_lines(intn);//函数声......
  • 实验
    1:在离线的环境中导入镜像在无法访问外网的情况下,通过将docker镜像导出为一个包,然后导入到另外的一台计算机上面,从而实现了不用访问外网就能拉取镜像了#将镜像输出到这个tar包[root@cleint~]#dockersave-ocentos.tarcentos#通过第三方的工具将这个tar包传输到其他机器......
  • 今天又是写前端的一天--web实验二
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>LoginPage</title><......
  • 实验3 C语言函数应用编程
    实验任务11#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#include<windows.h>5#defineN8067voidprint_text(intline,intcol,chartext[]);//函数声明8voidprint_spaces(intn);//函数声明9voidprint......
  • 实验5 循环结构程序设计(while、do-while语句的应用)
    c语言程序设计——实验报告五实验项目名称:实验项目类型:验证性实验日期:一、实验目的二、实验硬、软件环境Windows计算机、Devc6.0三、实验内容及步骤实验内容:编写程序:(1)用while语句计算自然数列1,2,3……,n的和,n的值在程序执行时输入。(2)用do-while语句计算n的阶乘(3)......
  • pta实验总结
    刚开始接触java,感到有些吃力,可能是因为C语言基础不牢的缘故,我的Java水平还有待提高。pta1:7-1设计一个风扇Fan类分数4入门作者殷伟凤单位浙江传媒学院设计一个名为Fan的类表示一个风扇。这个类包括:1.三个名为SlOW、MEDIUM和FAST,其值为1、2和3常量表示风扇的速度。2.一......
  • c语言程序设计——实验报告六
    c语言程序设计——实验报告六实验项目名称:实验项目类型:验证性实验日期:一、实验目的熟练掌握三种循环语句并能正确运用;能够用循环实现一些常用算法,如穷举法,迭代法,递推法等;进一步学习程序调试;了解中国算法,百钱买百鸡。二、实验硬、软件环境Windows计算机、Devc6.0......
  • 实验1-----keep健身运动APP
    一、对比分析墨刀、Axure、Mockplus等原型设计工具的各自的适用领域及优缺点。Axure优点:1.有强大的编辑功能,使得制作素材库会更便捷一点。2.更快的复制粘贴,素材库和原型库会多一些。3.可以项目共享,使得同事间可以同步工作,并保留所有工作历史,并可以随时到处历史版本的项目文......
  • 第八周实验
    与2252309合作结对编程四则混合运算#include<stdio.h>#include<stdlib.h>#include<time.h>intscore=0;intadd(inta,intb){ intc=a+b; if(c<1000)returnc; elsereturn-1;}intsubstract(inta,intb){ intc=a-b; if(c>=0&&......