1、实验1
实验1 运行结果
实现了每隔一秒随机弹出“hi,November~”。
2、实验2
实验2-1 源代码
1 #include<stdio.h> 2 long long fac(int n); 3 4 int main() 5 { 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 } 14 return 0; 15 } 16 long long fac(int n) 17 { 18 static long long p = 1; 19 20 p = p * n; 21 22 return p; 23 }
实验2-1 运行结果
实验2-2 源代码
1 #include<stdio.h> 2 int func(int,int); 3 4 int main() 5 { 6 int k = 4,m = 1,p1,p2; 7 8 p1 = func(k,m); 9 p2 = func(k,m); 10 printf("%d %d\n",p1,p2); 11 12 return 0; 13 } 14 15 int func(int a,int b) 16 { 17 static int m = 0,i = 2; 18 19 i += m+1; 20 m = i+a+b;; 21 return m; 22 }
实验2-2 运行结果
3、实验3
实验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 f = func(n); 10 printf("n = %d,f = %lld\n",n,f); 11 } 12 13 return 0; 14 } 15 long long func(int n) 16 { 17 long long x; 18 if(n == 0){ 19 x = 0; 20 } 21 else{ 22 x= (func(n-1)+1)*2-1; 23 } 24 25 return x; 26 }
实验3 运行结果
4、实验4
实验4 源代码(迭代)
1 int main() 2 { 3 int n,m; 4 while(scanf("%d%d",&n,&m)!= EOF) 5 printf("n = %d,m = %d, ans = %d\n",n,m,func(n,m)); 6 7 return 0; 8 } 9 int func(int n,int m) 10 { 11 int i,up=1,down=1,c; 12 if(n<m){ 13 c=0; 14 } 15 else{ 16 for(i=n;i>= n-m+1;--i){ 17 up = up*i; 18 } 19 for(i=1;i<=m;++i){ 20 down = down*i; 21 } 22 c= up/down; 23 } 24 return c; 25 }
实验4 运行结果
实验4 源代码(递归)
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 10 return 0; 11 } 12 int func(int n,int m) 13 { 14 int c; 15 if(n<m){ 16 return 0; 17 } 18 else if(m==0||n==1&&m==1){ 19 return 1; 20 } 21 else{ 22 c = func(n-1,m)+func(n-1,m-1); 23 return c; 24 } 25 }
实验4 运行结果
5、实验5
实验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 static int time=0; 6 int main() 7 { 8 unsigned int n; 9 while(scanf("%u",&n) != EOF){ 10 hanoi(n,'A','B','C'); 11 printf("一共移动了%d次\n",time); 12 time = 0;//将每次的time重置为0,所以必不可少 13 } 14 15 return 0; 16 } 17 void hanoi(unsigned int n,char from,char temp,char to) 18 { 19 if(n==1) 20 moveplate(n,from,to); 21 else 22 { 23 hanoi(n-1,from,to,temp); 24 moveplate(n,from,to); 25 hanoi(n-1,temp,from,to); 26 27 } 28 } 29 void moveplate(unsigned int n,char from,char to) 30 { 31 printf("%u:%c-->%c\n",n,from,to); 32 time++; 33 } 34
实验5 运行结果
6、实验6
实验6 源代码
1 #include<stdio.h> 2 #include<math.h> 3 long func(long s); 4 5 int main() 6 { 7 long s,t; 8 9 printf("Enter a number:"); 10 while(scanf("%ld",&s) != EOF){ 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 long a=0,b,i=0; 20 while(s != 0){ 21 b = s%10; 22 s = s/10; 23 if(b%2 != 0){ 24 a += b*pow(10,i); 25 ++i; 26 } 27 } 28 return a; 29 }
实验6 运行结果
标签:函数,int,编程,long,C语言,char,实验,func,printf From: https://www.cnblogs.com/liyushi0818/p/17798415.html