Программа на питоне, перебирающая варианты и возвращающая все ответы минимальной длины: def number_of_adds(n): def next_seq(seq): new_elems = set() for i in range(len(seq)): for j in range(i, len(seq)): new_elem = seq[i] + seq[j] if new_elem > seq[-1] and new_elem not in new_elems: new_elems.add(new_elem) yield seq + [new_elem]
current_stage = None next_stage = [[1]] answer = [] while len(answer) == 0: current_stage = next_stage next_stage = [] for chain in current_stage: next_stage.extend(next_seq(chain)) answer = [seq[1:] for seq in next_stage if seq[-1] == n] return answer
def print_solution(n): answer = number_of_adds(n) print("Для {} есть {} решений(-я, -е):".format(n, len(answer))) for i in range(len(answer)): print("{}. {}".format(i + 1, " ".join(map(str, answer[i] print()
print_solution(27)
Запустив, находим, что необходимо 6 сложений. За 6 сложений можно получить 27X, например, так: X + X = 2X 2X + X = 3X 3X + 3X = 6X 6X + 6X = 12X 12X + 12X = 24X 24X + 3X = 27X
#include <bits/stdc++.h>
using namespace std;
int main()
{
char a[8][8];
long long x, y, i, j;
cin >> y >> x;
for(i = 0; i < 8; i++){
for(j = 0; j < 8; j++){
a[i][j] = '.';
}
}
x--;
y--;
if(x - y >= 0){
i = 0;
j = x - y;
}
else{
j = 0;
i = y - x;
}
for(;i < 8 and j < 8;){
a[i][j] = '*';
i++;
j++;
}
if(x + y <= 7){
i = 0;
j = x + y;
}
else{
j = 7;
i = x + y - 7;
}
for(;i < 8 and j >= 0;){
a[i][j] = '*';
i++;
j--;
}
i = 0;
j = x;
for(;i < 8; i++){
a[i][j] = '*';
}
i = y;
j = 0;
for(;j < 8; j++){
a[i][j] = '*';
}
a[y][x] ='Q';
for(i = 0; i < 8; i++){
for(j = 0; j < 8; j++){
cout << a[i][j] << " ";
}
cout << '\n';
}
return 0;
}
Объяснение: