题略
用例:
输入:725
输出:BBBBBBBSS12345
功能:百位上数是7,用7个字母“B”表示,十位上的数是2,用2个字母“S”表示,个位上的数字是5,用12345表示
对于这种题型,我们第一反应就是求模取余,比如输入725,那么
百位上的数7=725 / 100
十位上的数2=(725 % 100) / 10
个位上的数5=(725 % 100)% 10
我们创建一个函数,函数的参数分别为百位上的数h,十位上的数d,个位上的数u
void transform(int h,int d,int u) {
for(int i = 0;i < h;i++)
cout<<'B';
for(int j = 0;j < d;j++)
cout<<'S';
for(int k = 1;k <= u;k++)
cout<<k;
cout<<endl;
}
此时,我们就可以在主函数中调用了。
int main(){
int n,h,d,u;
cin>>n;
h = n / 100;
d = n % 100 / 10;
u = n % 100 % 10;
transform(h,d,u);
}
完整的代码如下
#include<iostream>
using namespace std;
void transform(int h,int d,int u) {
for(int i = 0;i < h;i++)
cout<<'B';
for(int j = 0;j < d;j++)
cout<<'S';
for(int k = 1;k <= u;k++)
cout<<k;
cout<<endl;
}
int main(){
int n,h,d,u;
cin>>n;
h = n / 100;
d = n % 100 / 10;
u = n % 100 % 10;
transform(h,d,u);
}