task_1
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 #include <windows.h> 5 #define N 80 6 void print_text(int line, int col, char text[]); // 函数声明 7 8 void print_spaces(int n); // 函数声明 9 10 void print_blank_lines(int n); // 函数声明 11 12 13 14 int main() { 15 int line, col, i; 16 char text[N] = "hi, November~"; 17 srand(time(0)); // 以当前系统时间作为随机种子 18 for(i = 1; i <= 10; ++i) { 19 line = rand() % 25; 20 col = rand() % 80; 21 print_text(line, col, text); 22 Sleep(1000); // 暂停1000ms 23 } 24 system("pause"); 25 return 0; 26 } 27 28 29 30 31 // 打印n个空格 32 void print_spaces(int n){ 33 int i; 34 for(i = 1; i <= n; ++i) 35 printf(" "); 36 } 37 38 39 40 // 打印n行空白行 41 void print_blank_lines(int n) { 42 int i; 43 for(i = 1; i <= n; ++i) 44 printf("\n"); 45 } 46 47 48 49 // 在第line行第col列打印一段文本 50 void print_text(int line, int col, char text[]) { 51 print_blank_lines(line-1); // 打印(line-1)行空行 52 print_spaces(col-1); // 打印(col-1)列空格 53 printf("%s", text); // 在第line行、col列输出text中字符串 54 }
利用函数嵌套,在第line行第col列 打印一段字符串
task2_1
1 // 利用局部static变量的特性,计算阶乘 2 #include <stdio.h> 3 #include<stdlib.h> 4 long long fac(int n); // 函数声明 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 long long fac(int n) { 19 static long long p = 1; 20 printf("%ld\n",p); 21 p = p * n; 22 return p; 23 }
task2_2
#include <stdio.h> int func(int, int); // 函数声明 int main() { int k = 4, m = 1, p1, p2; p1 = func(k, m); // 函数调用 p2 = func(k, m); // 函数调用 printf("%d, %d\n", p1, p2); return 0; } // 函数定义 int func(int a, int b) { static int m = 0, i = 2; i += m + 1; m = i + a + b; return m; }
内存中始终留有static变量的空间
task_3
#include <stdio.h> long long func(int n); // 函数声明 int main() { int n; long long f; while (scanf("%d", &n) != EOF) { f = func(n); // 函数调用 printf("n = %d, f = %lld\n", n, f); } return 0; } //2*(f(n-1)+1)-1=f(n) f(1)=1 f(0)=0 long long func(int n){ long long t; if(n==0) return 0; if(n==1) return 1; if(n>=2){ t=(func(n-1)+1)*2-1; return t; } }
task_4
#include<stdio.h> int func(int n, int m); int main() { int n, m; while(scanf("%d%d", &n, &m) != EOF){ printf("n = %d, m = %d, ans = %d\n", n, m, func(n, m)); } return 0; } int func(int n,int m){ int t; if(m==0) return 1; if(m==1) return n; if(m==n) return 1; if(n<m) return 0; if(m>=2){ t=func(n-1,m)+func(n-1,m-1); return t; } }
task_5
#include <stdio.h> int mul(int n, int m); int main() { int n, m; while(scanf("%d%d", &n, &m) != EOF) printf("%d * %d = %d\n", n, m, mul(n, m)); return 0; } //m*n=m*(n-1)+m int mul(int n,int m){ int t=0; if(n==1) return m; if(n>1){ t=mul(n-1,m)+m; return t; } }
task_6
1 #include<stdio.h> 2 #include<stdlib.h> 3 void hanoi(int n,char A,char B,char C); 4 void moveplate(int n,char A,char C); 5 6 int count=0; 7 8 int main() 9 { 10 int n; 11 printf("input number:"); 12 13 while(scanf("%d",&n)!=EOF){ 14 count=0; 15 hanoi(n,'A','B','C'); 16 17 printf("共移动了%d次\n",count);} 18 system("pause"); 19 return 0; 20 } 21 22 void hanoi(int n,char A,char B,char C) 23 { 24 25 if(n==1) 26 moveplate(n,A,C); 27 else 28 { 29 hanoi(n-1,A,C,B); //n-1个圆盘从A以C为中转移到B上 30 moveplate(n,A,C); //将第n个圆盘从A移到C上 31 hanoi(n-1,B,A,C); //将n-1个圆盘从B上以A为中转移到C上 32 } 33 } 34 35 void moveplate(int n,char A,char C) 36 { 37 extern int count; 38 39 count+=1; 40 printf("%u:%c-->%c\n",n,A,C); 41 }
task_7
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<math.h> 4 5 int is_prime(int n); 6 7 int main(){ 8 int n; 9 while(scanf("%d",&n)!=EOF) 10 { 11 if(n%2!=0) 12 { 13 printf("输入的数不是偶数,请重新输入:"); 14 continue; 15 } 16 17 else 18 { 19 int p=1; 20 int q; 21 22 q=n-p; 23 24 for(p=1;p<q;p++) 25 { 26 q=n-p; 27 if(is_prime(p)==1&&is_prime(q)==1) 28 { 29 printf("%d=%d+%d \n",n,p,q); 30 break; 31 } 32 else 33 { 34 continue; 35 } 36 } 37 } 38 39 } 40 system("pause"); 41 return 0; 42 } 43 44 int is_prime(int n){ 45 int t; 46 int x=sqrt(1.0*n); 47 if(n==1) return 0; 48 for(t=2;t<=x;t++) 49 { 50 if(n%t==0) break; 51 } 52 53 if(t>x&&n!=1) return 1; 54 else return 0; 55 }
task_8
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<math.h> 4 long fun(long s); 5 6 int main() { 7 long s, t; 8 9 printf("Enter a number: "); 10 11 while (scanf("%ld", &s) != EOF) 12 { 13 t = fun(s); // 函数调用 14 printf("new number is: %ld\n\n", t); 15 printf("Enter a number: "); 16 } 17 18 return 0; 19 } 20 21 long fun(long s) 22 { 23 int t; 24 long y=0; 25 int z,a; 26 int count=0; 27 while(1) 28 { 29 t=s%10; 30 s/=10; 31 32 if((t%2)!=0) 33 { 34 z=pow(10.0,count); 35 count+=1; 36 a=z*t; 37 y+=a; 38 } 39 40 if(s==0) break; 41 42 } 43 return y; 44 }
标签:return,int,long,实验,func,printf,include From: https://www.cnblogs.com/meijianfeng/p/16849784.html