C语言 实验三

task1

#include <stdio.h> 
#include <stdlib.h>
#include <time.h>
#include <windows.h> 
#define N 80
void printText(int line, int col, char text[]); // 函数声明 
void printSpaces(int n); // 函数声明 
void printBlankLines(int n); // 函数声明 
/*随机插入10个hi,May~,之间包含了有随机空行和随机空格,嗯嗯,我感觉是这样*/ 
int main() 
{ int line, col, i; char text[N] = "hi, May~";
srand(time(0)); // 以当前系统时间作为随机种子 
for(i=1; i<=10; ++i) 
{ line = rand()%25;
 col = rand()%80; 
printText(line, col, text);
 Sleep(1000); // 暂停1000ms
 }return 0; 
 }
 // 打印n个空格 
 void printSpaces(int n)
 { int i; 
 for(i=1; i<=n; ++i) 
 printf(" "); }
 // 打印n行空白行 
 void printBlankLines(int n) 
 { int i; 
 for(i=1; i<=n; ++i) 
 printf("\n");
 }// 在第line行第col列打印一段文本 
 void printText(int line, int col, char text[]) 
 { printBlankLines(line-1); // 打印n-1行空行
 printSpaces(col-1); // 打印n-1列空格
  printf("%s", text); }

task2

#include <stdio.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;/*STATIC的用法就是每次使用的值都是上一次所遗留下来的那个*/
 p = p * n;
  return p;} 

#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

#include <stdio.h> 
long long fun(int); // 函数声明 
int main()
{ int n; 
long long f;
 while (scanf("%d", &n) != EOF) 
{ f = fun(n)-1; // 函数调用 
printf("n = %d, f = %lld\n", n, f); 
}
return 0;}
long long fun(int n)
{long long result;
if(n==0)
result=1;
else
result=2*fun(n-1);
return (result);
}

 

ta#include <stdio.h>

int h(int m);
void hanoi(unsigned n, char from, char temp, char to);  
void moveplate(unsigned n, char from ,char to);  

int main() 
{
    unsigned n;  // 盘子数目
    int s;
    while(scanf("%u", &n) != EOF)
     {
       hanoi(n, 'A', 'B', 'C');
      printf("一共移动盘子的次数:%ld",h(n));
     }
      0;
}
// 把n个盘子,从from →to, 借助temp
void hanoi(unsigned n, char from, char temp, char to) {
    if(n==1)
        moveplate(n, from, to);
    else {
        hanoi(n-1, from , to, temp);
        moveplate(n, from, to);
        hanoi(n-1, temp, from, to); 
    }
}// 把第n个盘子,从from →to
void moveplate(unsigned n, char from, char to) {
    printf("第%u个盘子: %c --> %c\n", n, from, to);
}
int h(int m)
{
    int s;
    if(m==1)
        s=1;
    else
        s=2*h(m-1)+1;
    return s;
}

 

task5

#include<stdio.h>
#include<math.h>
int is_prime(int);
int main()
{
    int i,j,k;
    for(i=4;i<=20;i+=2)
    {
    for(j=2;j<=(i/2);j++)
    {
    if(is_prime(j)==1)
    if((i-j)>1&&is_prime(i-j)==1)
    printf("%d=%d+%d\n",i,j,i-j);
    }
    }return 0;

}
int is_prime(int n)
{
    int i,fun;
    fun=1;
    for(i=2;i<n;i++)
    if(n%i==0)
    fun*=0;
    else
    fun*=1;
    return fun;
    
}

 

 task6


#include<stdio.h>
long fun(long s);
int main()
{ long s, t;
printf("enter a number: ");
while (scanf("%ld", &s) != EOF)
{
t = fun(s); // 函数调用
printf("new number is: %ld\n\n", t);
printf("Enter a number: "); }
return 0; }
long fun(long s)
{long m,k,g,w,z;
k=0;
z=0;
m=s;
while(m)
{
g=m%10;
m=m/10;
if(g%2)
k=k*10+g;
}
while(k)
{
w=k%10;
k=k/10;
z=z*10+w;
}
return z;
}

 

 

总结

会忘记许多细节

把关键词拼错

变量多了会容易搞混

希望自己能加强训练

加油

posted @ 2022-04-21 21:01  wen_zhang  阅读(68)  评论(0)    收藏  举报