-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpful_maths.cpp
More file actions
99 lines (78 loc) · 1.41 KB
/
Copy pathhelpful_maths.cpp
File metadata and controls
99 lines (78 loc) · 1.41 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
using namespace std;
// newline
void br(){
cout<<endl;
}
// Check if sorted
bool isSorted(int* a, int arr_len){
bool ok=true;
for(int i=0; i<arr_len; i++){
if(a[i]>a[i+1])
ok = false;
}
return ok;
}
// Adding element to end
void push_end(int* a,int i,int arr_len)
{
int aux;
aux = a[i];
// Shifting to left
for(int x=i;x<arr_len-1;x++)
a[x] = a[x+1];
a[arr_len-1] = aux;
}
void push_beg(int* a,int i, int arr_len)
{
int aux = a[i];
// shifting to right
for(int x=i; x>0; x--)
a[x] = a[x-1];
a[0] = aux;
}
void print_arr(int *a, int arr_len)
{
// Printing the array
for(int i=0; i<arr_len; i++)
cout<<a[i]<<endl;
}
int main()
{
string s="2+1+2+2+2+3+3+1+3+1+2";
cin>>s;
int arr_len=(s.length()+1)/2;
int a[arr_len];
// Printing the initial string
/* cout<<s; */
/* br(); */
// Getting the numbers
for(int i=0; i<arr_len; i++){
a[i]=s[i*2]-48;
}
/* print_arr(a,arr_len); */
/* br(); */
// Defining the index
int i=0;
// Sorting the array
while(!isSorted(a, arr_len)){
if(a[i]==3)
{
push_end(a, i, arr_len);
i--;
}
else if(a[i]==1)
push_beg(a, i, arr_len);
i++;
}
/* br(); */
/* print_arr(a,arr_len); */
/* br(); */
// Creating the ordered string
for(int i=0; i<arr_len; i++)
{
s[i*2] = a[i] + 48;
}
cout<<s;
return 0;
}