question
You are given N numbers (N≤100), each number is between (0∼1000), and there are many repeated numbers. Please keep only one repeated number, and arrange the remaining numbers from small to large. Sort and output.
1. Analyze the problem
- Known: N numbers
- Unknown: output sequence
- Relationship: Keep only one repeated number, and sort the remaining numbers from small to large
2. Define variables
x: The currently entered number.
count: The actual number of inputs.
//二、数据定义
int a[1001]={
0},n,x,count=0;
3. Enter data
Since the read elements are between 0 and 1000, we prepare an array with a length of 1001 to mark whether x has appeared.
//三、数据输入
cin>>n;
for(int i=0;i<n;i++){
cin>>x;
if(0==a[x]){
++count;
}
a[x]=1;
}
4.Data calculation
5. Output results
We only output the number with a value of 1 in the array. Note that we need to output its subscript, not its value.
#include<iostream>
using namespace std;
int main(){
//一、分析问题
//已知:N个数
//未知:输出数列
//关系:重复的数字只保留一个,并将剩下的数由小到大排序
//二、数据定义
int a[1001]={
0},n,x,count=0;
//三、数据输入
cin>>n;
for(int i=0;i<n;i++){
cin>>x;
if(0==a[x]){
++count;
}
a[x]=1;
}
//四、数据计算
//五、输出结果
cout<<count<<endl;
for(int i=0;i<1001;i++){
if(1==a[i]){
cout<<i<<endl;
}
}
return 0;
}