第二次上机作业

task 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<5;i++)
    {
        number = rand()%(R2-R1+1)+R1;
        printf("202283300%04d\n",number);
    }
    system("pause");
    return 0;
}

问题1:实现了生成一个586~701的随机数。

问题2:该程序的功能为随机抽取5个学号。

 

 

task 2

#include<stdio.h>
#include<stdlib.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);//将%f改为%lf 
    printf("x=%lf,y=%lf\n",x,y);//将%f改为%lf 
    system("pause");
    return 0;
}

task 3

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

    return 0;
}

 

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

 

task 4

#include<stdio.h>
#include<stdlib.h>
int main()
{
    char x;
    x = getchar();
    for(;x!= EOF ;)
    {
    switch(x)
    {
        case'y':printf("wait a minue\n");break;
        case'g':printf("go go go\n");break;
        case'r':printf("stop!\n");break;
        default:printf("something must be wrong...\n");break;
    }
    getchar();
    x = getchar();
    }
    system("pause");
    return 0;
}

task 5

 

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

 

 

task 6

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int i,j;
    for(i=1;i<=9;i++)
    {
        for(j=1;j<=i;j++)
        {
            printf("%dx%d = %2d\t",i,j,i*j);
        }
        printf("\n");
    }
    system("pause");
    return 0;
}

task 7

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int n,i,j,k;
    printf("请输入打印几行:"); 
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        for(k=1;k<=i-1;k++)
        {
        printf("\t");
        }
        for(j=1;j<=(2*(n+1-i)-1);j++)
        {
            printf(" O \t");
        }
        printf("\n");
        for(k=1;k<=i-1;k++)
        {
        printf("\t");
        }
        for(j=1;j<=(2*(n+1-i)-1);j++)
        {
            printf("<H> \t");
        
        }
        printf("\n");
        for(k=1;k<=i-1;k++)
        {
        printf("\t");
        }
        for(j=1;j<=(2*(n+1-i)-1);j++)
        {
            printf("I I\t");
        }
        printf("\n");

    }
    system("pause");
    return 0;
}

当输入n时:第i行需要打印2*(n+1-i)-1个小人

                    第i行,前面需要打印 i-1个空白(\t)

总结

1.通过本次实验,我更熟悉了for 语句的嵌套结果。

2.了解了break与continue的使用

3.if与for 结构的嵌套更加熟练

posted @ 2023-03-17 22:12  chen,,  阅读(20)  评论(0)    收藏  举报