实验三

test1.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 return 0;
24 
25 } // 打印n个空格
26 
27 void print_spaces(int n) {
28 int i;
29 
30 for(i = 1; i <= n; ++i)
31 printf(" ");
32 }
33 
34  // 打印n行空白行
35 void print_blank_lines(int n) {
36 int i;
37 for(i = 1; i <= n; ++i)
38 printf("\n");
39 }
40 
41 // 在第line行第col列打印一段文本
42 void print_text(int line, int col, char text[]) {
43 print_blank_lines(line-1); // 打印(line-1)行空行
44 print_spaces(col-1); // 打印(col-1)列空格
45 printf("%s", text); // 在第line行、col列输出text中字符串
46 }
View Code

功能为每隔一秒在下面第随机行随机列打出字符串

test2-1.c

 1 #include <stdio.h>
 2 long long fac(int n); // 函数声明
 3 
 4 int main() {
 5     int i, n;
 6 
 7     printf("Enter n: ");
 8     scanf("%d", &n);
 9 
10     for (i = 1; i <= n; ++i)
11         printf("%d! = %lld\n", i, fac(i));
12 
13     return 0;
14 }
15 
16 // 函数定义
17 long long fac(int n) {
18     static long long p = 1;
19 
20     p = p * n;
21 printf("p = %lld\n", p);
22     return p;
23 }
View Code

test2-2.c

 1 #include <stdio.h>
 2 int func(int, int);        // 函数声明
 3 
 4 int main() {
 5     int k = 4, m = 1, p1, p2;
 6 
 7     p1 = func(k, m);    // 函数调用
 8     p2 = func(k, m);    // 函数调用
 9     printf("%d, %d\n", p1, p2);
10 
11     return 0;
12 }
13 
14 // 函数定义
15 int func(int a, int b) {
16     static int m = 0, i = 2;
17 
18     i += m + 1;
19     m = i + a + b;
20 
21     return m;
22 }
View Code

test3.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 long long func(int n){
16     long long f;
17     if(n==1)
18     f=1;
19     else
20     f=(func(n-1)+1)*2-1;
21     
22     
23     return f;
24 } 
View Code

test4.c

 1 #include <stdio.h>
 2 int func(int n, int m);
 3 
 4 int main() {
 5     int n, m;
 6 long long ans;
 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     int i,a=1,b=1,c=1;
14     long long ans;
15     if(n<m)
16     ans=0;
17     else{
18     
19     for(i=1;i<=n;i++)
20     a*=i;
21     for(i=1;i<=m;i++)
22     b*=i;    
23     for(i=1;i<=n-m;i++)
24     c*=i;
25     ans=a/b/c;}
26     
27     
28     return ans;
29 }
View Code

 1 #include <stdio.h>
 2 int func(int n, int m);
 3 
 4 int main() {
 5     int n, m;
 6 long long ans;
 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     long long ans;
14     if(n<m)
15     ans=0;
16     else if(m==0||m==n)
17     ans=1;
18         else
19     {
20         ans=func(n-1,m)+func(n-1,m-1);
21         
22         
23             }
24     return ans;
25 }
View Code

 test5.c

 

 1 #include <stdio.h>
 2 
 3 void move(int n, char A, char B, char C);
 4 
 5 int main()
 6 {
 7     int n;
 8     char A, B, C;
 9     while (scanf("%d", &n) != EOF) {
10         int step = 0;
11         move(n, 'A', 'B', 'C');
12         for (n; n > 0; n--) {
13             step = 2 * step + 1;
14         }
15         printf("\n");
16         printf("一共移动了%d次\n", step);
17         printf("\n");
18     }
19 
20     return 0;
21 }
22 
23 void move(int n, char A, char B, char C) {
24     if (n == 1) {
25         printf("%d:%c --> %c\n", n, A, C);
26     }
27     else {
28         move(n - 1, 'A', 'C', 'B');
29         printf("%d:%c --> %c\n", n, A, C);
30         move(n - 1, 'B', 'A', 'C');
31     }
32 }
View Code

 

 

 

test6.c

 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: %ld\n\n", t);
10 printf("Enter a number: ");
11 }
12 return 0;
13 }
14 long func(long s)
15 {
16     int a=0,i=1;
17     float m=0.1;
18     long s1=0;
19     while(s>=1)
20     {
21         a=s%10;
22         if(a%2==1)
23         {
24         s1+=a*10*m;
25         m*=10;}
26         s/=10;
27     }
28     return s1;}
View Code

 

posted @ 2023-10-31 21:06  FrederickJACK  阅读(17)  评论(0)    收藏  举报