实验3

#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, April~";
     
    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中字符串
}

在25x80的区域内随机生成10个“hi,April~”

task2.1

 1 #include <stdio.h>
 2 #include<stdlib.h>
 3 long long fac(int n); // 函数声明
 4 
 5 int main() {
 6     int i, n;
 7 
 8     printf("Enter n: ");
 9     scanf("%d", &n);
10 
11     for (i = 1; i <= n; ++i)
12         printf("%d! = %lld\n", i, fac(i));
13 
14     system("pause");
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

分析输出结果为8,17

// 练习:局部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

 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         if (n<0||n>32)
10             {printf("Error,n>=0&&n<=32");
11         break;}
12         else
13         f = func(n); // 函数调用
14         printf("n = %d, f = %lld\n", n, f);
15     }
16 
17     return 0;
18 }
19 
20 // 函数定义
21 long long func(int n){
22 if(n==0)
23     return 0;
24 else
25     return 2*(func(n-1)+1)-1;
26 }

 

task4

 1 #include <stdio.h>
 2 int func(int n, int m);
 3 int func1(int x);
 4 
 5 int main() {
 6     int n, m;
 7 
 8     while(scanf("%d%d", &n, &m) != EOF)
 9         printf("n = %d, m = %d, ans = %d\n", n, m, func(n, m));
10     
11     return 0;
12 }
13 int func1(int x){
14 int i,j=1;
15 for(i=1;i<=x;i++){
16 j=j*i;
17 }
18 return j;
19 }
20 
21 
22 int func(int n,int m){
23     return func1(n)/(func1(m)*func1(n-m));
24 }

递归法4.2

 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     if(m == 0 || n == m)
17         return 1;
18     else
19         return func(n-1,m-1)+func(n-1,m);
20 }

 task5

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<math.h>
 4 void hanoi(unsigned int n,char from,char temp,char to);
 5 void moveplate(unsigned int n,char from,char to);
 6 
 7 int main()
 8 {
 9     unsigned int n;
10     int s;
11     while(scanf("%d",&n)!=EOF)
12     {
13     hanoi(n,'A','B','C');
14     s=pow(2,n)-1;
15     printf("一共移动了%d次\n",s); 
16 }
17     system("pause");
18     return 0;
19  } 
20  
21  void hanoi(unsigned int n,char from,char temp,char to)
22  {
23      if(n==1)
24       moveplate(n,from,to);
25      else
26      {
27          hanoi(n-1,from,to,temp);
28          moveplate(n,from,to);
29          hanoi(n-1,temp,from,to);
30      }
31  }
32  
33  void moveplate(unsigned int n,char from,char to)
34  {
35      printf("%d:%c-->%c\n",n,from,to);
36  }

 task6

 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_s("%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 long func(long s){
19     int m,t=1;
20     long ans=0;
21     while(s!=0)
22     {
23     m=s%10;
24         if(m%2!=0)
25         {
26         ans=ans+t*m;
27         t=t*10;
28         
29         }
30     s=s/10;
31     
32     }
33     return ans;
34 }

 

posted on 2024-04-28 07:26  马猴烧酒酱  阅读(6)  评论(0编辑  收藏  举报