问题:
解答:
#include <iostream>
using namespace std;
#define MONTHSCOUNT 12
#define YEARS 3
int main()
{
string months[MONTHSCOUNT] = { "January","February","March","April","May","June",
"July","August","September","October","November","December" };
int sales[YEARS][MONTHSCOUNT] = { 0 };
int sum[3] = { 0 };
for (int j = 0; j < YEARS; j++)
{
for (int i = 0; i < MONTHSCOUNT; i++)
{
cout << "请输入第"<<j+1<<"年,第"<< i + 1 << "月的图书销量:";
cin >> sales[j][i];
sum[j] += sales[j][i];
}
}
for (int j = 0; j < YEARS; j++)
{
cout << "第" << j + 1 << "年每月的销量如下:" << endl;
for (int i = 0; i < MONTHSCOUNT; i++)
{
cout << months[i] << "的销量为:" << sales[j][i] << endl;
}
cout << "第" << j + 1 << "年的总销量为:"<<sum[j] << endl;
cout << endl;
}
cout << "\n这个三年的总销售量为:" << sum[0] + sum[1] + sum[2] << endl;
return 0;
}
运行结果:
考查点:
- 二维数组
- 嵌套循环
2024年8月25日20:14:54