首页 > 其他分享 >shiyan3

shiyan3

时间:2023-04-06 15:01:10浏览次数:29  
标签:return int long char shiyan3 printf include

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

在随机行随机列出现十个“hi,april”,形成动画

task2.1

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;
    printf("p=%d\n",p)

    p = p * n;

    return p;
}

  

 

 task2.2

#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 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 result;
     if(n==0)  result=0;
    else result= 2*fun(n-1)+1;
    return result;
     
 }

  

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 comb(int n){
    int i,p=1;
    for(i=1;i<=n;i++)
    
        p=p*i;
        return p;
    
}
int func(int n,int m){
    
    int a,b,c;
    a=comb(n-1)/(comb(m)*comb(n-1-m));
    b=comb(n-1)/(comb(m-1)*comb(n-1-m+1));
    c=a+b;
}

  

 

 task5.1

#include <stdio.h>
#include<stdlib.h>
double mypow(int x, int y); 
int main() {
int x, y;
double ans;
while(scanf("%d%d", &x, &y) != EOF) {
ans = mypow(x, y); 
printf("%d的%d次方: %g\n\n", x, y, ans);
}

return 0;
}
double mypow(int x,int y)
{int i;double sum=1.0;
    for(i=1;i<=abs(y);i++)
    { sum*=x;
    }
if (y>=0)
return sum;
else
return (1/sum);
}

  

 

 task5.2

#include <stdio.h>
#include<stdlib.h>
double mypow(int x, int y); 
int main() {
int x, y;
double ans;
while(scanf("%d%d", &x, &y) != EOF) {
ans = mypow(x, y); 
printf("%d的%d次方: %g\n\n", x, y, ans);
}
system("pause");
return 0;
}
double mypow(int x, int y)
{
if (y==0)
 return 1;
 else
     if(y>0)
     return x*mypow(x,--y);
     else
     return 1.0/x*mypow(x,++y);

}

  

task6

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

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

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

  

 

task7

#include<stdio.h>
#include<math.h>
#define N 20
int is_prime(int n);


int main()
{
    int m,i,a,b;
   
{
    for(m=4;m<=N;m+=2)
  {
        
    for(i=2;i<=m;i++)
    {
        if(is_prime(i)&&is_prime(m-i))
        {a=i;b=m-i;break;}
    }
    printf("%d = %d + %d \n",m,a,b);
  }    
}
    return 0;
    
}
int is_prime(int n)
{
    int j,k, Flag=1;
    k=sqrt(n);
    if(n==2)
       return Flag=1;
    else
       for(j=2;j<=k;j++)
      {
        if((n%j)==0) 
          {Flag=0;
          break;}
        
      }
     return Flag;
    
}

 

 

task8

#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 a,b,c,d;
    b=0;d=0;
    while(s)
    {
        a=s%10;
        s/=10;
        if(a%2)
          {b=b*10+a;}
    
    }
    while(b)
    {
        c=b%10;
        b/=10;
        d=d*10+c;
    }
    return d;
}

  

 

标签:return,int,long,char,shiyan3,printf,include
From: https://www.cnblogs.com/ctayy/p/17292782.html

相关文章