-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAmazon.cpp
More file actions
57 lines (49 loc) · 1.26 KB
/
Copy pathAmazon.cpp
File metadata and controls
57 lines (49 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;
inline int compare (const void *a, const void * b) { return ( *(char *)a - *(char *)b ); }
void swap (char* a, char* b) {
char t = *a;
*a = *b;
*b = t;
}
int findCeil (char str[], char first, int l, int h) {
int ceilIndex = l;
for (int i = l+1; i <= h; i++)
if (str[i] > first && str[i] < str[ceilIndex])
ceilIndex = i;
return ceilIndex;
}
char * ans ( char str[], int n)
{
int size = strlen(str);
qsort( str, size, sizeof( str[0] ), compare );
int count=0;
bool isFinished = false;
while ( ! isFinished )
{
if(++count == n)
return str;
int i;
for ( i = size - 2; i >= 0; --i )
if (str[i] < str[i+1])
break;
if ( i == -1 )
isFinished = true;
else
{
int ceilIndex = findCeil( str, str[i], i + 1, size - 1 );
swap( &str[i], &str[ceilIndex] );
qsort( str + i + 1, size - i - 1, sizeof(str[0]), compare );
}
}
}
int main()
{
int test;
char str[] = "ABCD";
int n=3;
cout<<sortedPermutations( str, n );
return 0;
}