本文主要是介绍PAT 天梯赛 L1-027. 出租 (简单字符串处理),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目链接
#include<iostream>
#include<string>
#define MAX 10
using namespace std;
int book[MAX];
int ser(char c, string a){int i;for(i = 0; i < a.length(); ++i){if(a[i] == c)return i;}return -1;
}
int main(){string str, a, b;cin >> str;a = "";b = "";for(int i = 0; i < str.length(); ++i)book[str[i]-'0']++;for(int i = 9; i >= 0; --i){//从大到小获得 arr数组 if(book[i] != 0){a += char(i+'0'); }}int t;for(int i = 0; i < 10; ++i){ //index数组就是 查找str某位数字在arr中的索引 t = ser(str[i], a);b = b + char(t+'0')+",";}t = ser(str[10],a);b = b+ char(t+'0') + "};";b = "int[] index = new int[]{"+b ;string c = "int[] arr = new int[]{";for(int i = 0; i < a.length()-1; ++i)c = c + a[i] + ",";c = c + a[a.length()-1] + "};"; cout << c<<endl;cout << b ;return 0;}
这篇关于PAT 天梯赛 L1-027. 出租 (简单字符串处理)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!