7-13 出租(20 分)
下面是新浪微博上曾經很火的一張圖:
一時間網上一片求救聲,急問這個怎麼破。其實這段代碼很簡單,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 "algorithm"
#include "set"
#include "vector"
#include "iostream"
#include "cstdio"
using namespace std;
const int maxn = 11 + 10;
int main() {
set<int, greater<int> > st;
vector<int> phone;
int tmp;
string str;
getline(cin, str);
for(int i = 0; i < str.length(); i++){
phone . push_back(str[i]-'0');
st.insert(str[i] - '0');
}
set<int>::iterator it = st . begin();
int index[maxn] = {0};
tmp = 0;
int tmpindex = *it;
index[tmpindex] = tmp++;
cout << "int[] arr = new int[]{" << *it++;
for (; it != st . end(); it++) {
cout << "," << *it;
tmpindex = *it;
index[tmpindex] = tmp++;
}
cout << "};" << endl;
cout << "int[] index = new int[]{" << index[phone[0]];
for (int i = 1; i < phone . size(); i++) {
cout << "," << index[phone[i]];
}
cout << "};" << endl;
return 0;
}