标签:识别 stream int cst flag setfill 一个 格式 ace
题目链接:https://cn.vjudge.net/problem/HDU-6297
题目介绍:一道关于输出格式规范问题
wrong answer代码:
#include<iostream> #include<cstdio> #include<string.h> #include<string> using namespace std; int main() { int T; cin>>T; int rank; string str; int prob; string Tstr; while(T--) { int n=0; cin>>rank>>str>>prob>>Tstr; if(Tstr[0]==‘R‘) cin>>n; printf("%3d|",rank); int mylen=str.length(); cout<<str; for(int i=mylen;i<=15;i++) cout<<" "; printf("|%d|[",prob); if(Tstr[0]==‘R‘) { for(int i=1;i<=n;i++) cout<<"X"; for(int i=n;i<=10;i++) cout<<" "; cout<<"]"<<endl; } else { int length=Tstr.length(); for(int i=1;i<=4;i++) cout<<" "; if(Tstr[0]==‘F‘&&Tstr[1]==‘B‘) { length=3; cout<<"AC*"; } else cout<<Tstr; for(int i=length+4;i<=10;i++) cout<<" "; cout<<"]"<<endl; } } return 0; }
错误点:
1.识别Tstr字符串是否为“Running"时,只以首字母标记(Tstr[0]==‘R‘),导致识别资料不足,可以识别“RTE"与”Running",识别混乱,所以改成(Tstr==”Running")
2.与输出样式相比,末尾多输出了一个空格
ac代码:
#include<iostream> #include<cstdio> #include<string.h> #include<string> using namespace std; int main() { int T; cin>>T; int rank; string str; int prob; string Tstr; while(T--) { int n=0; cin>>rank>>str>>prob>>Tstr; if(Tstr=="Running") cin>>n; printf("%3d|",rank); int mylen=str.length(); cout<<str; for(int i=mylen;i<=15;i++) cout<<" "; printf("|%d|[",prob); if(Tstr=="Running") { for(int i=1;i<=n;i++) cout<<"X"; for(int i=n;i<10;i++) cout<<" "; cout<<"]"<<endl; } else { int length=Tstr.length(); for(int i=1;i<=4;i++) cout<<" "; if(Tstr[0]==‘F‘&&Tstr[1]==‘B‘) { length=3; cout<<"AC*"; } else cout<<Tstr; for(int i=length+4;i<10;i++) cout<<" "; cout<<"]"<<endl; } } return 0; }
接下来,我们来学习一下常用的输出格式:
头文件:#include<iomanip>
以下是一些常用的: dec 置基数为10 相当于"%d" hex 置基数为16 相当于"%X" oct 置基数为8 相当于"%o" setfill(c) 设填充字符为c setprecision(n) 设显示小数精度为n位 setw(n) 设域宽为n个字符 setioflags(ios::fixed) 固定的浮点显示 setioflags(ios::scientific) 指数表示 setiosflags(ios::left) 左对齐 setiosflags(ios::right) 右对齐 setiosflags(ios::skipws 忽略前导空白 setiosflags(ios::uppercase) 16进制数大写输出 setiosflags(ios::lowercase) 16进制小写输出 setiosflags(ios::showpoint) 强制显示小数点 setiosflags(ios::showpos) 强制显示符号
代码举例:
19|qqqqq_University|1001|[XXX ] 125|quailty_U_2 |1002|[ WA ] 4|quailty_U_3 |1003|[ TLE ]
比如上面的那道题,需要用到输出时每个数据是 右对齐3 左对齐16 4 10
像第一个数据需要右对齐:cout<<right<<setw(3)<<Rank<<"|";
第二个数据需要左对齐:cout<<left<<setw(16)<<name<<"|";
第三个数据左对齐:cout<<left<<setw(4)<<pro<<"|[";
第四个需要分类:
针对下面两个的类型:输出字符串,但字符串是居中的(我们可以这样处理:先输出四个空字符,然后对字符串进行左对齐:cout<<" "<<setw(6)<<status<<"]"<<endl;
标签:识别 stream int cst flag setfill 一个 格式 ace
原文地址:https://www.cnblogs.com/Aiahtwo/p/10494611.html