%*s 或 %*d在 c++ 输出的作用
#include<bits/stdc++.h>
using namespace std;
int main(){
printf("%*d\n",5,101);
return 0;
}
%*d
要求输出是整数型,比如上述代码中,输出的 101 前面有两个空格。
%*d
的意思是,输出特定的数字或字符串,要求字符的总长度为多少。
#include<bits/stdc++.h>
using namespace std;
int main(){
printf("%*s\n",5,"abc");
return 0;
}
指定的长度也可以通过外界变量来获取。
#include<bits/stdc++.h>
using namespace std;
int main(){
int m=5;
printf("%*s\n",m,"abc");
return 0;
}
蓝桥杯的出现这种用法的例题:
试题 算法训练 猴子吃包子