首页 > 编程语言 >实验3_C语言函数应用编程

实验3_C语言函数应用编程

时间:2023-10-30 19:11:23浏览次数:31  
标签:10 函数 int 编程 long C语言 func printf include

1.task_1

 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 {
13     int line,col,i;
14     char text[N]="hi,November~";
15     srand (time(0));
16     for (i=1;i<=10;++i)
17     {
18         line=rand()%25;
19         col=rand()%80;
20         print_text(line,col,text);
21         Sleep(1000);
22     }
23     return 0;
24 }
25 
26 void print_spaces(int n)
27 {
28     int i;
29     for (i=1;i<=n;++i)
30         printf(" ");
31 }
32 
33 void print_blank_lines(int n)
34 {
35     int i;
36     for (i=1;i<=n;++i)
37         printf("\n");
38 }
39 
40 void print_text(int line,int col,char text[])
41 {
42     print_blank_lines(line-1);
43     print_spaces(col-1);
44     printf("%s",text);
45 }

答:下“hi,November~”雨,总计10个

 

2.task_2

  (1)

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

(2)

 1 #include <stdio.h>
 2 int func(int,int);
 3 
 4 int main()
 5 {
 6     int k=4,m=1,p1,p2;
 7     p1=func(k,m);
 8     p2=func(k,m);
 9     printf("%d,%d\n",p1,p2);
10     return 0;
11 }
12 
13 int func(int a,int b)
14 {
15     static int m=0,i=2;
16     i+=m+1;
17     m=i+a+b;
18     return m;
19 }

答:与分析一致。static定义答变量不随计算而改变

 

3.task_3

 1 #include <stdio.h>
 2 long long func(int n);
 3 
 4 int main()
 5 {
 6     int n;
 7     long long f;
 8     while (scanf("%d",&n)!=EOF)
 9     {
10         f=func(n);
11         printf("n=%d,f=%lld\n",n,f);
12     }
13     return 0;
14 }
15 
16 long long func(int n)
17 {
18     long long int f=1;
19     int i;
20     for (i=1;i<=n;i++)
21         f=f*2;
22     f=f-1;
23     return f;
24 }

 

4.task_4

(1)

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

(2)

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

 

5.rask_5

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 void hanoi(unsigned int n,char from,char temp,char to);
 4 void moveplate(unsigned int n,char from,char to);
 5 int count;
 6 
 7 int main()
 8 {
 9     unsigned int n;
10     while(scanf("%u",&n)!=EOF)
11     {
12         count=0;
13         hanoi(n,'A','B','C');
14         printf("\n");
15         printf("一共移动了%d次.\n",count);
16         printf("\n");
17     }
18     return 0;
19 }
20 
21 void hanoi(unsigned int n,char from,char temp,char to)
22 {
23     if (n==1)
24        moveplate(n,from,to);
25     else
26     {
27         hanoi(n-1,from,to,temp);
28         moveplate(n,from,to);
29         hanoi(n-1,temp,from,to);
30     }
31 }
32 
33 void moveplate(unsigned int n,char from,char to)
34 {
35     printf("%u:%c-->%c\n",n,from,to);
36     count++;
37 }

 

6.task_6

 1 #include <stdio.h>
 2 #include <math.h>
 3 long func(long s);
 4 
 5 int main()
 6 {
 7     long s,t;
 8     printf("Enter a number:");
 9     while (scanf("%ld",&s)!=EOF)
10     {
11         t=func(s);
12         printf("new number is:%ld\n\n",t);
13         printf("Enter a number:"); 
14     }
15     return 0;
16 }
17 
18 long func(long s)
19 {
20     long k=0,m,t=0,n;
21     do
22     {
23         m=s%10;
24         if (m%2==1)
25         {
26             k=k*10+m;
27         }
28         s=s/10;
29     }while (s!=0);
30     do
31     {
32         n=k%10;
33         t=t*10+n;
34         k=k/10;
35     }while (k!=0);
36     return t;
37 }

 

标签:10,函数,int,编程,long,C语言,func,printf,include
From: https://www.cnblogs.com/zxy2004/p/17797394.html

相关文章

  • 加餐-TensorFlow编程最佳实践
    0.参考文档TensorFlow官方文档eat_tensorflow2_in_30_days机器学习实战:基于Scikit-Learn和TensorFlow1.TensorFlow的几个核心概念张量和计算图TensorFlow->张量流动程序=数据结构+算法->TensorFlow程序=张量数据结构+计算图算法语言三种计算图静......
  • 无涯教程-C语言 - 数组(Array)
    数组是一种数据结构,可以存储相同类型的元素的固定大小的顺序集合。所有数组均包含连续的内存位置,最低地址对应于第一个元素,最高地址对应于最后一个元素。声明数组要在C中声明数组,程序员可以指定元素的类型和数组所需的元素数量,如下所示-typearrayName[arraySize];这称为......
  • vue关于render函数如何渲染v-html
    render函数如何渲染v-html,两种方式(适用于搜索关键字高亮) 例子:“互联网数据”搜索关键字"网"==>row.htmlStr:互联<spanclass="search-text">网</span>数据render:(h,{row})=>{//模板组件方式returnh({template:"<span>"+row.htmlStr+"&l......
  • 29win32编程基础——线程控制
    suspendThred挂起线程ResumeThread恢复线程结束线程1、ExitThread2、线程函数返回,即线程正常结束,正常结束3、线程强制结束TerminateThread,告诉操作系统要结束线程WaitForSingleObject TerminateThread和ExitThread区别是:1、TerminateThread是异步调用,用于强制终止线程;E......
  • 实验3 C语言函数应用编程
    实验任务1源代码task1.c1#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_blank_lines(......
  • go 函数
    函数:声明: func函数名字(参数列表)(返回值列表){}举例: funcadd(){} funcadd(aint,bint)int{} funcadd(aint,bint)(int,int){}特点: a.不支持重载,一个包不能有两个名字一样的函数. b.函数也是一种类型,一个函数可以赋值给变量(这个变量是......
  • 深入理解多线程编程和 JVM 内存模型
    文章目录1.理解进程和线程的概念进程(Process)线程(Thread)2.理解竞态条件和死锁竞态条件(RaceCondition)死锁(Deadlock)3.JVM内存模型堆(Heap)栈(Stack)方法区(MethodArea)本地方法栈(NativeMethodStack)PC寄存器(ProgramCounterRegister)垃圾回收4.常见的多线程编程模式生产者-消费者......
  • 适用批处理的softmax函数的输入矩阵为什么要转置
    #适用批输入的softmax函数defSoftmax(x):ifx.ndim==2:x=x.Tx=x-np.max(x,axis=0)y=np.exp(x)/np.sum(np.exp(x),axis=0)returny.T#为什么要转置原因是Numpy数组的扩张原理是把行向量复制扩张成矩阵例子如下......
  • Linux21--shell编程基础
    1bashshell介绍#介绍shell是一门解释型、弱类型、动态语言#类比shell语法<===>python语法bash解释器<===>python解释器平台<===>平台2变量2.1基本使用###交互式环境中[root@localhost~]#x=1#1定义变量中间不能有空......
  • 使用c#在指定的时间内等待函数执行结果
    在指定的时间内等待某个函数的执行结果的方法。该方法接受三个参数:等待的最大时间、等待的频率和要等待的函数。方法会在指定的时间内每隔一定频率检查函数的执行结果,如果在等待时间内函数返回了true,则表示等待成功,返回true;如果超过等待时间仍未返回true,则表示等待失败,返回false。......