123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- #include <iostream>
- //编译预处理命令
- #include<iostream>
- using namespace std;
- //圆盘类circle的定义与实现
- class circle {
- private:
- int num[20];
- int Max[4];
- int Min[4];
- int sum_max;
- int sum_min;
- public:
- // 构造函数,初始化 num 数组
- circle() {}
- circle(int arr[20]) {
- for (int i = 0; i < 20; ++i)
- num[i] = arr[i];
- }
- // 计算连续四个数之和最大的数和其和值
- void maxSum() {
- int maxSum = 0;
- int index = 0;
- for (int i = 0; i < 17; ++i) {
- int sum = num[i] + num[i + 1] + num[i + 2] + num[i + 3];
- if (sum > maxSum) {
- maxSum = sum;
- index = i;
- }
- }
- sum_max = maxSum;
- for (int i = 0; i < 4; ++i)
- Max[i] = num[index + i];
- }
- // 计算连续四个数之和最小的数和其和值
- void minSum() {
- int minSum = 999999;
- int index = 0;
- for (int i = 0; i < 17; ++i) {
- int sum = num[i] + num[i + 1] + num[i + 2] + num[i + 3];
- if (sum < minSum) {
- minSum = sum;
- index = i;
- }
- }
- sum_min = minSum;
- for (int i = 0; i < 4; ++i)
- Min[i] = num[index + i];
- }
- // 输出圆盘的数列、最大和最小的四个数及其和值
- void show() {
- for (int i = 0; i < 20; ++i)
- cout << num[i] << " ";
- cout << endl;
- cout << "Max=" << sum_max << endl;
- for (int i = 0; i < 4; ++i)
- cout << Max[i] << " ";
- cout << endl;
- cout << "Min=" << sum_min << endl;
- for (int i = 0; i < 4; ++i)
- cout << Min[i] << " ";
- cout << endl;
- }
- };
- int main(void)
- {
- circle a; //圆盘类的对象a
- int i,test[20];
- //输入数据test
-
- for (int i = 0; i < 20; ++i)
- cin >> test[i];
-
- a = circle(test);
-
- for (int i = 0; i < 20; ++i)
- cin >> test[i];
- circle b(test); //圆盘类的对象b
- //测试圆盘a,b
- cout << "圆盘a:" << endl;
- a.maxSum();
- a.minSum();
- a.show();
-
- cout << "圆盘b:" << endl;
- b.maxSum();
- b.minSum();
- b.show();
-
- return 0;
- }
|