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, November~"; 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 }
运行截图
task2-1.c
static变量的特性:在·计算机中开辟一块特定的内存空间,储存的值一直保留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 15 return 0; 16 } 17 18 // 函数定义 19 long long fac(int n) { 20 static long long p = 1; 21 printf("p = %lld\n", p); 22 p = p * n; 23 24 return p; 25 }
运行截图
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 13 return 0; 14 } 15 16 // 函数定义 17 int func(int a, int b) { 18 static int m = 0, i = 2; 19 20 i += m + 1; 21 m = i + a + b; 22 23 return m; 24 } 25 //i=3m=8;p1=8;i=12,m=17,p2= 17
运行截图
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 13 return 0; 14 } 15 16 long long func(int n){ 17 int sum=1; 18 int i; 19 for(i=0;i<n;i++){ 20 sum*=2; 21 } 22 return sum-1; 23 }
运行截图
task4.c
1 #include <stdio.h> 2 int func(int n, int m); 3 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, func(n, m)); 9 10 return 0; 11 } 12 13 int func(int n, int m){ 14 if(n<m){ 15 return 0; 16 } 17 else if(m==1){ 18 return n; 19 } 20 else if(m==0){ 21 return 1; 22 } 23 else{ 24 return func(n-1,m)+func(n-1,m-1); 25 } 26 27 28 }
运行截图
task5.c
1 #include <stdio.h> 2 #include<stdlib.h> 3 void hanoi(int n,char a,char b,char c); 4 void move(int n,char a,char c); 5 int main() { 6 int n; 7 int cnt=1; 8 int i; 9 while(scanf("%d",&n)!=EOF){ 10 hanoi(n,'A','B','C'); 11 for(i=0;i<n;i++){ 12 cnt*=2 ; 13 } 14 printf("一共移动了%d次\n",cnt-1); 15 cnt=1; 16 } 17 return 0; 18 } 19 20 void hanoi(int n,char a,char b,char c){ 21 int cnt =0; 22 if(n==1){ 23 move(n,a,c); 24 }else{ 25 hanoi(n-1,a,c,b); 26 move(n,a,c); 27 hanoi(n-1,b,a,c); 28 } 29 } 30 void move(int n,char a,char c){ 31 printf("%d:%c-->%c\n",n,a,c); 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 16 return 0; 17 } 18 19 long func(long s){ 20 static long sum = 0; 21 while(s!=0){ 22 long a=s%10; 23 if(a%2!=0){ 24 sum=sum*10+a; 25 } 26 s/=10; 27 } 28 long t=0; 29 while(sum!=0){ 30 long b=sum%10; 31 t=t*10+b; 32 sum/=10; 33 } 34 return t; 35 }
运行截图
标签:10,12,int,long,实验,func,include From: https://www.cnblogs.com/homeh/p/17798367.html