思路:
首先用set來記錄電話號碼中出現的數字,並用arr數組存儲起來。接着遍歷電話號碼中的各個數字,在arr中找到各個數字所在的位置。再按照對應的格式進行輸出
這裏還有道相似的題目,並附有set的詳細介紹和使用!
https://blog.csdn.net/weixin_43535668/article/details/104381291
#include<iostream>
#include<set>
#include<algorithm>
#include<string>
using namespace std;
int main()
{
char a[11];//電話號碼
set<char> s;//記錄電話號碼出現的數字
for (int i = 0; i < 11; i++)
{
cin >> a[i];
s.insert(a[i]);
}
int num = s.size();//記錄相異的數字個數
int arr[10];
int index[11];
int i =num-1;//下標記錄
set<char>::iterator it;
for (it=s.begin();it!=s.end();it++)//set的迭代器遍歷
{
arr[i] = *it - '0';
i--;
}
for (int i = 0; i < 11; i++)
{
for (int j = 0; j < num; j++)
{
if ((a[i] - '0') == arr[j])//找電話號碼每個數字在index中的位置
{
index[i] = j;
break;
}
}
}
//下面爲輸出格式設置
cout << "int[] arr = new int[]{";
for (int i = 0; i < num; i++)
{
cout << arr[i];
if (i != num - 1)
{
cout << ",";
}
}
cout << "};" << endl;
cout << "int[] index = new int[]{";
for (int i = 0; i < 11; i++)
{
cout << index[i];
if (i != 10)
{
cout << ",";
}
}
cout << "};";
system("pause");
return 0;
}