task1.c源代码
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 void print_spaces(int n); // 函数声明 8 void print_blank_lines(int n); // 函数声明 9 int main() { 10 int line, col, i; 11 char text[N] = "hi, November~"; 12 srand(time(0)); // 以当前系统时间作为随机种子 13 for (i = 1; i <= 10; ++i) { 14 line = rand() % 25; 15 col = rand() % 80; 16 print_text(line, col, text); 17 Sleep(1000); // 暂停1000ms 18 } 19 return 0; 20 } 21 // 打印n个空格 22 void print_spaces(int n) { 23 int i; 24 for (i = 1; i <= n; ++i) 25 printf(" "); 26 } 27 // 打印n行空白行 28 void print_blank_lines(int n) { 29 int i; 30 for (i = 1; i <= n; ++i) 31 printf("\n"); 32 } 33 // 在第line行第col列打印一段文本 34 void print_text(int line, int col, char text[]) { 35 print_blank_lines(line - 1); // 打印(line-1)行空行 36 print_spaces(col - 1); // 打印(col-1)列空格 37 printf("%s", text); // 在第line行、col列输出text中字符串 38 }
运行截图
目的:每隔一秒随机输出一个hi,Novmber~
task2_1.c源代码
1 // 利用局部static变量的特性,计算阶乘 2 #define _CRT_SECURE_NO_WARNINGS 3 #include <stdio.h> 4 long long fac(int n); // 函数声明 5 int main() { 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 static long long p = 1; 16 printf("p = %lld\n", p); 17 p = p * n; 18 return p; 19 }
运行结果
task2_2.c源代码
// 练习:局部static变量特性 #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; }
运行截图
task3.c源代码
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 long long func(int n); // 函数声明 4 int main() { 5 int n; 6 long long f; 7 while (scanf("%d", &n) != EOF) { 8 f = func(n); // 函数调用 9 printf("n = %d, f = %lld\n", n, f); 10 } 11 return 0; 12 } 13 long long func(int n) 14 { 15 long long a = 1; 16 for (int i = 1; i <= n; ++i) 17 { 18 a *= 2; 19 } 20 return a - 1; 21 }
运行截图
task4_1.c源代码
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 int func(int n, int m); 4 int main() { 5 int n, m; 6 while (scanf("%d%d", &n, &m) != EOF) 7 printf("n = %d, m = %d, ans = %d\n", n, m, func(n, m)); 8 return 0; 9 } 10 int func(int n, int m) 11 { 12 int a = 1, b = 1, c = 1,sum; 13 if (n >= m) { 14 for (int i = 1; i <= n; ++i) 15 { 16 a *= i; 17 } 18 for (int i = 1; i <= m; ++i) 19 { 20 b *= i; 21 } 22 for (int i = 1; i <= n-m; ++i) 23 { 24 c *= i; 25 } 26 sum = a / b / c; 27 return sum; 28 } 29 else 30 return 0; 31 }
运行结果
task4_2.c源代码
#define _CRT_SECURE_NO_WARNINGS #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) { if (m > n) return 0; else if ( n==m||m == 0) return 1; else return func(n - 1, m) + func(n - 1, m - 1); }
运行结果
task5.c源代码
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> void hanoi(unsigned int n, char from, char temp, char to); void move(unsigned int n, char from, char to); int a; int main() { unsigned int n; while (scanf("%d", &n) != EOF) { hanoi(n, 'A', 'B', 'C'); printf("共移动了%d次\n", a); a = 0; } return 0; } void hanoi(unsigned int n, char from, char temp, char to) { a++; if (n == 1) move(n, from, to); else { hanoi(n - 1, from, to, temp); move(n, from, to); hanoi(n - 1, temp, from, to); } } void move(unsigned int n, char from, char to) { printf("%u:%c-->%c\n", n, from, to); }
运行截图
task6,c源代码
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include <math.h> 4 long func(long s); // 函数声明 5 int main() { 6 long s, t; 7 printf("Enter a number: "); 8 while (scanf("%ld", &s) != EOF) { 9 t = func(s); // 函数调用 10 printf("new number is: %ld\n\n", t); 11 printf("Enter a number: "); 12 } 13 return 0; 14 } 15 long func(long s) 16 { 17 int i, k, d = 1, sum = 0; 18 int a[10]; 19 int b[10]; 20 for (i = 0, k = 0; i < 10; i++) 21 { 22 a[i] = s % 10; 23 if (a[i] % 2 != 0) 24 { 25 b[k] = a[i]; 26 k++; 27 } 28 s /= 10; 29 } 30 for (i = 0; i < k; i++) 31 { 32 sum += b[i] * d; 33 d *= 10; 34 } 35 return sum; 36 }
运行结果
标签:return,int,long,char,实验,func,include From: https://www.cnblogs.com/ctl050312/p/17798387.html