有没有更好的算法实现这个程序?

707wk 2014-12-24 06:56:00
Missing Numbers
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:1536KB
Total submit users: 36, Accepted users: 19
Problem 10050 : No special judgement
Problem description
Leo loves collecting numbers, he consumed half of his spare time and made a collection of all the numbers from 1 to n, one day, he decided to take out all the numbers he gathered and count them(a fantastic idea... -______-b). when the counting was finished, he found that there are only n - 2 numbers left, he can't memorize those numbers he already got, but still wonders which two numbers are missing.

Input
The input file consists of several test cases. Each case starts with a line containing one integer n, 2 < n <= 2000000, Then followed n - 2 lines, each line contains one exclusive integer k, 0 < k <= n. The last line of 0 indicates the end of input which should not be processed.

Output
For each test case, output a single line contains the two missing numbers in ascending order.

Sample Input

3
2
5
1
5
3
0

Sample Output

1 3
2 4

Judge Tips
Huge input file, 'scanf'&'printf' recommended to avoid TLE.
MLE expected...

Problem Source
HNU Contest

输入n,然后输入n-2个数,找出没有输入的那两个数。
我用的方法是一个位记录一个数字,内存占用大概需要238KB,需要两次历遍数组,一次处理,一次输出。代码写的有点问题,输出结果和预期不一样。
各位有没有更好的算法实现这个程序?
...全文
242 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
fly_dragon_fly 2014-12-26
  • 打赏
  • 举报
回复
引用 2 楼 fly_dragon_fly 的回复:
我有个想法,不过没有验证是否满足时间限制, 就扫描一遍就可以了。需要64位的int(用于存放平方和) 1) 每得到一个数字,分别加上和以及平方和 2) 扫描之后通过公式得到 缺少2个数字的 和 , 平方和 3) 解方程就可以得到缺少的2个数字。
64位,也就是2个int了,你的int不会是16位的吧
fly_dragon_fly 2014-12-25
  • 打赏
  • 举报
回复
我有个想法,不过没有验证是否满足时间限制, 就扫描一遍就可以了。需要64位的int(用于存放平方和) 1) 每得到一个数字,分别加上和以及平方和 2) 扫描之后通过公式得到 缺少2个数字的 和 , 平方和 3) 解方程就可以得到缺少的2个数字。
707wk 2014-12-25
  • 打赏
  • 举报
回复
引用 2 楼 fly_dragon_fly 的回复:
我有个想法,不过没有验证是否满足时间限制, 就扫描一遍就可以了。需要64位的int(用于存放平方和) 1) 每得到一个数字,分别加上和以及平方和 2) 扫描之后通过公式得到 缺少2个数字的 和 , 平方和 3) 解方程就可以得到缺少的2个数字。
数值有点大。。。
JiangWenjie2014 2014-12-24
  • 打赏
  • 举报
回复
就是这个思路没错了,仔细检查你的代码把

5,530

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 模式及实现
社区管理员
  • 模式及实现社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧