dty1202

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

上机实验作业3

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

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);
    }
    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);
}
复制代码

功能:输出10次hi,May~,每两次中间空行数为0-24的随机数,空格数为0-79的随机数。

task 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;
    printf("p=%lld\n",p);
    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;
}
复制代码

static变量具有继承性,保留上一次计算结果带入新的计算。

task 3

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

task 4

复制代码
#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;int t;
    while(scanf("%d",&n)!=EOF)
    { 
      
       hanoi(n,'A','B','C')    ;
       t=pow(2,n)-1;
       printf("一共移动了%d次.\n\n",t); 
       
    }
    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);
}
复制代码

task 5

复制代码
#include <stdio.h>
#include<stdlib.h> 
#include <math.h>
#define N 20
int is_Prime(int n);

int main()
{
     int t,m;

    for(t=4;t<=N;t=t+2)
  {
        
    for(m=2;m<=t;m++)
    {
        if((is_Prime(m))&&(is_Prime(t-m)))
        {
          printf("%d = %d + %d \n",t,m,t-m);
          break;
        }
    }
  }    

   return 0;
}

int is_Prime(int n)
{
    int m;
    for(m=2;m<=sqrt(n);++m)
     if(n%m==0)
     break;
     
   if(m>sqrt(n)&&n>=2)
   return 1;
   else
   return 0;
}
复制代码

task 6

复制代码
#include <stdio.h>
#include <math.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,x=0,n=0;
    while(s!=0)
    {
        i=s%10;
        if(i%2!=0)
        {
           x=x+i*pow(10,n);
           n=n+1;
        }
        s=s/10;
    }
    return x;
}
复制代码

 

posted on   董亭忆  阅读(31)  评论(3编辑  收藏  举报

相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
点击右上角即可分享
微信分享提示