处女座点名

链接:https://ac.nowcoder.com/acm/contest/329/C
来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述

处女座觉得自己手上的经费可能不太够,于是决定给牛逼学生们带家教。

一天他去上课用自己的火眼金睛感觉教室里有一个学生没有来,于是他就叫学生们报出自己的学号。

已知这个班上的学号是从1开始连续编号的,处女座告诉你这个班上有多少人,想问问你到底是谁没有来。
输入描述:
输入数据共两行,第一行为一个整数N,表示班上的学生数量。

第二行为一行N-1个整数,表示已经来的学生的学号,按升序给出。
输出描述:
输出一个整数,为没有来的学生的学号。
示例1
输入
复制
3
1 3
输出
复制
2
备注:
2

N

200
,
000

思路:本来是打算直接一个循环就查出缺的就行,结果一直WA,说返回非零。改成数组查才AC,憋屈啊

错误代码:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n=sc.nextLong();
        long index=0;
        for(long i=1;i<=n;i++){
            long temp=sc.nextLong();
            if(temp!=i){
                index=i;
                i++;
            }
        }
        System.out.println(index);
        sc.close();
    }
}

AC代码:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n=sc.nextInt();
        int index=0;
        int []num=new int[n+1];
        for(int i=1;i<n;i++){
            num[i]=sc.nextInt();
        }
        for(int i=1;i<=n;i++){
            if(num[i]!=i){
                index=i;
                break;
            }
        }
        System.out.println(index);
        sc.close();
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_42105744/article/details/87088457
今日推荐