FOJ1001

Problem 1001 Duplicate Pair
Accept: 3796    Submit: 18020
Time Limit: 1000 mSec    Memory Limit : 65536 KB
Problem Description

An array of length n, with address from 1 to n inclusive, contains entries from the set {1,2,...,n-1} and there's exactly two elements with the same value. Your task is to find out the value.

Input
Input contains several cases.
Each case includes a number n (1<n<=10^6), which is followed by n integers.
The input is ended up with the end of file.

Output
Your must output the value for each case, one per line.
Sample Input
2
1 1
4
1 2 3 2
Sample Output
1 2


这道题用C实现时困难挺大的,涉及到好多知识,如int 数据类型所能表示的最大值,
出现很多错误:如 runtime error ,因为 (m= scanf(“%ld",&n))!=EOF
wrong answer ,输出数据的格式问题,n*(n-1) 时数据溢出的问题。
用java 实现时老是出timelimit exceed 的超时错误。
#include <stdio.h>
int main()
{
        long n,i;
        long temp;
        double result1=0;
        double result2=0;
        double temp1;
        while(scanf("%ld",&n)!=EOF){
                temp1=n;
                result1=(temp1-1)*temp1/2;
                result2=0;
                for(i=1;i<=n;i++){
                        scanf("%ld",&temp);
                        result2+=temp;
                }
                printf("%.0lf\n",result2-result1);
        }

}
因为C 的基础不扎实,所以之前写的程序都是通过调试得出原因,现在每次出现wrong answer 错误,很难知道问题出在哪里,因为没有case ,也无法调试。这是一个难点。不知到其他人是怎么做的?

后来参考了http://hi.baidu.com/casualzone/blog/item/c26ef50278b0d3703812bb20.html 的实现,才做出来。
注意这里不需要定义数组去存所有的数。

你可能感兴趣的:(ACM)