Răspuns :
#include <fstream>
using namespace std;
bool accesibil (int a){
int ok;
if (a <= 9)
ok = 0;
else ok = 1;
while (a > 9) {
if(a % 10 != (a % 100 / 10 + 1 ))
ok = 0;
a /= 10;
}
return ok;
}
int nrcif(int n) {
int nr = 0 ;
while (n > 0) {
nr++;
n /= 10;
}
return nr;
}
bool devine (int a) {
int sol = 0, cif = nrcif(a), nrnou, aa, nr, p;
if (accesibil(a))
sol = 0;
else {
for (int i = 1 ; i <= cif; i++){
aa = a;
nr = 0;
p = 1;
nrnou = 0;
while (aa > 0) {
nr++;
if (nr != i){
nrnou = (aa % 10) * p + nrnou;
p = p * 10;
}
aa /= 10;
}
if (accesibil(nrnou))
sol++;
}
return sol > 0;
}
}
int main() {
ifstream cin("accesibil.in");
ofstream cout("accesibil.out");
int p , n, k;
cin >> p >> k >> n;
if (p == 1) {
int max1 = 0, max2 = 0, max3 = 0, a, ok;
for (int i = 0; i < n; i++) {
cin >> a;
if (a > 9) {
if (accesibil(a)) {
if (a >= max1) {
max3 = max2;
max2 = max1;
max1 = a;
}
else if ( a >= max2) {
max3 = max2;
max2 = a;
}
else if (a >= max3)
max3 = a;
}
}
}
cout << max3 << " " << max2 <<" " << max1;
}
else if (p == 2){
int sol = 0, a;
for (int i = 0; i < n; i++) {
cin >> a;
if (a > 9 && !(accesibil(a)))
if (devine(a))
sol++;
}
cout << sol;
}
else if (p == 3) {
int min = 1, max = 9 - k + 1;
for (int i = 2; i <= k; i++)
min = min * 10 + i;
for (int i = 9 - k + 2; i <= 9; i++)
max = max * 10 + i;
if (min != max)
cout << min << " " << max;
else
cout << min;
}
else{
int par = 0, impar = 0, nr;
for (int i = 1; i <= 9 - k + 1; i++) {
nr = 0;
for (int y = i; y < i + k; y++)
nr = nr * 10 + y;
if (accesibil(nr)) {
if(nr % 2 == 0)
par++;
else
impar++;
}
}
cout << par << " " <<impar;
}
return 0;
}
using namespace std;
bool accesibil (int a){
int ok;
if (a <= 9)
ok = 0;
else ok = 1;
while (a > 9) {
if(a % 10 != (a % 100 / 10 + 1 ))
ok = 0;
a /= 10;
}
return ok;
}
int nrcif(int n) {
int nr = 0 ;
while (n > 0) {
nr++;
n /= 10;
}
return nr;
}
bool devine (int a) {
int sol = 0, cif = nrcif(a), nrnou, aa, nr, p;
if (accesibil(a))
sol = 0;
else {
for (int i = 1 ; i <= cif; i++){
aa = a;
nr = 0;
p = 1;
nrnou = 0;
while (aa > 0) {
nr++;
if (nr != i){
nrnou = (aa % 10) * p + nrnou;
p = p * 10;
}
aa /= 10;
}
if (accesibil(nrnou))
sol++;
}
return sol > 0;
}
}
int main() {
ifstream cin("accesibil.in");
ofstream cout("accesibil.out");
int p , n, k;
cin >> p >> k >> n;
if (p == 1) {
int max1 = 0, max2 = 0, max3 = 0, a, ok;
for (int i = 0; i < n; i++) {
cin >> a;
if (a > 9) {
if (accesibil(a)) {
if (a >= max1) {
max3 = max2;
max2 = max1;
max1 = a;
}
else if ( a >= max2) {
max3 = max2;
max2 = a;
}
else if (a >= max3)
max3 = a;
}
}
}
cout << max3 << " " << max2 <<" " << max1;
}
else if (p == 2){
int sol = 0, a;
for (int i = 0; i < n; i++) {
cin >> a;
if (a > 9 && !(accesibil(a)))
if (devine(a))
sol++;
}
cout << sol;
}
else if (p == 3) {
int min = 1, max = 9 - k + 1;
for (int i = 2; i <= k; i++)
min = min * 10 + i;
for (int i = 9 - k + 2; i <= 9; i++)
max = max * 10 + i;
if (min != max)
cout << min << " " << max;
else
cout << min;
}
else{
int par = 0, impar = 0, nr;
for (int i = 1; i <= 9 - k + 1; i++) {
nr = 0;
for (int y = i; y < i + k; y++)
nr = nr * 10 + y;
if (accesibil(nr)) {
if(nr % 2 == 0)
par++;
else
impar++;
}
}
cout << par << " " <<impar;
}
return 0;
}