#include <iostream>
using namespace std;
// Nearly Lucky Number
int main()
{
//如果小于=4,就直接输出
//如果大于4,
//
//
//这应该叫贪心?
int n; cin >> n;
if (n <= 4) {
for (int i = 0; i < n; i++) cout << (char) ('a' + i);
return 0;
}
char* c = new char[n] ;
int j = 0;
for (int i = 0; i < n; i++) {
if (i - 4 >= 0 && i - 7 >= 0) {
if (c[i - 4] < c[i - 7] && c[i - 1] != c[i - 4]) c[i] = c[i - 4];//i-4小,与i-1不相同
else if (c[i - 7] < c[i-4]&& c[i - 7] != c[i - 3]) { c[i] = c[i - 7]; }//i-7小, i-7与i-3不相同
else if (c[i - 4] != c[i - 1]) c[i] = c[i - 4];//i-4大,但可以
else if (c[i - 7] != c[i - 3]) c[i] = c[i - 7];//i-7大,但可以
}
else if (i - 4 >= 0 && i - 7 < 0) c[i] = c[i - 4];
else {
c[i] =(char) ( 'a' + j);
j++;
}
}
for (int i = 0; i < n; i++) cout << c[i];
return 0;
}
Petya recently learned to determine whether a string of lowercase Latin letters is lucky. For each individual letter all its positions in the string are written out in the increasing order. This results in 26 lists of numbers; some of them can be empty. A string is considered lucky if and only if in each list the absolute difference of any two adjacent numbers is a lucky number.
该程序是用C++编写的,用于确定一个由小写字母组成的字符串是否为NearlyLuckyNumber。当字符串中每个字母的位置按升序列出时,如果所有列表中任意相邻数字的绝对差是幸运数,则字符串被认为是幸运的。程序采用了贪心策略来处理字符串中的字符选择。
1365

被折叠的 条评论
为什么被折叠?



