蓝桥杯-寻找数列中最大数

http://lx.lanqiao.cn/problem.page?gpid=T111

按照题意,是应该用数组来做的,后面用集合的方式就不能通过官方测试系统。(有点醉~~~)

//通过
import java.util.Scanner;

public class Main { 
public static void main(String[] args){
        int[] a = new int[1001];
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int max = 0;
        int t = 0;

        for (int i=0;i<n;i++){
            a[i] = sc.nextInt();
            if (i==0){
                max = a[i];
                t = i;
            }else if (i>0){
                if (max <a[i]){
                    max = a[i];
                    t = i;
                }
            }
        }

        System.out.println(max +" " +t);

    }
}
//不能通过
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int max = 0;
        int t = 0;
        int key;
        for (key=0;key<n;key++){
            int value = sc.nextInt();
            map.put(key, value);
            if (key==0){
                max = map.get(key);
                t=key;
            }else if(key>0){
                if (max < map.get(key)){
                    max = map.get(key);
                    t = key;
                }
                System.out.println("t="+t + "max="+max);
            }
        }
        System.out.println(max + " " +t);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值