L1-027 出租
下面是新浪微博上曾经很火的一张图:

一时间网上一片求救声,急问这个怎么破。其实这段代码很简单,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};
解题思路
- 用map存取电话号码中出现的数字,因为map本身对key字典序排序,即存入的数字升序排序,所以不用再排序
- 两个for循环遍历原号码和arr中的数字,记录位置存入vector index中
- 因为map中数字是升序排列的,所以可以用
reverse_iterator反向遍历map - 具体操作见代码
附上代码
#include<bits/stdc++.h>
#define int long long
#define lowbit(x) (x &(-x))
using namespace std;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const double eps=1e-10;
const int M=1e9+7;
const int N=1e5+5;
typedef long long ll;
typedef pair<int,int> PII;
string s;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>s;
map<char,int> mp;
map<char,int>::reverse_iterator it;
vector<int> index;
for(int i=0;i<11;i++)
mp[s[i]]=1;
int j;
for(int i=0;i<11;i++){
for(it=mp.rbegin(),j=0;it!=mp.rend();it++,j++){
//j用来模拟记录数字在arr数组的下标
if(s[i]==it->first)
index.push_back(j);
}
}
cout<<"int[] arr = new int[]{";
for(it=mp.rbegin();it!=mp.rend();it++){
if(it!=mp.rbegin())
cout<<","<<it->first;
else
cout<<it->first;
}
cout<<"};"<<endl;
cout<<"int[] index = new int[]{";
for(int i=0;i<11;i++){
if(i!=0)
cout<<","<<index[i];
else
cout<<index[i];
}
cout<<"};";
return 0;
}
本文介绍了一种将电话号码转换为特定编码格式的方法,通过使用C++编程语言和标准库中的map和vector,实现了对输入的11位手机号码生成对应的代码格式。文章详细解释了代码实现过程,包括如何利用map存储电话号码中出现的数字,并通过双for循环和reverse_iterator进行位置记录和数字排序。
464

被折叠的 条评论
为什么被折叠?



