1

#yyds干货盘点# 解决名企真题: 序列找数

 1 year ago
source link: https://blog.51cto.com/u_15488507/5429417
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

#yyds干货盘点# 解决名企真题: 序列找数

原创

97的风 2022-06-30 10:14:13 博主文章分类:面试题 ©著作权

文章标签 子序列 代码实现 java 文章分类 Java 编程语言 阅读数121

1.简述:

从非负整数序列 0, 1, 2, ..., n中给出包含其中n个数的子序列,请找出未出现在该子序列中的那个数。

输入描述:

输入为n+1个非负整数,用空格分开。

其中:首个数字为非负整数序列的最大值n,后面n个数字为子序列中包含的数字。

输出描述:

输出为1个数字,即未出现在子序列中的那个数。

2.代码实现:

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);
}
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK