实验3

task1

#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 pirnt_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);
    }
    return 0;
} 

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

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

void print_text(int line,int col,char text[]){
    print_blank_lines(line-1);
    print_spaces(col-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;
    p=p*n;
    return p;
}

task3

#include<stdio.h>
long long func(int n);
int main()
{
    int n;
    long long f;
    while(scanf("%d",&n)!=EOF){
        f=func(n);
        printf("n=%d,f=%lld\n",n,f);
    }
    return 0;
}

long long func(int n){
    long long f;
    if(n==0)
    f=0;
    else
    f=2*func(n-1)+1    ;
    return f;
}

task4

#include<stdio.h>
int func(int n,int m);
int main(){
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    printf("n=%d,m=%d,ans=%d\n",n,m,func(n,m));
    return 0;
}

int func(int n,int m){
    int s;
    if(m>n)
    s=0;
    if(m==n||m==0)
    s=1;
    else
    s=func(n-1,m)+func(n-1,m-1);
    return s;
}

task5

#include<stdio.h>
int mul(int n,int m);
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    printf("%d*%d=%d\n",n,m,mul(n,m));
    return 0;
}

int mul(int n,int m){
    int s;
if(m==1)
s=n;
if(m==0||n==0)
s=0;
else
s=mul(n-1,m)+m;
    return s;    
}

task6

#include <stdio.h>
#include <stdlib.h>

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

int count = 0;

int main ()
{
    int n;
    while(scanf("%d",&n) != EOF)
    {
        hanoi(n,'A','B','C');
        printf("\n一共移动了%d次\n",count);
        count = 0;
    }
    
    
    return 0;
}

void hanoi(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(int n,char from,char to)
{
    printf("%u:%c --> %c\n",n,from,to);
    count++;
}

task7

#include<stdio.h>
#include<math.h>
int is_prime(int n);
int main()
{
    int i,j,n,s;
    while(scanf("%d",&i)!=EOF){
        n=i/2;   s=i/2;
        for(;n>0,s<i;n--,s++){
            if(is_prime(n)&&is_prime(s))
            printf("%d=%d+%d\n",i,n,s) ;
    }
        }
    }

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

task8

#include<stdio.h>
long func(long s); 
int main(){
    long s,t;
    printf("Enter a number:");
    while(scanf("%ld",&s)!=EOF){
        t=func(s);
        printf("new number is:%ld\n\n",t);
        printf("Enter a number:");
    } 
    return 0;
}

long func(long s){
    int i; 
    int j=0;
    int t=0;
    int count[100];
    while(s!=0){
        i=s%10;
        s=s/10;
        if(i%2!=0)
        {
        count[j]=i;
        j++;
        }
    }
    for(int k=j-1;k>=0;k--){
            t=t*10+count[k];
        }
return t;
}

 

posted @ 2022-11-06 23:00  sakura——  阅读(34)  评论(0)    收藏  举报