m0_54132659 2021-03-15 10:47 采纳率: 83.3%
浏览 171
已结题

c/c++ (计算机系统)的编程题,请会的大佬直接留下代码

Celmomether

A thermometer measures the temperature at regular intervals and stores the measured temperatures (may be also negative) in the temperatures array of integers (the int type). Unfortunately, the device is somewhat unreliable, so sometimes it is not able to measure the temperature. In that case, the value stored in the array is equal to no_value constant.

Display the measured values in the array on the standard output as a horizontal bar thermometer. Each measured value will be displayed on one line. The first column of the output represents the most negative measured value (ignore no_value constant). Each additional column of the output represents a temperature 1 degree higher. The temperature will be displayed as a continuous segment of the characters * from the column representing the measured temperature to the column representing the temperature 0. Space characters are used for padding of the unused columns on the left side. If a value corresponding to the constant no_value appears in the measured values, then the last actually measured value is used instead (use 0 if there is no such value).

To simplify the input, insert the following two lines into your source code and use them as input array for measured values

constexpr int no_value = -999;
int temperatures[] { 10, 12, no_value, no_value, 20, 14, 6, -1, -5, 0, no_value, 1, -3 };

Demo output for values -1, no_value, -3, 0, 2:

  **
  **
****
   *
   ***
  • 写回答

3条回答 默认 最新

  • 502203305 2021-03-15 11:20
    关注
    #include<stdio.h>
    int main(void){
        constexpr int no_value = -999;
        int temperatures[] { 10, 12, no_value, no_value, 20, 14, 6, -1, -5, 0, no_value, 1, -3 };
        int templen = sizeof(temperatures)/sizeof(*temperatures);
        int tempMax,tempStart, temperMin = 999;
        for(int i = 0 ; i < templen; i++)
        {
            if(no_value == temperatures[i])
            {
                if(0 == i)
                {
                    temperatures[i] = 0;
                }
                else
                {
                    temperatures[i] = temperatures[i - 1];
                }
            }
            temperMin = temperatures[i] < temperMin ? temperatures[i] : temperMin;
        }
    
        for(int i = 0 ; i < templen; i++)
        {
            if(temperatures[i] < 0)
            {
                tempMax = 0 ;
                tempStart = temperatures[i];
            }
            else
            {
                tempMax = temperatures[i] ;
                tempStart = 0;
            }
            for(int j = temperMin ; j <= tempMax ; j ++)
            {
                printf(j < tempStart?" ":"*");
            }
            printf("\n");
        }
    }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 steam下载游戏占用内存
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系