`
naouguhtaeyeti
  • 浏览: 19936 次
文章分类
社区版块
存档分类
最新评论

FOJ1001

    博客分类:
  • ACM
ACM 
阅读更多
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 的实现,才做出来。
注意这里不需要定义数组去存所有的数。
分享到:
评论

相关推荐

    FOj部分水题AC答案

    代Un的还没被Ac,其余不保证算法够好,只是随便传传

    FOJ.1207.zip_26.2_了然foj

    给定一个自然数n,由n开始可以依次产生半数集set(n)中的数如下。 (1)n∈set(n); (2)在n的左边加上一个自然数,但该自然数不能超过最近添加的数的一半; (3)按此规则进行处理,直到不能再添加自然数为止。...

    FOJ(大部分标程) ACM

    FOJ(大部分标程) ACMFOJ(大部分标程) ACMFOJ(大部分标程) ACMFOJ(大部分标程) ACMFOJ(大部分标程) ACMFOJ(大部分标程) ACM

    FOJ 1150 Peter's smokes

    第一次上传东西... 本人是个初学者,希望大家多多指教

    foj.rar_On the Line_meet62l_pick8xd_界面编程

    Source code example for shortcuts to create arbitrary files on the command line

Global site tag (gtag.js) - Google Analytics