首页 > 其他分享 >实验2

实验2

时间:2023-03-18 21:33:40浏览次数:30  
标签:lf include int scanf 实验 printf main

实验任务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;
}

运算结果:

line 18 是随机生成586——701之间的一个随机数,该程序是随机生成5个学号

实验任务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);
    
    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:

代码1:

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

运算结果1:

代码2:

#include <math.h>

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

运算结果2:

实验任务4:

代码:

#include<stdio.h>

int main(){
    char color;
    scanf("%c",&color);
    while(scanf("%c",&color) !=EOF){
    switch(color){
        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;
    }
  }
  return 0;
}

运算结果:

 

 

实验任务5:

代码:

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

int main()
{
    int num, i, n;
    srand(time(0));
    num = rand() % (30 - 1 + 1) + 1;
    printf("猜猜2023年4月哪一天是你的lucky day\n");
    printf("\n");
    printf("开始喽,你有三次机会,猜吧(1~30):");
    scanf("%d", &n);
    printf("\n");
    for(i=1;i<3;i++)
    {
        if (n > num)
            printf("你猜的日期晚了,你的lucky day已经过了\n");
        else if (n < num)
            printf("你猜的日期早了,你的lucky day还没到呢\n");
        else
        {
            printf("哇,猜中了:-)");
            break;
        }

        printf("\n");
        printf("再猜(1~30):");
        scanf("%d", &n);
        printf("\n");
    }
    if (n != num)
        printf("次数用完啦。偷偷告诉你:4月,你的lucky day是%d号",num);
    return 0;
}

 

运算结果:

 

 

实验任务6:

代码:

int main()
{
    int i, n,s;
    for (n = 1; n <= 9; n++)
    {
        for (i = 1; i <= n; i++)
        {
            s = n * i;
            printf("%dx%d=%2d  ", n, i, s);
        }
        printf("\n");
    }
    return 0;
}

运算结果:

 

实验任务7:

代码:

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

运算结果:

第i行  2(n-i)+1个小人

3+4(n-i)个空白

 

标签:lf,include,int,scanf,实验,printf,main
From: https://www.cnblogs.com/qwerty666/p/17224213.html

相关文章

  • 实验二
    实验任务一源代码#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineR1586#defineR2701intmain(){intnumber;inti;......
  • SYNU PTA C++ 第三章实验题
    题目详细内容见PTA,仅提供答案参考。7-7冒泡1#include<iostream>2usingnamespacestd;34voidmerge(int*arr,intleft,intmid,intright)5{6......
  • 实验二
    task1.c#defineR1586#defineR2701intmain(){intnumber;inti;srand(time(0));for(i=0;i<N;++i){number......
  • 实验2 C语言输入输出和控制语句应用编程
    实验任务11#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#defineN55#defineR15866#defineR27017intmain()8{9intn......
  • 202113020023李湘楠实验2实验报告
    //test1.c#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineR1586#defineR2701intmain(){intnumber;inti;sr......
  • STM32 HAL库学习 (2) USART实验
    使用STM32F407串口:PA9、PA10(利用CH340G驱动)1.stm32f4xx_hal_uart.c函数说明HAL_UART_Init函数要使用一个外设首先要对它进行初始化,所以先看串口的初始化函数,其声明......
  • 实验2
    实验任务1实验代码#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineR1586#defineR2701intmain(){intnumber;inti......
  • 实验2
    实验任务1#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineR1586#defineR2701intmain(){intnumber;inti;......
  • 实验2
    实验任务一运行结果  程序源码#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineR1586#defineR2701intmain(){intnumbe......
  • 实验二
    1、程序源码#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineR1586#defineR2701intmain(){ intnumber; inti; srand(time(0)......