题目链接: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=;
cin>>rank>>str>>prob>>Tstr;
if(Tstr[]=='R')
cin>>n;
printf("%3d|",rank);
int mylen=str.length();
cout<<str;
for(int i=mylen;i<=;i++)
cout<<" ";
printf("|%d|[",prob);
if(Tstr[]=='R')
{
for(int i=;i<=n;i++)
cout<<"X";
for(int i=n;i<=;i++)
cout<<" ";
cout<<"]"<<endl;
}
else
{
int length=Tstr.length();
for(int i=;i<=;i++)
cout<<" ";
if(Tstr[]=='F'&&Tstr[]=='B')
{
length=;
cout<<"AC*";
}
else
cout<<Tstr;
for(int i=length+;i<=;i++)
cout<<" ";
cout<<"]"<<endl; }
}
return ;
}

错误点:

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=;
cin>>rank>>str>>prob>>Tstr;
if(Tstr=="Running")
cin>>n;
printf("%3d|",rank);
int mylen=str.length();
cout<<str;
for(int i=mylen;i<=;i++)
cout<<" ";
printf("|%d|[",prob);
if(Tstr=="Running")
{
for(int i=;i<=n;i++)
cout<<"X";
for(int i=n;i<;i++)
cout<<" ";
cout<<"]"<<endl;
}
else
{
int length=Tstr.length();
for(int i=;i<=;i++)
cout<<" ";
if(Tstr[]=='F'&&Tstr[]=='B')
{
length=;
cout<<"AC*";
}
else
cout<<Tstr;
for(int i=length+;i<;i++)
cout<<" ";
cout<<"]"<<endl;
}
}
return ;
}

接下来,我们来学习一下常用的输出格式:

头文件:#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;

最新文章

  1. 了解PHP中的Array数组和foreach
  2. SpringMvc中的数据校验
  3. C#并行
  4. 评论Final版本发布
  5. SQL Server 中 with tmp 临时表的用法
  6. Mysql 允许null 与 default值
  7. POJ 3311 Hie with the Pie(Floyd+状态压缩DP)
  8. sublime3配置Quick-X+自动错误提示
  9. js判断IE6(推荐方法一)
  10. python-setuptool安装
  11. python发送html格式的邮件
  12. 关于ios 11 的问题
  13. Win7 IIS7 HTTP 错误 404.2 - Not Found解决方法 ISAPI CGI
  14. gcc中C++一个特别的头文件
  15. linux下搭建LAMP
  16. ACM-ICPC (10/15) Codeforces Round #440 (Div. 2, based on Technocup 2018 Elimination Round 2)
  17. 通过Eclipse生成可运行的jar包
  18. JdbcUtils 小工具
  19. MySQL:习题(单表多条件查询二)
  20. 51nod1212【最小生成树kruskal算法】

热门文章

  1. JDBC MySQL 实例之 用户管理系统
  2. opencv3更换图片背景
  3. IFC数据模型在三维引擎中模拟
  4. Effective Objective-C [下]
  5. Python程序设计8——网络编程
  6. sed编辑器使用(转)
  7. Thread.currentThread().getContextClassLoader() 和 Class.getClassLoader()区别
  8. VS2012,更新补丁后--创建项目未找到与约束匹配的导出
  9. wampserver2.5 在 Win7 64位下的相关配置备忘
  10. Note: Transparent data deduplication in the cloud