CodeGym
CodeGym University
学习
课程
任务
调查和小测验
游戏
帮助
提醒时间表
社区
用户
论坛
聊天
文章
成功故事
活动
评论
订阅服务
浅色主题
立即开始学习
  • 全部问题
null
第 10 级
  • 08.09.2021
  • 277次阅读
  • 1个评论

Where is my problem ?

有关 任务的问题 有关算法的任务
Java 语法,  第 5 级,  课程 12
正在讨论
0
评论 (1)
  • 受欢迎
  • 新
  • 旧
你必须先登录才能发表评论
Gellert Varga
级别 23 ,Szekesfehervar,Hungary
8 九月 2021, 16:54
import java.io.*;
import java.util.*;
/*
有关算法的任务
*/

public class Solution {
    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int maximum =0 ;
        int number = Integer.parseInt(reader.readLine());

        if(number>0){
        //在此编写你的代码
            int [] num = new int[number];
            int max = 0;

            for(int i = 0; i<=number-1; i++){
                num[i] = Integer.parseInt(reader.readLine());
            }

            for(int j = 0;j<=num.length-2;j++){
                max = j;

                for(int k =j+1;k<=num.length-1;k++){
                    if(num[max]<num[k]){
                        max = k;
                    }
                    int temp = num[j];
                    num[j] = num [max];
                    num[max] = temp;
                }
            }

            maximum = num[0];
            System.out.println(maximum);
        }
    }
}
This is your code. The only difference is that it's in a clear format here. Now anybody can clearly see what a block of code contains and where it ends. And now we can talk about it because we have line numbers too. (In the next case please if you click the "help" button on top: Do not copy and paste code, just make sure "attach my solution" button is selected.) line 25: no internal (nested) loop required. It is unnecessary to put the array in ascending or descending order, it is not a requirement now. You just need one loop where you review the elements and if you find a larger one, you change the 'max' to the appropriate index. At the end of the loop, the 'max' variable will contain the index where the largest element is in the array. Then: maximum = num[max]; Because of these changes, '.length-2' in line 22 will need to be modified. Translated with www.DeepL.com/Translator (free)
0
了解
  • 注册
  • Java 课程
  • 任务帮助
  • 定价
  • 游戏项目
  • Java 语法
社区
  • 用户
  • 文章
  • 论坛
  • 聊天
  • 成功故事
  • 活动
  • Affiliate Program
公司
  • 关于我们
  • 联系方式
  • 评论
  • 新闻发布室
  • CodeGym 教育
  • 常见问题
  • 支持
CodeGym CodeGym 是一个从零开始学习 Java 语言编程的在线课程。本课程是初学者掌握 Java 语言的绝佳方式。它包含 1200 多个可即时验证的任务,以及基本范围内的 Java 基础理论。为了帮助你在教育上取得成功,我们实现了一组激励功能:小测验、编码项目以及有关高效学习和 Java 语言开发人员职业方面的内容。
关注我们
界面语言
程序员不是天生的 © 2023 CodeGym
MastercardVisa
程序员不是天生的 © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.