实验2 C输入输出

task1

程序源码

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

 程序运行截图

 

 

 

task2

程序源码

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

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

 

 

 

程序运行截图

 

 

 

 

 

 

 

task3.1

 

程序源码

#include <stdio.h>
#include <math.h>

int main()
{
    double x, ans;
    
    scanf("%lf", &x);
    ans = pow(x, 365);
    printf("%.2f的365次方:%.2f\n", x, ans);
    
    return 0;
}

 

程序运行截图

 

 

 

 

 

 

 

 

task3.2c

程序源码

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

 程序运行截图

 

 

 

 

 

task4

程序源码

 

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

运行结果

 

 

 

 

 

 

 task5

程序源码

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

 

 

运行结果

 

 

 

 

 

task6

程序源码

 

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

 

运行结果

 

 task7

程序源码

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

运行结果

 

posted @ 2023-03-18 10:25  陈忆安  阅读(14)  评论(0编辑  收藏  举报