7-3 出租 (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 <iostream> #include<bits/stdc++.h> using namespace std; int main() { string a; cin>>a; int len = a.size(); vector<int>o; int book[11]= {0}; for(int i=0; i<len; i++) { int p = a[i]-'0'; if(!book[p]) { o.push_back(p); book[p] = 1; } } map<int,int>pp; sort(o.begin(),o.end(),greater<int>()); int ll = o.size(); for(int i=0; i<ll; i++) { pp[o[i]] = i; } printf("int[] arr = new int[]{"); int top = 1; for(int k:o) { if(top)top = 0; else printf(","); cout<<k; } printf("};\n"); top = 1; printf("int[] index = new int[]{"); for(int i=0; i<len; i++) { if(top)top = 0; else printf(","); int uu = a[i]-'0'; cout<<pp[uu]; } printf("};"); return 0; }