一時間網上一片求救聲,急問這個怎麼破。其實這段代碼很簡單,index數組就是arr數組的下標,index[0]=2 對應 arr[2]=1,index[1]=0 對應 arr[0]=8,index[2]=3 對應 arr[3]=0,以此類推…… 很容易得到電話號碼是18013820100。
本題要求你編寫一個程序,爲任何一個電話號碼生成這段代碼 —— 事實上,只要生成最前面兩行就可以了,後面內容是不變的。
輸入格式:
輸入在一行中給出一個由11位數字組成的手機號碼。
輸出格式:
爲輸入的號碼生成代碼的前兩行,其中arr中的數字必須按遞減順序給出。
輸入樣例:
18013820100
輸出樣例:
int[] arr = new int[]{8,3,2,1,0};
int[] index = new int[]{3,0,4,3,1,0,2,4,3,4,4};
#include <iostream>
#include <string>
#include <set>
#include <array>
#include <algorithm>
using namespace std;
int main()
{
set<int>s;
string num;
int a[11];
int flag = 0;
cin >> num;
for (int i = 0; i < 11; i++)
s.insert(num[i]-'0');
//for (auto i=s.rbegin();i!=s.rend();i++)
//cout << *i;
cout << "int[] arr = new int[]{";
for (auto i = s.rbegin(); i != s.rend(); i++)
{
a[*i] =flag ;
flag++;
if (i == s.rbegin())cout << *i;
else
cout <<','<< *i;
}
cout << "};" << endl;
cout << "int[] index = new int[]{";
for (int i = 0; i < 11; i++) {
if (i == 0)cout << a[num[i]-'0'];
else
cout << ',' << a[num[i]-'0'];
}
cout << "};";
system("pause");
}
知識點,rebegin and rend,it’s still from rebegin to the rend,but you should make it ++ and it will form the end to the begin;