首页 > 其他分享 >实验三

实验三

时间:2023-10-31 09:56:25浏览次数:25  
标签:int long char 实验 printf include

 

 

实验3.1

实验3.1源码

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

 

 

 

实验3.1运行结果

 实验3.2

实验3.2源码

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

 

 

 

实验3.2运行结果

 

 理解:static局部变量只有在第一次会为其赋初值,其它时候都会改变。

 

实验3.3

实验3.3源码

#include<stdio.h>
#include<stdlib.h>
long long fac(int n);
int main()
{
    int n;
    scanf("%d",&n);
    printf("%lld",fac(n));
    system("pause");
    return 0;
}
long long fac(int n)
{
    if(n==1)
        return 1;
    else
        return fac(n-1)*2+1;
}

 

 

 

 

实验3.3运行结果

 

实验3.4

实验3.4源码

 

#include<stdio.h>
#include<stdlib.h>
int func(int n,int m);
int main()
{
    int n,m;
    while("scanf("%d%d",&n,&m)!=EOF)
    {
        printf("%lld",func(n));
    }
    system("pause");
    return 0;
}
int func(int n,int m)
{
    long long a=1,b=1;
    int i;
    for(i=n;i>=n-m+1;n--)
        a*=i;
    for(i=1;i<=m;i++)
        b*=i;
    return a/b;
}

 

#include<stdio.h>
#include<stdlib.h>
int func(int n,int m);
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        printf("%lld",func(n,m));
    }
    system("pause");
    return 0;
}
int func(int n,int m)
{   if(n<m)
      return 0;
    else if(n==m||m==0)
      return 1;
    else
      return func(n-1,m)+func(n-1,m-1);
}

 

 

实验3.4运行结果

 

 

 

实验3.5

实验3.5源码

 

#include<stdio.h>
#include<stdlib.h>
void hanio(int n,char from,char temp,char to);
void move(int n,char from,char to);
int t;
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {   
        hanio(n,'A','B','C');
        printf("一共移动了%d\n",t);
        t=0;
        

    }
    system("pause");
    return 0;
}
void hanio(int n,char from,char temp,char to)
{
    if(n==1)
        move(n,from,to);
    else
    {
        hanio(n-1,from,to,temp);
        move(n,from,to);
        hanio(n-1,temp,from,to);
        
    }
}
void move(int n,char from,char to)
{   
    printf("%d:%c->%c\n",n,from,to);
    t++;


}

 

 

实验3.5运行结果

 

 

 

实验3.6

实验3.6源码

 

 

 

#include<stdio.h>
#include<stdlib.h>
#include<math.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 new number:");
        
    }
    system("pause");
    return 0;
}
long func(long s)
{   
    long a=0,b=0;
    do
    {
        if(s%2!=0)
        { a=a*10+s%10;}
        s/=10;



    }while(s!=0);
    while(a!=0)
    {
        b=b*10+a%10;
        a/=10;
    }
    return b;




}

 

 

实验3.6运行结果

 

 实验3.7

实验3.7源码

 

 

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
void bubble_sort(int x[],int n);
void hb(int x[],int a[],int b[],int n,int m);
int xi;
int main()
{
    int a[10];
    int b[10];
    int c[20];
    int n,i,t,o,p,z,k,r;
    
    long tot1,tot2;
for(i=10;i<=1000;i++)
/*while(scanf("%d",&i)!=EOF)*/
{   z=0,t=0,k=0,r=0;
    tot1=pow(i,2);
    tot2=pow(i,3);
    while(tot1!=0)
    {
        a[k++]=tot1%10;
        tot1/=10;
    }
    
    while(tot2!=0)
    {
        b[t++]=tot2%10;
        tot2/=10;
        
    }

    
    
    bubble_sort(a,k);
/*    for(o=0;o<k;o++)
    { printf("%3d",a[o]);
    }
    printf("\n");*/
    bubble_sort(b,t);
/*    for(o=0;o<t;o++)
    { printf("%3d",b[o]);
    }
    printf("\n");*/
    hb(c,a,b,k,t);
/*    for(o=0;o<k+t;o++)
    { printf("%3d",c[o]);
    }
    printf("\n");*/
    if(xi==10)
    {    
        
        for(r=0;r<10;r++)
        {
            if(c[r]==9-r)
                z++;

        }
        if(z==10)

        printf("%d\n",i);

    }
    
}
}
void bubble_sort(int x[],int n)
{
    int i,j,t;
    for(i=0;i<n-1;i++)
    {
        for(j=0;j<n-1-i;j++)
        {
            if(x[j]<x[j+1])
            {
                t=x[j+1];
                x[j+1]=x[j];
                x[j]=t;
            }

        }
    }
}
void hb(int x[],int a[],int b[],int n,int m)
{
    int ai,bi;
    xi=0,bi=0,ai=0;
    while(ai<n&&bi<m)
    {
        if(a[ai]<b[bi])
            x[xi++]=b[bi++];
        else
            x[xi++]=a[ai++];
    }
    while(ai<n)
        x[xi++]=a[ai++];
    while(bi<m)
        x[xi++]=b[bi++];
}

 

 

 

实验3.7运行结果

 

 

标签:int,long,char,实验,printf,include
From: https://www.cnblogs.com/chwchw/p/17798411.html

相关文章

  • 深入浅出Docker应用(阿里云实验)
    (Docker安装和配置)一、docker安装yuminstall-yyum-utilsdevice-mapper-persistent-datalvm2yuminstall-yjqyum-config-manager--add-repohttps://download.docker.com/linux/centos/docker-ce.repoyuminstalldocker-ce.x86_64yuminstall-ydocker-ce.x86_643:......
  • 实验3
    一、实验任务1#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);?//函数声明voidprint_spaces(intn);?//函数声明voidprint_blank_lines(intn);//......
  • 实验3 C语言函数应用编程
    实验任务11#include<stdio.h>2#include<math.h>3#include<stdlib.h>4#include<time.h>5#include<windows.h>6#defineN807voidprint_text(intline,intcol,chartext[]);8voidprint_spaces(intn);9voidpr......
  • 实验三
    实验三任务一代码1#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#include<windows.h>5#defineN806voidprint_text(intline,intcol,chartext[]);7voidprint_spaces(intn);8voidprint_blank_lines(intn);9......
  • 实验三
    实验1每隔100ms在随机显示字符串 实验2 一致 实验3    实验4   实验5   实验六    实验七  #include<stdio.h>intfunc(intn2,intn3);intmain(){ intn=0; intflag=0; while(1){ intn2,n3; n2=n*n;......
  • 实验三 计算机九班周天意202383290419
    一、实验目的1.能正确使用c语法规则定义、声明、调用函数2.能正确编写递归函数3.针对具体问题场景,能合理抽象出独立的功能模块,正确定义函数并使用,使得代码更具可读性、可维护性4.针对具体问题场景,能正确、合理使用全局变量和局部static变量,解决实际问题二、实验准备实验前......
  • 实验3
    task11#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#include<windows.h>5#defineN806voidprint_text(intline,intcol,chartext[]);//函数声明7voidprint_spaces(intn);//函数声明8voidprint_blank_l......
  • 实验3 C语言函数应用编程
    一、实验目的能正确使用c语法规则定义、声明、调用函数能正确编写递归函数针对具体问题场景,能合理抽象出独立的功能模块,正确定义函数并使用,使得代码更具可读性、可维护性针对具体问题场景,能正确、合理使用全局变量和局部static变量,解决实际问题二、实验准备函数定义、声......
  • 实验三
    #include<stdio.h>inti=0;voidhanoi(unsignedintn,charfrom,chartemp,charto);voidmoveplate(unsignedintn,charfrom,charto);intmain(){unsignedintn;while(scanf("%u",&n)!=EOF){i=0;......
  • 实验3_C语言函数应用编程
    1.task_11#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#include<windows.h>5#defineN8067voidprint_text(intline,intcol,chartext[]);8voidprint_spaces(intn);9voidprint_blank_lines(intn)......