#include <stdio.h> #include <stdlib.h> #include <time.h> #include <windows.h> #define N 80 void print_text(int line, int col, char text[]); // 函数声明 void print_spaces(int n); // 函数声明 void print_blank_lines(int n); // 函数声明 int main() { int line, col, i; char text[N] = "hi, November~"; srand(time(0)); // 以当前系统时间作为随机种子 for(i = 1; i <= 10; ++i) { line = rand() % 25; col = rand() % 80; print_text(line, col, text); Sleep(1000); // 暂停1000ms } return 0; } // 打印n个空格 void print_spaces(int n) { int i; for(i = 1; i <= n; ++i) printf(" "); } // 打印n行空白行 void print_blank_lines(int n) { int i; for(i = 1; i <= n; ++i) printf("\n"); } // 在第line行第col列打印一段文本 void print_text(int line, int col, char text[]) { print_blank_lines(line-1); // 打印(line-1)行空行 print_spaces(col-1); // 打印(col-1)列空格 printf("%s", text); // 在第line行、col列输出text中字符串 }
在屏幕上打出一串字符串“hi,November”并在随机行,随机列生成,每次生成的时间相隔一秒。执行n次后自动弹出。
任务2
#include <stdio.h> #include<stdlib.h> long long fac(int n); // 函数声明 int main() { int i, n; printf("Enter n: "); scanf("%d", &n); for (i = 1; i <= n; ++i) printf("%d! = %lld\n", i, fac(i)); return 0; } // 函数定义 long long fac(int n) { static long long p = 1; p = p * n; system("pause"); return p; }
实验2_2
理论: 8,17.
#include<stdio.h> #include<stdlib.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); system("pause"); return 0; } int func(int a, int b) { static int m = 0, i = 2; i += m + 1;/*p1ʱ3 p2ʱ12*/ m = i + a + b;/*p1ʱ8 p2ʱ17*/ return m; }
任务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 return 0; 14 } 15 long long func(int n){ 16 if(n==0) 17 return 0; 18 else 19 return func(n-1)*2+1; 20 }
运行结果
任务4
源代码
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 /*int func(int n,int m){ //递归法 13 if(n<m) 14 return 0; 15 if(m==0||n==0) 16 return 1; 17 else 18 return func(n-1,m)+func(n-1,m-1); 19 20 }*/ 21 int func(int n,int m){ //迭代法 22 int i,a=1,b=1,c=1,s=0; 23 if(n<m) 24 return 0; 25 else if(m==0||n==0) 26 return 1; 27 else{ 28 for(i=1;i<=n;i++) 29 a*=i; 30 for(i=1;i<=m;i++) 31 b*=i; 32 for(i=1;i<=n-m;i++) 33 c*=i; 34 s=a/b/c; 35 return s;} 36 37 }
运行结果
任务5
源代码
1 #include<stdio.h> 2 void nannoi(unsigned int n,char from,char temp,char to); 3 void print(unsigned int n,char from,char to); 4 int count(int n); 5 int main(){ 6 unsigned int n,t=0; 7 while(scanf("%d",&n)!=EOF){ 8 nannoi(n,'A','B','C'); 9 t=count(n); 10 printf("一共移动了%u次\n",t);} 11 return 0; 12 } 13 void nannoi(unsigned int n,char from,char temp,char to){ 14 if(n==1) 15 print(n,from,to); 16 else{ 17 nannoi(n-1,from,temp,to); 18 print(n,from,to); 19 nannoi(n-1,temp,to,from); 20 } 21 } 22 void print(unsigned int n,char from,char to){ 23 printf("%u:%c-->%c\n",n,from,to); 24 } 25 int count(int n){ 26 if(n==1) 27 return 1; 28 else 29 return 2*count(n-1)+1; 30 }
运行结果
任务六
源代码
1 #include<stdio.h> 2 #include<math.h> 3 long func(long s); 4 int main(){ 5 long s,t; 6 printf("Enter a number:"); 7 while(scanf("%ld",&s)!=EOF){ 8 t=func(s); 9 printf("new number is: %d\n\n",t); 10 printf("Enter a number: "); 11 } 12 13 return 0; 14 } 15 long func(long s){ 16 int x,t=0; 17 while(s!=0){ 18 x=s%10; 19 s/=10; 20 if(x%2!=0) 21 t=t*10+x; 22 } 23 while(t!=0){ 24 x=t%10; 25 t/=10; 26 s=s*10+x; 27 } 28 return s; 29 }
运行结果
标签:return,int,long,char,实验,func,include From: https://www.cnblogs.com/p2kp2k/p/17798727.html