7-6 出租 (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};
AC
参考链接:
#include<bits/stdc++.h>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
string s;
cin>>s;
int a[10]= {
0};
for(int i=0; i<11; i++) {
a[s[i]-'0']++;
}
vector<int> v1,v2;
for(int i=9; i>=0; i--) {
if(a[i]!=0)
v1.push_back(i);
}
for(int i=0; i<11; i++) {
for(int j=0; j<v1.size(); j++) {
if(s[i]-'0'==v1[j])
v2.push_back(j);
}
}
cout<<"int[] arr = new int[]{"<<v1[0];
for(int i=1; i<v1.size(); i++)
cout<<","<<v1[i];
cout<<"};"<<endl;
cout<<"int[] index = new int[]{"<<v2[0];
for(int i=1; i<v2.size(); i++)
cout<<","<<v2[i];
cout<<"};";
return 0;
}
有点问题的方法
#include<bits/stdc++.h>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
string s;
cin>>s;
int a[10]= {
0},aa[10];
for(int i=0; i<11; i++)
a[s[i]-'0']++;
int p=0;
for(int i=9; i>=0; i--) {
if(a[i]!=0) aa[p++]=i;
}
cout<<"int[] arr = new int[]{"<<aa[0];
for(int i=1; i<p; i++) {
cout<<","<<aa[i];
}
cout<<"};"<<endl;
int b[11]= {
0};
for(int i=0; i<11; i++) {
for(int j=0; j<p; j++) {
if(s[i]-'0'==aa[j]) {
b[i]=j;
}
}
}
cout<<"int[] index = new int[]{"<<b[0];
for(int i=0; i<11; i++)
cout<<","<<b[i];
cout<<"};";
return 0;
}