实验2

实验任务1

程序代码:

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

#define N 5
#define R1 586
#define R2 701

int main()
{
    int number;
    int i;
    
    srand( time(0) );
    
    for(i=0;i<N;++i)
    {
        number=rand()%(R2-R1+1)+R1;
        printf("20228330%04d\n",number);
    }
    
    return 0;
 } 

 

运行结果:

 

讨论:

line18生成了一个从586-701之间的随机数

点名

 

实验任务2

程序代码:

#include<stdio.h>
int main()
{
double x, y;
char c1, c2, c3;
int a1, a2, a3;

scanf("%d%d%d", &a1, &a2, &a3);
printf("a1 = %d, a2 = %d, a3 = %d\n", a1,a2,a3);
getchar();

scanf("%c%c%c", &c1, &c2, &c3);
printf("c1 = %c, c2 = %c, c3 = %c\n", c1, c2, c3);

scanf("%lf,%lf", &x, &y);
printf("x = %lf, y = %lf\n",x, y);

return 0;
}

 

运行结果:

 

讨论;

 

实验任务3

task2

程序代码:

#include <stdio.h>
#include <math.h>
int main()
{
double x, ans;
while(scanf("%lf", &x) != EOF)
{
ans = pow(x, 365);
printf("%.2f的365次方: %.2f\n", x, ans);
printf("\n");
} 
return 0;
}

 

运行结果:

 

讨论:

 

task3

程序代码:

#include <stdio.h>
#include <math.h>
int main()
{
double c, f;
while(scanf("%lf", &c) != EOF)
{
f = 1.8*c+32;
printf("摄氏度c=%.2f时,华氏度f=%.2f",c,f);
printf("\n");
} 
return 0;
}

 

实验结果:

 

讨论:

 

实验任务4

程序代码:

 

#include<stdio.h>
int main()
{
    char a;
    while(scanf("%c",&a)!=EOF)
    {   
        switch(a)
        {
            case 'r':printf("stop!\n");break;
            case 'y':printf("wait some time.\n");break;
            case 'g':printf("go go go\n");break;
            default:printf("something must be wrong...\n");break;
        
        }
            getchar();
    }
    return 0;
}

 

运行结果:

 

 

讨论:

 

 

实验任务5

程序代码:

 

#include<stdio.h>
#include<time.h>
int main()
{
    int l,a,i,flag;
    flag=1;
    srand(time(0));
    l=rand()%30+1;
    printf("猜一猜2023年4月哪一天会是你的lucky day\n");
    printf("开始咯,你有三次机会,猜吧(1~30):\n");
    
    for(i=1;i<=3;i++)
    {
        scanf("%d",&a);
        if(a==l)
          {
          printf("哇~~猜中了!!");
          flag=0;
          break;
            }
        else if(a<l)
        {
            printf("你猜的日期早了,你的lucky day还没到呢\n再猜(1~30):");
             
        }
        else if(a>l)
        {
            printf("你猜的日期晚了,你的lucky day已经过啦\n再猜(1~30):");
        }
    }
    if(flag)
    printf("次数用完啦。偷偷告诉你:4月,你的lucky day是%d号",l);
    
    return 0;
}

 

运行结果:

 

 

讨论:

 

 

实验任务6

程序代码:

 

#include<stdio.h>
int main()
{
    int h,l;
    h=1,l=1;
    for(;h<=9;h++)
    {
        for(;l<=h;l++)
        {
            printf("%d×%d = %d \t",l,h,l*h);
        }
        printf("\n");
        l=1;
    }
    
    return 0;
}

 

运行结果:

 

 

讨论:

 

 

实验任务7

程序代码:

 

#include<stdio.h>
int main()
{
    int n,i,j,m,k;
    scanf("%d",&n);
    m=n;
    for(i=1;i<=n;i++)
    {
        for(k=1;k<=i-1;k++)
        {
            printf("\t");
        }
        for(j=1;j<=m;j++)
        {
            printf(" 0 \t");
        }
            printf("\n");
            
        for(k=1;k<=i-1;k++)
        {
            printf("\t");
        }
        for(j=1;j<=m;j++)
        {
            printf("<H>\t");
        }
        printf("\n");
        
        for(k=1;k<=i-1;k++)
        {
            printf("\t");
        }
        for(j=1;j<=m;j++)
        {
            printf("I I\t");
        }
        m=m-2;
        printf("\n");

    }
    
    return 0;
}

 

运行结果:

 

 

讨论:

 当输入为n时,第i行,需要打印n-2i个小人,前面需要使用i-1个‘ \t ’

posted @ 2023-03-17 12:38  lzque  阅读(18)  评论(0编辑  收藏  举报