WHCSRL 技术网

C++程设第一次作业 基本数据类型和运算符(碎碎念版)

1. 编写程序,读入圆柱体的半径和高,计算并输出圆柱体的表面积和体积。(对输出的格式要求比较高,调试好累。)

  1. #include <iostream>
  2. #include <iomanip> //头文件不能忘
  3. #include <cmath>
  4. using namespace std;
  5. int main()
  6. {
  7. const double pi=3.14;
  8. double r,h,s1,s2,s,v;
  9. cin >> r >> h;
  10. s1=pi*r*r;
  11. s2=2*pi*r*h; //说实话差点把公式忘了
  12. s=2*s1+s2;
  13. v=s1*h;
  14. cout<<setiosflags(ios::fixed); //这项设置后setprecision()才生效
  15. cout<<setprecision(2)<<s<<" "; //括号内数字表示保留的位数
  16. cout<<setprecision(2)<<v<<endl;
  17. return 0;
  18. }

2. 编写程序,读入一个三位整数,计算并输出三个数字之和。(总感觉它有更好的写法但是我不知道。)

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int a,b,c,d;
  6. cin >> a;
  7. b=a/100;
  8. c=(a-b*100)/10;
  9. d=a-b*100-c*10;
  10. cout << b+c+d << endl;
  11. return 0;
  12. }

3. 编写一个程序,输入一个字母,如果是小写,输出它的大写;如果是大写,输出它的小写。(利用条件运算符实现)(它是想为难我的ASCII水平对吧)

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. char a;
  6. cin>>a;
  7. if (a>=97){
  8. a=a-32;
  9. }
  10. else{
  11. a=a+32;
  12. }
  13. cout << a << endl;
  14. return 0;
  15. }

 4. 编写程序,读入3个整数a,b,c,交换他们的值,使a存放b的值,b存放c的值,c存放a的值。(呃......)

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int a,b,c,k;
  6. cin>>a>>b>>c;
  7. k=a;
  8. a=b;
  9. b=c;
  10. c=k;
  11. cout << a << " " << b <<" " << c << endl;
  12. return 0;
  13. }

5. 编写程序,读入秒数,计算秒数对应的时分秒(如读入3700秒,输出1h 1min 40s)(一个合格的数学问题)

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int h,min,s,k;
  6. cin>>k;
  7. h=k/3600;
  8. min=(k-h*3600)/60;
  9. s=k-h*3600-min*60;
  10. cout<<h<<"h "<<min<<"min "<<s<<"s "<<endl;
  11. return 0;
  12. }

推荐阅读