实验2

实验任务一

运行结果

 

 

程序源码

#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)+R1;
        printf("20228330%04d\n",number);
    }
    return 0;
 } 

 

分析

line 18的功能是产生随机数

功能是从学号尾号586到701的人中随机抽取5人

 

实验任务二

运行结果

 

 

程序源码

#include<stdio.h>
int main() 
{
    double x, y; 
    char c1, c2, c3; 
    int a1, a2, a3;
    scanf("%d%d%d", &a1, &a2, &a3); 
    getchar();
    scanf("%c,%c,%c", &c1, &c2, &c3); 
    getchar();
    scanf("%f,%lf",&x,&y); 
    printf("a1 = %d, a2 = %d, a3 = %d\n", a1,a2,a3);
    printf("c1 ='%c', c2 = '%c', c3 = '%c'\n", c1, c2, c3); 
    printf("x = %f, y = %lf\n",x, y); 
    return 0; 
}

  

 

实验任务三

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

 

3-2

运行结果

 

 

程序源码

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

 

3-3.

运行结果

 

 

程序源码

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

 

实验任务四

运行结果

 

 

 

程序源码

 

#include<stdio.h>
int main()
{
    char ans;
    
    printf("输入r或R表示red,输入g或G表示green, 输入y或Y表示yellow\n");
    
    while(scanf("%c", &ans) != EOF)
    {
        getchar();
        
        switch( ans )
        {
            case 'r' :
            case 'R' :printf("stop\n");break;
            case 'y' :
            case 'Y' :printf("wait a minute\n");break;
            case 'g' :
            case 'G' :printf("go go go\n");break;
            default :printf("something must be wrong...\n")    ;    
        }
        
        printf("输入r或R表示red,输入g或G表示green, 输入y或Y表示yellow\n");    
    }
    
    return 0;
    
    
 } 

 

实验任务五

运行结果

 

 

 

 

程序源码

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

int main()
{
    int ans, luckyday, t;
    
    srand( time(0) );
    luckyday = rand() % (30 - 1 + 1) + 1;
    
    printf("猜猜2023年4月的哪一天是你的luckyday?\n");
    scanf("%d",&ans);
        for (t = 3; t >0;) 

    {
        if (ans == luckyday)
            printf("哇,猜中了!");
        else
            if (luckyday> ans)
                printf("你猜的日期早了,你的lucky day还没有到呢\n");
            else
                printf("你猜的日期晚了,你的lucky day已经过啦\n");
        t--;
        if (ans == luckyday||t==0)
            break;
        else
        {
            printf("再猜(1~30):");
            scanf_s("%d", &ans);
        }
    }
    if (t == 0)
        printf("次数用完啦,偷偷告诉你:4月,你的lucky day是%d号", luckyday);
         
    return 0; 
}
    

 

实验任务六

运行结果

 

 

程序源码

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

int main()
{
    int l, c;
    
    for(l = 1; l <= 9; l++)
    {
        for(c = 1; c <= l; c++)
        {
            printf("%d * %d = %2d   ", c, l, c * l);
        }
        
        printf("\n");
    }
    
    system("pause");
    
    return 0;
}

 

实验任务七

运行结果

 

 

程序源码

#include <stdio.h>

int main()
{
    int n, i, j=0;
    printf("input n:");
    
    for(scanf("%d", &n); n>0; n--)
    {
        for( i=1; i<=j; i++)
        {
            printf("      ");
        }
        
        for( i=1; i<=(2*n - 1); i++)
        {
            printf(" o    ");
        }
        
        printf("\n");
        
        for( i=1; i<=j; i++)
        {
            printf("      ");
        }
        
        for( i=1; i<=(2*n - 1); i++)
        {
            printf("<H>   ");
        }
        
        printf("\n");
        
        for( i=1; i<=j; i++)
        {
            printf("      ");
        }
        
        for( i=1; i<=(2*n - 1); i++)
        {
            printf("I I   ");
        }
        
        j++;
        
        printf("\n");
    }
    
    return 0;
}

 

分析

当输入为n时:
第i行,需要打印2*(n-i+1)个字符小人
第i行,前面需要打印i-1个空白

 

posted @ 2023-03-18 11:34  浮灵不漂  阅读(10)  评论(0编辑  收藏  举报