2017-04-05 45 views
-1

链接到有问题的问题:http://codeforces.com/problemset/problem/784/F这个Codeforces挑战784F需要做什么改变?

这只是一个排序问题,所以我用我的数组冒泡排序。但是当我提交我的答案时,即使它在我运行时运行得很好,它仍会拒绝测试1。我确信我为我的代码选择了正确的编译器,所以这不是问题。我的代码有问题吗?

这里是我的代码:

int arr[10]; 

/* number of inputs */ 
int n; 
cin >> n; 

/* inputting n numbers to the array */ 
for (int i = 0; i < n; ++i) 
    cin >> arr[i]; 

/* bubble sort array */ 
for (int i = 0; i < n - 1; ++i) { 
    for (int j = 0; j < n - i - 1; ++j) { 
     if (arr[j] > arr[j+1]) { 
      int temp = arr[j]; 
      arr[j] = arr[j+1]; 
      arr[j + 1] = temp; 
     } 
    } 
} 

/* printing all the numbers in array */ 
for (int i = 0; i < n; ++i) { 
    cout << arr[i] << ' '; 
} 
cout << endl; 
+1

如果'N'的值设置为大于10,会发生什么?提示:你的数组被定义为'int arr [10]' – Jonas

+1

除此之外,如果代码中存在时间限制等,为什么要进行冒泡排序呢? “我确定我选择了正确的编译器”是什么意思? – deviantfan

+1

看着这个网站,似乎不仅需要对数字进行排序,而且还必须删除任何重复数据: 输入:3 3 1 2 输出:1 2 3 – Jonas

回答

0

你并不需要实现一切你的自我,只是使用标准库:

// number of inputs 
int n; 
std::cin >> n; 

// Vector storing the numbers 
std::vector<int> v(n); 

// Input n numbers 
for (int i = 0; i < n; ++i) 
{ 
    std::cin >> arr[i]; 
} 

// Sort the numbers 
std::sort(v.begin(), v.end()); 

// Removed all duplicates (all numbers are unique) 
auto last = std::unique(v.begin(), v.end()); 
v.erase(last, v.end()); 

// Print all the numbers 
for (int i : v) 
    std::cout << i << ' '; 
std::cout << std::endl;