标签:public roc put 显示 format 算法竞赛入门经典 get str otherwise
#include<iostream> #include<iomanip> using namespace std; int main(void){ double s; cin>>s; if(s*95>=300){ cout<<setiosflags(ios::fixed)<<setprecision(2)<<s*95*0.85<<endl; } else{ cout<<setiosflags(ios::fixed)<<setprecision(2)<<s*95<<endl; } return 0; }
于是将C++中如何显示不同格式的小数查了一下:
#include <iostream> #include <iomanip> using namespace std; int main(void){ double S = 0.000000123; double A = 456.7; double B = 8910000000000.0; cout << "default precision is " << cout.precision() << endl; // 6 cout << " S = " << S << endl; // 1.23e-07 cout << " A = " << A << endl; // 456.7 cout << " B = " << B << endl; // 8.91e+12 cout << setiosflags(ios::showpoint) << "ios::showpoint ON" << endl; cout << " S = " << S << endl; // 1.23000e-07 cout << " A = " << A << endl; // 456.700 cout << " B = " << B << endl; // 8.91000e+12 cout << resetiosflags(ios::showpoint) << "ios::showpoint OFF" << endl; cout << setiosflags(ios::fixed) << "ios::fixed ON" << endl; cout << " S = " << S << endl; // 0.000000 cout << " A = " << A << endl; // 456.700000 cout << " B = " << B << endl; // 8910000000000.000000 cout << resetiosflags(ios::fixed) << setiosflags(ios::scientific) << "ios::scientific ON" << endl; cout << " S = " << S << endl; // 1.230000e-07 cout << " A = " << A << endl; // 4.567000e+02 cout << " B = " << B << endl; // 8.910000e+12 }
输出为:
default precision is 6
S = 1.23e-007
A = 456.7
B = 8.91e+012
ios::showpoint ON
S = 1.23000e-007
A = 456.700
B = 8.91000e+012
ios::showpoint OFF
ios::fixed ON
S = 0.000000
A = 456.700000
B = 8910000000000.000000
ios::scientific ON
S = 1.230000e-007
A = 4.567000e+002
B = 8.910000e+012
--------------------------------
Process exited after 0.02482 seconds with return value 0
请按任意键继续. . .
参考
https://www.cs.duke.edu/courses/cps149s/fall99/resources/n2.html
标签:public roc put 显示 format 算法竞赛入门经典 get str otherwise
原文地址:http://www.cnblogs.com/yizhaoAI/p/7029488.html