一道acm入门题想知道为什么TLE了

siberia0015 2017-07-31 07:16:54
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

就是先输一个数字确定所输入的数据的个数,然后输入数据,输入数据中要有两个一样的数,输出的结果是这个有重复的数字

#include <stdio.h>
#include <stdlib.h>

int main()
{
int n,i,f,j=0;
while(scanf("%d",&n)!=EOF){
int *a = (int*)malloc(n*sizeof(int));
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
for(i=0;i<n;i++){
for(f=i+1;f<n;f++){
if(a[i]==a[f]){
j=a[i];
}
}
}
printf("%d",j);
free(a);
}
return 0;
}
...全文
310 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
siberia0015 2017-08-05
  • 打赏
  • 举报
回复
引用 3 楼 mLee79 的回复:
正常的代码应该是 O(N) 的, 你这是 O(N^2)的, 能不超时么。
我大概理解您的意思了,谢谢
mLee79 2017-08-02
  • 打赏
  • 举报
回复
正常的代码应该是 O(N) 的, 你这是 O(N^2)的, 能不超时么。
jena_wy 2017-08-01
  • 打赏
  • 举报
回复
n的初始值是0
siberia0015 2017-08-01
  • 打赏
  • 举报
回复
引用 1 楼 wyyy2088511的回复:
n的初始值是0
请问是应该把n的初始值设为0,是吗?是因为什么导致超时的呢?

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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