hdu 6297(常用的输出格式总结)
2024-08-24 02:47:41
题目链接: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;
最新文章
- 了解PHP中的Array数组和foreach
- SpringMvc中的数据校验
- C#并行
- 评论Final版本发布
- SQL Server 中 with tmp 临时表的用法
- Mysql 允许null 与 default值
- POJ 3311 Hie with the Pie(Floyd+状态压缩DP)
- sublime3配置Quick-X+自动错误提示
- js判断IE6(推荐方法一)
- python-setuptool安装
- python发送html格式的邮件
- 关于ios 11 的问题
- Win7 IIS7 HTTP 错误 404.2 - Not Found解决方法 ISAPI CGI
- gcc中C++一个特别的头文件
- linux下搭建LAMP
- ACM-ICPC (10/15) Codeforces Round #440 (Div. 2, based on Technocup 2018 Elimination Round 2)
- 通过Eclipse生成可运行的jar包
- JdbcUtils 小工具
- MySQL:习题(单表多条件查询二)
- 51nod1212【最小生成树kruskal算法】
热门文章
- JDBC MySQL 实例之 用户管理系统
- opencv3更换图片背景
- IFC数据模型在三维引擎中模拟
- Effective Objective-C [下]
- Python程序设计8——网络编程
- sed编辑器使用(转)
- Thread.currentThread().getContextClassLoader() 和 Class.getClassLoader()区别
- VS2012,更新补丁后--创建项目未找到与约束匹配的导出
- wampserver2.5 在 Win7 64位下的相关配置备忘
- Note: Transparent data deduplication in the cloud