-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2nd.cpp
68 lines (65 loc) · 1.36 KB
/
2nd.cpp
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
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
int check(string s)
{
int ch=0;
for(int i=0;i<((s.length())-1);i++)
{
if((s[i]-'0')>(s[i+1]-'0'))
{ch=1;break;}
}
return ch;
}
long long int contostr(string s)
{
long long int sum=0;
for(int i=s.length()-1;i>=0;i--)
{
sum=sum+((s[i]-'0')*pow(10,s.length()-i-1));
}
return sum;
}
int main()
{
int t;string s;int i;int k;
cin>>t;int ch=0;
while(t--)
{
cin>>s;
string h=s;
for(i=0;i<((s.length())-1);i++)
{
if((s[i]-'0')>(s[i+1]-'0'))
ch=1;
}
if(ch==0)
{cout<<s;return 0;}
else
{
int l=s.length();
s[l-1]='9';
for(int j=i-1;j>=0;j--)
{
if(s[j]!='9')
s[j]=((s[j]-'0')-1)+'0';
if(j==0 && s[j]=='0')
{
for(k=0;k<(s.length()-1);k++)
s[k]=s[k+1];
s[k]='\0';
}
if(check(s)==0)
{
cout<<s;
return 0;
}
if(s[j]=='0' || (s[j]-'0')<=(j))
s[j]='9';
}
}
cout<<s;
return 0;
}
}