Title Description
Given A positive integer A of no more than 6, consider four consecutive numbers starting with A. Please output 3 digits of all non repeating numbers composed of them.
input
Enter A on one line.
output
Output 3 digits that meet the conditions, from small to large, 6 integers per line. Integers are separated by spaces, but no extra spaces are allowed at the end of the line.
sample input
2
sample output
234 235 243 245 253 254 324 325 342 345 352 354 423 425 432 435 452 453 523 524 532 534 542 543
thinking
Two ideas
1. Use next ﹣ permutation enumeration to store in set and then output
2. similar to dfs
Idea one code
#include <bits/stdc++.h>
using namespace std;
set<int> st;
int a[3];
int main(){
int n;
cin>>n;
a[0] = n;
a[1] = n+1;
a[2] = n+2;
do{
st.insert(a[0]*100+a[1]*10+a[2]);
}while(next_permutation(a, a+3));
a[0] = n;
a[1] = n+1;
a[2] = n+3;
do{
st.insert(a[0]*100+a[1]*10+a[2]);
}while(next_permutation(a, a+3));
a[0] = n;
a[1] = n+2;
a[2] = n+3;
do{
st.insert(a[0]*100+a[1]*10+a[2]);
}while(next_permutation(a, a+3));
a[0] = n+1;
a[1] = n+2;
a[2] = n+3;
do{
st.insert(a[0]*100+a[1]*10+a[2]);
}while(next_permutation(a, a+3));
int cnt = 0;
for(set<int>::iterator it=st.begin(); it!=st.end(); it++){
if(cnt){
cout<<' ';
}
cout<<*it;
if(++cnt%6 == 0){
cnt = 0;
cout<<endl;
}
}
return 0;
}
Idea 2 code
#include <bits/stdc++.h>
using namespace std;
int book[10];
int main(){
int a;
int cnt = 0;
cin>>a;
int i, j, k;
for(i=a; i<a+4; i++){
book[i] = 1;
for(j=a; j<a+4; j++){
if(book[j] == 0){
book[j] = 1;
for(k=a; k<a+4; k++){
if(book[k] == 0){
book[k] = 1;
if(cnt){
cout<<' ';
}
cout<<i<<j<<k;
if(++cnt==6){
cnt = 0;
cout<<endl;
}
book[k] = 0;
}
}
book[j] = 0;
}
}
book[i] = 0;
}
return 0;
}