它們添加到端我在data.txt
文件等9 3 9 4 5 4 3 7 1 9 6從數組中刪除重複的數字,並在C++
陣列我需要找到重複的數字,並從刪除它們陣列。
之後,我需要收集他們在數組的末尾。
我寫了一個代碼,輸出是9 3 4 5 7 1 6 9 3 4 9,但我需要將重複的數字放在數組中,按照它們出現在原始數組中的順序。
所以我需要得到{ 9, 3, 4, 5, 7, 1, 6, 9, 4, 3, 9 }
作爲輸出。
如何使用代碼實現我的目標?
#include <iostream>
#include <fstream>
using namespace std;
#define SZ 11
int main(){
ifstream fs("data.txt");
if (!fs)
return 0;
int a[SZ];
for (int i = 0; i < SZ; ++i)
fs >> a[i];
for (int k=0; k<SZ; k++) {
for (int j=k+1; j< SZ ; j++) {
if (a[j]==a[k]) {
for (int l=j; l<SZ-1; l++) {
a[l]=a[l+1];
}
a[10]=a[k];
}
}
}
for (int i = 0; i < SZ; ++i)
cout << a[i];
return 1;}
我不假設'std :: sort'數組,然後在其上運行'std :: unique'是一個選項。 – WhozCraig 2014-09-29 21:58:01
我期望它的功課,所以一切都必須寫成它的C代碼,而不是使用適當的C++習慣用法。 – 2014-09-29 22:03:09
您是否需要爲您的示例輸入獲取「{9,3,4,5,7,1,6}」? – 2014-09-29 22:11:18