版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhaoshuling1109/article/details/82458248
19华为笔试 Code3
题目需求:解码(根据每个字母字符串后面的数字对其进行输出)先次数升序,后Asc码升序 。
输入:a11b2bac3bad3abcd2
输出:abcdabcdbbbacbacbacbadbadbadaaaaaaaaaaa
分析:排序后:abcd 2;b2;bac 3;bad 3;a 11;所以abcd输出2次,b输出2次.......等等;
涉及知识点:字符串分割、结构体排序、字符串转换为整型数字
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
//a11b2bac3bad3abcd2
//先次数升序,后Asc码升序
struct node{
string str1,str2;
int no;
}brr[100];
//是否是数字
int isNum(char c)
{
for(char j='1';j<='9';j++)
{
if(c==j)return 1;
}
}
//是否是字母
int isAlpa(char c)
{
return(c>='a'&&c<='z');
}
int main()
{
string str,arr[100];
cin>>str;
int temp=0,k=0;
//第一步:将字符串截取成 a11,b2,bac3,bad3,abcd2
for(int i=0;i<str.length();i++)
{
//temp=i;
if(isNum(str[i])&&isAlpa(str[i+1]))
{
//cout<<"i: "<<i;
//cout<<str.substr(temp,i+1-temp)<<endl;
arr[k]=str.substr(temp,i+1-temp);
temp=i+1;
k++;
}
}
//cout<<str.substr(temp,str.length()-temp)<<endl;
arr[k]=str.substr(temp,str.length()-temp);
//for(int i=0;i<=k;i++)
//cout<<arr[i]<<endl;
//第二步:将arr转换为结构体
//第三步:结构体中的数字字符串转化为整型数字
int p=0,temp1=0;
for(int i=0;i<=k;i++)
{
for(int j=0;j<arr[i].length();j++)
{
if(isAlpa(arr[i][j])&&isNum(arr[i][j+1]))
{
brr[p].str1=arr[i].substr(temp1,j+1-temp1);
//cout<<"temp1: "<<temp1<<" L: "<<brr[p].str1.length()<<endl;
brr[p].str2=arr[i].substr(temp1+brr[p].str1.length(),arr[i].length()-brr[p].str1.length());
//任意的字符类型转换
stringstream seam;//随意的什么类型都可以输入
seam<<brr[p].str2;//向流中传入值s 字符串型
seam>>brr[p].no;//向A中写入值A 整型
p++;
}
}
}
//for(int i=0;i<p;i++)
//cout<<brr[i].str1<<" "<<brr[i].str2<<" "<<brr[i].no<<endl;
//第四步:次数升序,次数相等则ASC码升序
struct node change;
for(int i=0;i<p-1;i++)
{
for(int j=i+1;j<p;j++)
{
if(brr[i].no>brr[j].no)
{
swap(brr[i],brr[j]);
}
else if(brr[i].no==brr[j].no)
{
if(brr[i].str1.compare(brr[j].str1)>0)//字符串的比较(字符串型)
{
swap(brr[i],brr[j]);
}
}
}
}
//for(int i=0;i<p;i++)
//cout<<brr[i].str1<<" "<<brr[i].str2<<" "<<brr[i].no<<endl;
//第五步:输出
for(int i=0;i<p;i++)
{
for(int j=0;j<brr[i].no;j++)
{
cout<<brr[i].str1;
}
}
}