Each egg should be painted one color out of 7: red, orange, yellow, green, blue, indigo or violet. Also, the following conditions should be satisfied:
- Each of the seven colors should be used to paint at least one egg.
- Any four eggs lying sequentially should be painted different colors
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
using namespace std;
class List
{
public:
char val;
List* prior;
List* next;
List(char data = '\0', List* p = NULL, List* q = NULL) { val = data; prior = p; next = q; }
};
char color[7] = { 'R','O','Y','G','B','I','V' };
List* Creat(int n)
{
List* head = new List('\0', NULL);
List* last = head;
for (int i = 0; i < n; i++) {
List* node = new List;
node->val = '\0';//表示还没有
last->next = node;
node->prior = last;
last = node;
}
last->next = head->next;
head->prior = last;
return head;
}
//d be painted one color out of 7
// "R" stands for red, "O" stands for orange, "Y" stands for yellow, "G" stands for green, "B" stands for blue, "I" stands for indigo, "V" stands for violet.
//ROYGBIV
void paint(List *head,int n)
{
List* p = head->next;
int i = 0;
while (p != head->next||i==0) {
while (1) {
int j = i % 7;
if (p->prior->val != color[j] && p->prior->prior->val != color[j] && p->prior->prior->prior->val != color[j])
{
if (p->next->val != color[j] && p->next->next->val != color[j] && p->next->next->next->val != color[j])
{
p->val = color[j]; break;
}
}
i++;
}
p = p->next;
}
}
int main()
{
int n; cin >> n;
List* head = Creat(n);
paint(head, n);
List* p = head->next;
int i = 0;
while (p != head->next||i==0) {
cout << p->val;
p = p->next;
++i;
}
//向前找三个后找三个
//没有能放下的就返回上一个?//不用吧
return 0;
}