让我们用字母 B 来表示“百”、字母 S 表示“十”,用 12…n 来表示不为零的个位数字 n(<10),换个格式来输出任一个不超过 3 位的正整数。例如 234 应该被输出为 BBSSS1234,因为它有 2 个“百”、3 个“十”、以及个位的 4。
输入格式:
每个测试输入包含 1 个测试用例,给出正整数 n(<1000)。
输出格式:
每个测试用例的输出占一行,用规定的格式输出 n。
输入样例 1:
234
输出样例 1:
BBSSS1234
输入样例 2:
23
输出样例 2:
SS123
第一种方法,定义数组依次求余算个数,比较基础好理解,但是比较麻烦,可供参考
#include <iostream>
#include<map>
#include<string>
#include<set>
#include<stack>
#include<queue>
#include<algorithm>
#include<unordered_set>
using namespace std;
int main() {
int n;
cin>>n;
int count=0;
int a[3]={0};//定义一个数组
int i=0;
while(n){
a[i++]=n%10;//求余,最后从后往前就是百十个位
n/=10;
count++;
}
if(count==3){
for(int j=0;j<a[2];j++){
cout<<"B";
}
for(int j=0;j<a[1];j++){
cout<<"S";
}
for(int j=1;j<=a[0];j++){
cout<<j;
}
}
else if(count==2){
for(int j=0;j<a[1];j++){
cout<<"S";
}
for(int j=1;j<=a[0];j++){
cout<<j;
}
}
else if(count==1){
for(int j=1;j<=a[0];j++){
cout<<j;
}
}
return 0;
}
第二种方法,运用字符串,由于字符串的size可以算出有多少位,所以直接写就行,string更简单
#include <iostream>
#include<map>
#include<string>
#include<set>
#include<stack>
#include<queue>
#include<algorithm>
#include<unordered_set>
using namespace std;
int main() {
string s;
cin>>s;
int len=s.size();
if(len==3){
for(int j=0;j<s[0]-'0';j++){
cout<<"B";
}
for(int j=0;j<s[1]-'0';j++){
cout<<"S";
}
for(int j=1;j<=s[2]-'0';j++){
cout<<j;
}
}
else if(len==2){
for(int j=0;j<s[0]-'0';j++){
cout<<"S";
}
for(int j=1;j<=s[1]-'0';j++){
cout<<j;
}
}
else{
for(int j=1;j<=s[0]-'0';j++){
cout<<j;
}
}
return 0;
}