cracker_03 2021-09-08 20:38 采纳率: 82.6%
浏览 23
已结题

代码答案一直出错,不知道哪里需要改

Description

There are some students in a class, Can you help teacher find the highest student .

Input

There are some cases. The first line contains an integer t, indicate the cases; Each case have an integer n ( 1 ≤ n ≤ 100 ) , followed n students’ height.

Output

For each case output the highest height, the height to two decimal plases;

Sample Input

2
3 170.00 165.00 180.00
4 165.00 182.00 172.00 160.00
Sample Output

180.00
182.00


#include<stdio.h>
int main()
{
    int t;
    scanf("%d", &t);
    for (int i = 0; i < t; i++)
    {
        int n;
        scanf("%d", &n);
        double a[100] = {'\0'};
        for (int j = 0; j < n; j++)
        {
            scanf("%lf", &a[i]);
        }
        double max=0.0;
        for (int k = 0; k < n; k++)
        {
            if (a[i] > max)
                max = a[i];
        }
        printf("%.2lf\n", max);
    }
    return 0;
}

  • 写回答

2条回答 默认 最新

  • qzjhjxj 2021-09-08 21:00
    关注

    修改如下,供参考:

    #include<stdio.h>
    int main()
    {
        int t;
        scanf("%d", &t);
        for (int i = 0; i < t; i++)
        {
            int n;
            double a, max;
            scanf("%d", &n);
            for (int j = 0; j < n; j++)
            {
                scanf("%lf", &a);
                if (j == 0) {
                    max = a;
                }
                else {
                    if (max < a)max = a;
                }
            }
            printf("%.2lf\n", max);
        }
        return 0;
    }
    
    

    题主的代码修改如下,问题见注释:

    #include<stdio.h>
    int main()
    {
        int t;
        scanf("%d", &t);
        for (int i = 0; i < t; i++)
        {
            int n;
            scanf("%d", &n);
            double a[100] = { 0 };//double a[100] = { '\0' };
            for (int j = 0; j < n; j++)
            {
                scanf("%lf", &a[j]);//scanf("%lf", &a[i]);
            }
            double max = 0.0;
            for (int k = 0; k < n; k++)
            {
                if (a[k] > max)  //if (a[i] > max)
                    max = a[k];  //max = a[i];
            }
            printf("%.2lf\n", max);
        }
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 9月16日
  • 已采纳回答 9月8日
  • 创建了问题 9月8日

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题