L1-042 日期格式化 (5分)
题目详情:

①简单粗暴:
#include<iostream>
using namespace std;
int main()
{
string str;
cin>>str;
cout<<str[6]<<str[7]<<str[8]<<str[9]<<str[5]<<str[0]<<str[1]<<str[2]<<str[3]<<str[4]<<endl;
return 0;
}
运行结果:

②另解:
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int a,b,c;
cin>>a;
cin.ignore();
cin>>b;
cin.ignore();
cin>>c;
cout<<setw(4)<<setfill('0')<<c<<'-'<<setw(2)<<a<<'-'<<setw(2)<<b;
return 0;
}
运行结果:

相关链接:C++中的cin.ignore()函数
永远相信美好🎈
本文介绍了两种在C++中进行日期格式化的有效方法。第一种是直接通过字符串操作实现,第二种利用cin.ignore()和iomanip库函数setw()、setfill()进行格式调整,输出为YYYY-MM-DD形式。

715

被折叠的 条评论
为什么被折叠?



