【问题描述】
圆盘找数问题。设有20个整数的数组成一个圆(首尾相接),分别求出连续的四个数之和为最大的四个数及其和值)以及连续的四个数之和为最小的四个数(输出整个数列、最大、最小的四个数及其和值。
要求:
(1)圆盘类class circle中包含私有成员int num[20], int Max[4] , int Min[4],int sum_max, int sum_min, 同时根据需要增加私有或公有成员及必要的公有成员函数。
int num[20]; //存放输入的20个随机数
int Max[4]; //20个数中连续4个数之和中最大的那4个连续的数
int Min[4]; //20个中连续4个数之和中最小的那4个连续的数
int sum_max; //20个数中连续4个数之和中最大的和
int sum_min; //20个数中连续4个数之和中最小的和
(2)在键盘随机输入20个数
【输入形式】
9 2 19 65 31 74 89 3 57 81 52 96 63 36 41 44 76 64 84 80
62 37 34 62 24 66 52 34 99 16 85 1 5 33 67 42 80 55 75 58
【输出形式】
圆盘a:
9 2 19 65 31 74 89 3 57 81 52 96 63 36 41 44 76 64 84 80
Max=304
76 64 84 80
Min=95
9 2 19 65
圆盘b:
62 37 34 62 24 66 52 34 99 16 85 1 5 33 67 42 80 55 75 58
Max=268
80 55 75 58
Min=106
1 5 33 67
【问题分析】
本题可以使用循环链表解题,也可以通过数组模拟循环链表,也可使用双指针算法,这里只写出最容易懂的简单方式
首先介绍一个STL中的pair<int,int>
pair<int,int> a;
//a中存储两个整型数据
a.first = 1;
a.second = 2;
//上面对A中的两个数据进行赋值
//同理也可使用两个不同的数据类型,如:
pair<float,int> x;
pair<string,int> s;
//也可以使用sort函数对pair数组进行排序,默认以a[i].first进行排序,如
pair<int,int> a[10];
sort(a, a+10);
后面将会使用到pair
首先画一个示意图表示20个数据在内存中的示意图
要计算连续四个数之和且要找到这些和的最大最小值那么可以先从前往后遍历到n[16]
当红色箭头指向n[16]时,绿色箭头已经不能后移,那么就进行分类讨论
【参考代码】
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int, int> PII;
class Circle
{
private:
int num[20];
int Max[4];
int Min[4];
int sum_max;
int sum_min;
PII add[20]; //此数组first为以i为起点四个元素之和,second记录起始元素下标
public:
void setNum(int p[])
{
for (int i = 0; i < 20; i++)
{
num[i] = p[i];
}
}
void addNum()
{
for (int i = 0; i < 17; i++)
{
add[i].first = num[i] + num[i + 1] + num[i + 2] + num[i + 3];
add[i].second = i;
}
//17-19号元素单独讨论
add[17].first = num[17] + num[18] + num[19] + num[0];
add[17].second = 17;
add[18].first = num[18] + num[19] + num[0] + num[1];
add[18].second = 18;
add[19].first = num[19] + num[0] + num[1] + num[2];
add[19].second = 19;
}
void findx()
{
sort(add, add + 20);
sum_max = add[19].first;
sum_min = add[0].first;
//记录最大元素
if (add[19].second <= 16)
{
for (int i = add[19].second, j = 0; j < 4; i++, j++)
{
Max[j] = num[i];
}
}
else if (add[19].second == 17)
{
Max[0] = num[17];
Max[1] = num[18];
Max[2] = num[19];
Max[3] = num[0];
}
else if (add[19].second == 18)
{
Max[0] = num[18];
Max[1] = num[19];
Max[2] = num[0];
Max[3] = num[1];
}
else if (add[19].second == 19)
{
Max[0] = num[19];
Max[1] = num[0];
Max[2] = num[1];
Max[3] = num[2];
}
//记录最小元素
if (add[0].second <= 16)
{
for (int i = add[0].second, j = 0; j < 4; i++, j++)
{
Min[j] = num[i];
}
}
else if (add[0].second == 17)
{
Min[0] = num[17];
Min[1] = num[18];
Min[2] = num[19];
Min[3] = num[0];
}
else if (add[0].second == 18)
{
Min[0] = num[18];
Min[1] = num[19];
Min[2] = num[0];
Min[3] = num[1];
}
else if (add[0].second == 19)
{
Min[0] = num[19];
Min[1] = num[0];
Min[2] = num[1];
Min[3] = num[2];
}
}
void print()
{
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()
{
Circle a, b;
int t[20];
for (int i = 0; i < 20; i++)
{
cin >> t[i];
}
a.setNum(t);
for (int i = 0; i < 20; i++)
{
cin >> test[i];
}
b.setNum(t);
a.addNum();
b.addNum();
a.findx();
b.findx();
cout << "圆盘a:" << endl;
a.print();
cout << "圆盘b:" << endl;
b.print();
return 0;
}