L1-032 Left-pad (20分)
题目详情:
AC代码:
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int n,len;
char c;
string str;
cin>>n>>c;
cin.ignore();//吸收输入数字和字符之后的换行符
getline(cin,str);
len=str.size();
if(n>len)
cout<<setw(n)<<setfill(c)<<str<<endl;
else
cout<<str.substr(len-n)<<endl;//输出str[len-n]~str[len-1]之间的字符串
return 0;
}
substr()的两种用法:
- string s=“12345678”;
- string str=s.substr(3);//从下标3开始一直到结尾。str=“45678”。
- string str=s.substr(3,4);//从下标3开始截取长度为4的字符串。str=“4567”。
永远相信美好🎈