task1.c
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 }
功能是每隔1s在命令行随机位置打印一串指定文字
task2_1.c
1 // 利用局部static变量的特性,计算阶乘 2 3 #include <stdio.h> 4 long long fac(int n); // 函数声明 5 6 int main() { 7 int i, n; 8 9 printf("Enter n: "); 10 scanf("%d", &n); 11 12 for (i = 1; i <= n; ++i) 13 printf("%d! = %lld\n", i, fac(i)); 14 return 0; 15 } 16 17 // 函数定义 18 long long fac(int n) { 19 static long long p = 1; 20 printf("p = %lld\n", p); 21 p = p * n; 22 23 return p; 24 }
task2_2.c
1 // 练习:局部static变量特性 2 3 #include <stdio.h> 4 int func(int, int); // 函数声明 5 6 int main() { 7 int k = 4, m = 1, p1, p2; 8 9 p1 = func(k, m); // 函数调用 10 p2 = func(k, m); // 函数调用 11 printf("%d, %d\n", p1, p2); 12 return 0; 13 } 14 15 // 函数定义 16 int func(int a, int b) { 17 static int m = 0, i = 2; 18 19 i += m + 1; 20 m = i + a + b; 21 22 return m; 23 }
局部static变量只在第一次被调用时进行初始化
task3.c
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 return 0; 13 } 14 15 // 函数定义 16 long long func(int n){ 17 if(n==0) 18 { 19 return 0; 20 } 21 else 22 { 23 return 2*func(n-1)+1; 24 } 25 }
task4_1.c
1 #include <stdio.h> 2 int func1(int n, int m); 3 int func2(int n, int m); 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, func1(n, m)); 9 return 0; 10 } 11 12 // 函数定义 13 int func1(int n, int m){ 14 int i,result=1; 15 if(m==n || m==0) 16 { 17 return 1; 18 } 19 for(i=1;i<=m;i++) 20 { 21 result*=(n-i+1)/i; 22 } 23 return result; 24 } 25 26 int func2(int n, int m){ 27 if(m==n || m==0) 28 { 29 return 1; 30 } 31 else if(m>n) 32 { 33 return 0; 34 } 35 else 36 { 37 return func2(n-1,m)+func2(n-1,m-1); 38 } 39 }
task4_2.c
1 #include <stdio.h> 2 int func1(int n, int m); 3 int func2(int n, int m); 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, func2(n, m)); 9 return 0; 10 } 11 12 // 函数定义 13 int func1(int n, int m){ 14 int i,result=1; 15 if(m==n || m==0) 16 { 17 return 1; 18 } 19 for(i=1;i<=m;i++) 20 { 21 result*=(n-i+1)/i; 22 } 23 return result; 24 } 25 26 int func2(int n, int m){ 27 if(m==n || m==0) 28 { 29 return 1; 30 } 31 else if(m>n) 32 { 33 return 0; 34 } 35 else 36 { 37 return func2(n-1,m)+func2(n-1,m-1); 38 } 39 }
task5.c
1 #include <stdio.h> 2 3 void hanoi(unsigned int n, char from, char temp, char to, int* m); 4 void moveplate(unsigned int n, char from, char to, int* m); 5 int main() 6 { 7 unsigned int n; 8 while (scanf_s("%u", &n) != EOF) 9 { 10 int m = 0; 11 hanoi(n, 'A', 'B', 'C', &m); 12 printf("\n一共移动了%d次.\n\n", m); 13 } 14 return 0; 15 } 16 17 void hanoi(unsigned int n, char from, char temp, char to, int* m) 18 { 19 if (n == 1) 20 moveplate(n, from, to, m); 21 else 22 { 23 hanoi(n - 1, from, to, temp, m); 24 moveplate(n, from, to, m); 25 hanoi(n - 1, temp, from, to, m); 26 } 27 } 28 void moveplate(unsigned int n, char from, char to, int* m) 29 { 30 printf("%u:%c --> %c\n", n, from, to); 31 ++(*m); 32 }
task6.c
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 // 函数定义 19 long func(long s){ 20 int ans=0,t=1,i; 21 while((s) != 0) 22 { 23 if((s%10)%2) 24 { 25 ans+=t*(s%10); 26 t*=10; 27 } 28 s/=10; 29 } 30 return ans; 31 }
标签:10,return,函数,int,编程,long,C语言,char,include From: https://www.cnblogs.com/1262438226qqcom/p/18150501