C++程设第一次作业 基本数据类型和运算符(碎碎念版)
1. 编写程序,读入圆柱体的半径和高,计算并输出圆柱体的表面积和体积。(对输出的格式要求比较高,调试好累。)
- #include <iostream>
- #include <iomanip> //头文件不能忘
- #include <cmath>
- using namespace std;
- int main()
- {
- const double pi=3.14;
- double r,h,s1,s2,s,v;
- cin >> r >> h;
- s1=pi*r*r;
- s2=2*pi*r*h; //说实话差点把公式忘了
- s=2*s1+s2;
- v=s1*h;
- cout<<setiosflags(ios::fixed); //这项设置后setprecision()才生效
- cout<<setprecision(2)<<s<<" "; //括号内数字表示保留的位数
- cout<<setprecision(2)<<v<<endl;
- return 0;
- }
2. 编写程序,读入一个三位整数,计算并输出三个数字之和。(总感觉它有更好的写法但是我不知道。)
- #include <iostream>
- using namespace std;
-
- int main()
- {
- int a,b,c,d;
- cin >> a;
- b=a/100;
- c=(a-b*100)/10;
- d=a-b*100-c*10;
- cout << b+c+d << endl;
- return 0;
- }
3. 编写一个程序,输入一个字母,如果是小写,输出它的大写;如果是大写,输出它的小写。(利用条件运算符实现)(它是想为难我的ASCII水平对吧)
- #include <iostream>
- using namespace std;
-
- int main()
- {
- char a;
- cin>>a;
- if (a>=97){
- a=a-32;
- }
- else{
- a=a+32;
- }
- cout << a << endl;
- return 0;
- }
4. 编写程序,读入3个整数a,b,c,交换他们的值,使a存放b的值,b存放c的值,c存放a的值。(呃......)
- #include <iostream>
- using namespace std;
-
- int main()
- {
- int a,b,c,k;
- cin>>a>>b>>c;
- k=a;
- a=b;
- b=c;
- c=k;
- cout << a << " " << b <<" " << c << endl;
- return 0;
- }
5. 编写程序,读入秒数,计算秒数对应的时分秒(如读入3700秒,输出1h 1min 40s)(一个合格的数学问题)
- #include <iostream>
- using namespace std;
-
- int main()
- {
- int h,min,s,k;
- cin>>k;
- h=k/3600;
- min=(k-h*3600)/60;
- s=k-h*3600-min*60;
- cout<<h<<"h "<<min<<"min "<<s<<"s "<<endl;
- return 0;
- }
推荐阅读