21xrx.com
2025-03-28 15:04:15 Friday
文章检索 我的文章 写文章
C++第三章习题答案
2023-07-05 04:58:09 深夜i     8     0
C++ 第三章 习题 答案 编程语言

本人热爱编程,最近学习了C++第三章,因此整理了一份习题答案,希望对各位同学有所帮助。

1. 编写一个程序,要求输出Windows使用者想打印的份数,然后使用循环语句输出该数字的2次方、3次方、4次方。

#include <iostream>
using namespace std;
int main() {
  int num;
  cout << "请输入要打印的数字数量: ";
  cin >> num;
  for(int i = 1; i <= num; i++) {
    cout << i << " " << i*i << " " << i*i*i << " " << i*i*i*i << endl;
  }
  return 0;
}

2. 编写一个程序,输出一个立方的体积。将所有的计算和输出都放在一个语句中,使用一个cout语句同时输出所有的结果。

#include <iostream>
using namespace std;
int main() {
  const double edge = 3.0;
  cout << "The volume of a cube with edge of " << edge << " is " << edge * edge * edge << "." << endl;
  return 0;
}

3. 编写一个程序,让用户输入年龄,然后输出该年龄包含的月数和天数。

#include <iostream>
using namespace std;
int main() {
  int age;
  cout << "请输入您的年龄: ";
  cin >> age;
  int months = age * 12;
  int days = age * 365;
  cout << age << "岁包含了 " << months << " 个月和 " << days << " 天。" << endl;
  return 0;
}

4. 编写一个程序,将华氏温度转换为摄氏温度。公式为c = (f - 32) / 1.8。

#include <iostream>
using namespace std;
int main() {
  double fahrenheit;
  cout << "请输入华氏温度: ";
  cin >> fahrenheit;
  double celsius = (fahrenheit - 32) / 1.8;
  cout << fahrenheit << "℉ = " << celsius << "℃" << endl;
  return 0;
}

5. 编写一个程序,用循环嵌套打印出下面图形:

*
**
***
****
*****

#include <iostream>
using namespace std;
int main() {
  for(int i = 1; i <= 5; i++) {
    for(int j = 1; j <= i; j++) {
      cout << "*";
    }
    cout << endl;
  }
  return 0;
}

  
  

评论区