实验2

发布时间 2023-10-21 21:11:23作者: bunga

实验任务1

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

#define N 5
#define N1 374
#define N2 465

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

1:作用是将随机数字限制在374到465

2:

实验任务2

 

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

 

实验任务3

 

 

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main()
{
    int i,n,day,c;
    day = rand()%30 + 1;
    c = 0;
    printf("猜猜2023年11月哪天是你的lucky day\n");
    printf("开始喽,你有三次机会,猜吧\n");
    for(i=1;i <=3;++i)
    {
        scanf("%d",&n);
        if(n<day)
        {
            printf("你猜的日期早了,你的lucky day还没到呢\n");
            c++;
            if(c<3)
            printf("再猜\n");
        }
        else if(n>day)
        {
            printf("你的日期晚了,你的lucky day已经过了\n");
            c++;
            if(c<3)
            printf("再猜\n");
        }
        else{
            printf("哇,猜中了\n");
        }
        break;
    }
    return 0;
}

 

实验 任务4

 

#include <stdio.h>
#include <math.h>
int main()
{
    int a,n,i;
    double s,down;
    while(scanf("%d%d",&n,&a)!=EOF)
    {
        down = a;
        s = 0;
        for(i = 1;i <= n;++i)
        {
            s +=i/down;
            down = 10 *down + a;
        }
        printf("n = %d,a = %d ,s = %lf\n",n,a,s);
    }
    return 0;
}

 实验任务5

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

 

 

实验任务6

#include<stdio.h>
int main()
{
     int n;
     scanf("%d", &n);
     int n1 = n;
     int s;
     int space; 
     for (n = n; n > 0; n--)
{
    for (space = n1 - n; space > 0; space--)
{
             printf("\t");
         }
         for (s = 2 * n - 1; s > 0; s--) 
{
             printf(" o\t");
         }
         printf("\n");
         for (space = n1 - n; space > 0; space--)
{
             printf("\t");
         }
         for (s = 2 * n - 1; s > 0; s--) 
{
             printf("<H>\t");
       }
         printf("\n"); 
        for (space = n1 - n; space > 0; space--)
{
             printf("\t");
        }
         for (s = 2 * n - 1; s > 0; s--)
{
             printf("I I\t");
         }
         for (space = n1 - n; space > 0; space--)
{
             printf("\t");
         }
        printf("\n");
     }
 
     return 0;
}