实验3

------------恢复内容开始------------

tesk.1.c

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#define N 80
void printText(int line,int col,char text[]);
void printSpaces(int n);
void printBlanklines(int n);
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(1);
    }
    return 0;
}

void printSpaces(int n)
{
    int i;
    for ( i = 1; i <=n; i++)
    {
    printf(" ");
    }
    
}
 
void printBlanklines(int n)
{
    int i;
    for ( i = 1; i <=n; i++)
    {
        printf("\n");
    }
}

void printText(int line,int col,char text[])
{
    printBlanklines(line-1);
    printSpaces(col-1);
    printf("%s",text);
}

tesk2-1.c

#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;
    p*=n;
    return p;

}

tesk2-2.c

#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;
}

tesk3.c

#include <stdio.h>
long long fun(int n);
int main()
{
    int n;
    long long f;
    while (scanf("%d",&n)!=EOF)
    {
        f=fun(n);
        printf("n=%d,f=%lld\n",n,f);
    }
    return 0;
}
long long fun(int n)
{
    long long p;
    if (n==0)
    p=0;
    else
     p=2*fun(n-1)+1;
    return p;
}

tesk4.c

#include<stdio.h>
#include<math.h>
void hanoi(unsigned int n, char from, char temp, char to);
void moveplate(unsigned int n, char from, char to);
int main()
{
    unsigned int n,m;
    while (scanf("%u", &n) != EOF)
    {
        hanoi(n, 'A', 'B', 'C');
        m = pow(2, n) - 1;
        printf("一共移动了%d次\n\n", m);
    }
    return 0;
}
void hanoi(unsigned int 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);
    }
}
void moveplate(unsigned int n, char from, char to)
{
    printf("第%u个盘子:%c-->%c\n", n, from, to);
}

tesk5.c

 

#include <stdio.h>
#include <math.h>
int is_prime(int x)
{
    int m;
     for ( m = 2; m<=sqrt(x); m++)
    if(x%m==0)
    return 0;
    return 1;
}

int main()
{
    int j,i,n;
    for ( j = 4; j <=20; j+=2)
    {
      for ( i = 2; i <=j/2; i++)
      {
          if(is_prime(i)==1&&is_prime(j-i)==1)
          printf("%d=%d+%d\n",j,i,j-i);
      }
    }
 return 0;   
}

tesk6,c

#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)
{
    int i=0,p,t=0,x,y=0;
    p=s;
    while (p!=0)
    {
        i=p%10;
        p=p/10;
        if (i%2==0)
         continue;
        if(i%2==1)
         t=t*10+i;
        continue;
    }
    while (t%10!=0)
    { 
        x=t%10;
        t/=10;
        y=10*y+x;
    }
    return y;
    
}

 

posted @ 2022-04-23 14:43  甜司康饼  阅读(16)  评论(3编辑  收藏  举报