task1
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 int line,col,i; 13 char text[N] = "hi,November"; 14 15 srand(time(0)); 16 17 for(i = 1;1 <= 10 ;++i) 18 { 19 line = rand() % 25; 20 col = rand() % 80; 21 print_text(line,col,text); 22 sleep(1000); 23 } 24 return 0; 25 26 27 } 28 29 void print_spaces(int n){ 30 int i; 31 32 for(i = 1;i <= n ; i++) 33 printf(" "); 34 35 36 } 37 38 void print_blank_lines(int n){ 39 int i; 40 41 for(i = 1;i <= n ; i++) 42 printf("\n"); 43 44 45 } 46 47 void print_text(int line,int col,char text[]){ 48 print_blank_lines(line-1); 49 print_spaces(col-1); 50 printf("%s",text); 51 52 53 }
1 结果
task 2
2 源代码
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 for(i = 1;i <=n ;++i) 11 printf("%d!=%lld\n",i,fac(i)); 12 system("pause"); 13 return 0; 14 15 } 16 17 long long fac(int n){ 18 static long long p = 1; 19 20 p = p*n; 21 22 return p; 23 24 }
2结果
task2_2
源代码
1 #include<stdio.h> 2 #include<stdlib.h> 3 int func(int ,int); 4 5 int main(){ 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 system("pause"); 12 return 0; 13 14 ; 15 } 16 int func(int a,int b){ 17 static int m = 0,i = 2; 18 19 i += m + i; 20 m = i + a + b; 21 22 return m; 23 24 25 }
结果
task3_3
源代码
1 #include<stdio.h> 2 long long func(int n); 3 4 int main(){ 5 int n; 6 long long f; 7 8 while(scanf("%d",&n)!=EOF){ 9 f = func(n); 10 printf("n = %d,f = %lld\n",n,f); 11 12 13 } 14 15 return 0; 16 } 17 long long func(int n){ 18 int i; 19 long long p = 1; 20 if(n==0) 21 p = 0; 22 else 23 p = func(n-1)*2+1; 24 return p; 25 26 }
结果
task 3_4
源代码
迭代
1 #include<stdio.h> 2 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 return 0; 11 } 12 int func(int n ,int m){ 13 int up=1 ,down=1; 14 int ans; 15 int i; 16 for(i=n-m+1;i<=n;i++) 17 up=up*i; 18 for(i=1;i<=m;i++) 19 down=down*i; 20 ans = up/down; 21 22 return ans; 23 24 }
结果
递归
1 #include<stdio.h> 2 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 return 0; 11 } 12 13 int func(int n, int m){ 14 int i; 15 int ans; 16 if(n==m) 17 ans=1; 18 else if(n<m) 19 ans = 0; 20 else if (m==0) 21 ans = 1; 22 23 24 else 25 ans=func(n-1,m)+func(n-1,m-1); 26 return ans; 27 }
task3_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 i =0; 6 int main(){ 7 unsigned int n; 8 9 10 while(scanf("%u",&n)!=EOF) 11 { 12 hanoi(n,'A','B','C'); 13 printf("一共移动了%d次",i); 14 i=0; 15 16 17 } 18 system("pause"); 19 return 0; 20 21 } 22 void hanoi(unsigned int n ,char from,char temp,char to) 23 { 24 25 if(n==1){ 26 moveplate(n,from,to); 27 28 } 29 else 30 { 31 hanoi(n-1,from,to,temp); 32 33 moveplate(n,from,to); 34 hanoi(n-1,temp,from,to); 35 36 } 37 } 38 void moveplate(unsigned int n,char from,char to) 39 { 40 41 printf("%u:%c-->%c\n",n,from,to); 42 i++; 43 }
task3_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 numberm is:%ld\n\n",t); 13 printf("Enter a number:"); 14 15 16 } 17 18 return 0; 19 } 20 long func(long s){ 21 int n,m; 22 long long k=0,d=0; 23 24 while(s>0){ 25 m=s%10; 26 if(m%2==0) 27 printf(" "); 28 else 29 k=k*10+m; 30 s=s/10; 31 32 } 33 while(k>0){ 34 n=k%10; 35 d=d*10+n; 36 k=k/10; 37 38 39 40 } 41 return d; 42 43 }
结果
标签:10,函数,int,编程,long,char,实验,func,include From: https://www.cnblogs.com/meng36/p/17798205.html