12.
#include //cin, cout所需的库
#include //setprecision()需要调用iomanip库
using namespace std;
int main()
{
int a, b;
cin >> a >> b;
cout << fixed << setprecision(3); //保留小数点3位
cout << 100.0 * b / a << '%'; //100.0* 强制将整数型数字转换为浮点数
return 0;
}
结果:
13.
#include
#include //setprecision()需要调用iomanip库
using namespace std;
int main()
{
double x, a, b, c, d; //声明double型变量x,a,b,c,d
cin >> x >> a >> b >> c >> d; //输入x,a,b,c,d的值
cout << fixed << setprecision(7); //保留小数点7位
cout << a * x * x * x + b * x * x + c * x + d;
return 0;
}
结果:
14.
#include
#include
using namespace std;
int main()
{
double F; //声明double型变量F
cin >> F;
cout << fixed << setprecision(5); //保留小数点5位
cout << 5 * (F - 32) / 9; //华氏度转摄氏度
return 0;
}
结果:
15.
#include
#include
using namespace std;
int main()
{
double PI = 3.14159, r; //声明double型变量PI和r, 并且PI是常量,值为3.14159
cin >> r;
cout << fixed << setprecision(4); //保留小数点4位
cout << r * 2 << " ";
cout << r * 2 * PI << " ";
cout << r * r * PI;
return 0;
}
结果:
16.
#include
#include
using namespace std;
int main()
{
double R, r1, r2; //声明double变量 R,r1,r2
cin >> r1 >> r2;
R = 1 / (1 / r1 + 1 / r2);
cout << fixed << setprecision(2) << R;
return 0;
}
结果:
17.
#include
using namespace std;
int main()
{
int a;
short b; //声明short型变量
cout << sizeof(a) << ' ' << sizeof b; //sizeof()函数用来计算变量的长度,结果表明整数型4,短型2
return 0;
}
结果:
18.
#include
using namespace std;
int main()
{
float a;
double b;
cout << sizeof(a) << ' ' << sizeof b; //结果显示浮点型长度为4,double型为8
return 0;
}
结果:
19.
#include
using namespace std;
int main()
{
bool a;
char b;
cout << sizeof(a) << ' ' << sizeof(b); //结果显示bool型长度为1,字符型为1
return 0;
}
结果:
20.
#include
using namespace std;
int main()
{
float a; //声明浮点型变量a
cin >> a;
cout << int(a); //将变量a转换为整型
return 0;
}
结果:
21.
#include
using namespace std;
int main()
{
char c = cin.get(); //cin.get()用来输入一个字符 char c 声明字符变量c
cout << int(c); //将变量c转换为整数型
return 0;
}
结果:
22.
#include
using namespace std;
int main()
{
int n;
cin >> n;
cout << char(n); //将变量n转换为字符变量
return 0;
}
结果:
23.
#include
using namespace std;
int main()
{
int n;
cin >> n; //如 6
bool b = n; //将n的值赋予bool变量b 1 bool型的值只有两个0和1
n = b; //将b的值赋予变量n 1
cout << n; //输出n的值
return 0;
}
结果:
24.
#include
using namespace std;
int main()
{
cout << sizeof("Hello, World!"); //计算字符串Hello,World!的长度
return 0;
}
结果:
留言与评论(共有 0 条评论) “” |