找出缺失的数

序列找数

http://www.nowcoder.com/questionTerminal/a7d1856a72404ea69fdfb5786d65539c

等差数列求和,减去已输入的数字的和,差即为未出现的:

import java.util.*;
public class Main {
    public static void main(String[] args) throws Exception{
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int sum = 0;
        while(sc.hasNext()) sum += sc.nextInt();
        System.out.println((1 + n)*n/2 - sum);
    }
}
全部评论
好家伙 看到题目提示贪心,压根没往这个方向上想
点赞 回复 分享
发布于 2022-08-18 22:50 湖北

相关推荐

点赞 评论 收藏
分享
评论
29
2
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务