UVa 263 - Number Chains
// Accepted
==================== Approach : 1 ==========================
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef vector<LL>vll;
typedef map<LL, bool> mpbll;
vll v1 , v2;
void get_num(LL n)
{
v1.clear();
v2.clear();
while(n)
{
v1.push_back(n % 10);
v2.push_back(n % 10);
n/=10;
}
//for(int i=0; i<v1.size(); i++) cout << v1[i] << " ";
}
LL one()
{
LL r=0;
sort(v1.begin() , v1.end());
int len=v1.size();
for(int i=0; i<len; i++)
{
r = v1[i] + (r * 10);
}
return r;
}
LL two()
{
LL r=0;
sort(v2.rbegin() , v2.rend());
int len = v2.size();
for(int i=0; i<len; i++)
{
r = v2[i] + ( r * 10);
}
return r;
}
int main()
{
ios_base::sync_with_stdio(0);
LL n;
mpbll mp;
while(cin >> n and n)
{
mp.clear();
// get_num(n);
// LL a = one(); //cout << a << "\n" ;
// LL b = two(); //cout << b << "\n";
LL d = -1 , a , b , cnt=0;
mp[d] = true;
cout << "Original number was " << n << "\n";
while(mp[d])
{
get_num(n);
a = one();
b = two();
cout << b << " - " << a << " = ";
d = a > b ? a-b : b-a;
cout << d << "\n";
if(!mp[d])
{
n = d;
cnt++;
mp[d] = true;
}
else
{
cout << "Chain length " << cnt+1 << "\n\n";
break;
}
}
}
return 0;
}
============================== Approach: 2================================
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef map<LL, bool>mpbll;
string asc(string x)
{
sort(x.begin(), x.end());
return x;
}
string desc(string x)
{
sort(x.rbegin(), x.rend());
return x;
}
int main()
{
ios_base::sync_with_stdio(0);
string s , tmpstr;
mpbll mp;
while(cin >> s and s!="0")
{
tmpstr = s;
LL a, b, d=-1 , cnt=0;
mp[d] = true;
//s = asc(s);
//cout << s << "\n";
//tmpstr = desc(tmpstr);
//cout << tmpstr << "\n";
cout << "Original number was " << s << "\n";
while(mp[d])
{
s = asc(s);
stringstream ss1(s);
ss1 >> a;
tmpstr = desc(tmpstr);
stringstream ss2(tmpstr);
ss2 >> b;
cout << b << " - " << a << " = ";
d = a > b ? a-b : b-a;
if(!mp[d])
{
stringstream ss3;
ss3 << d;
ss3 >> s;
tmpstr = s;
cnt++;
cout << d << "\n";
mp[d] = true;
}
else
{
cout << d << "\n";
cout << "Chain length " << cnt+1 << "\n\n";
break;
}
}
mp.clear();
}
return 0;
}
==================== Approach : 1 ==========================
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef vector<LL>vll;
typedef map<LL, bool> mpbll;
vll v1 , v2;
void get_num(LL n)
{
v1.clear();
v2.clear();
while(n)
{
v1.push_back(n % 10);
v2.push_back(n % 10);
n/=10;
}
//for(int i=0; i<v1.size(); i++) cout << v1[i] << " ";
}
LL one()
{
LL r=0;
sort(v1.begin() , v1.end());
int len=v1.size();
for(int i=0; i<len; i++)
{
r = v1[i] + (r * 10);
}
return r;
}
LL two()
{
LL r=0;
sort(v2.rbegin() , v2.rend());
int len = v2.size();
for(int i=0; i<len; i++)
{
r = v2[i] + ( r * 10);
}
return r;
}
int main()
{
ios_base::sync_with_stdio(0);
LL n;
mpbll mp;
while(cin >> n and n)
{
mp.clear();
// get_num(n);
// LL a = one(); //cout << a << "\n" ;
// LL b = two(); //cout << b << "\n";
LL d = -1 , a , b , cnt=0;
mp[d] = true;
cout << "Original number was " << n << "\n";
while(mp[d])
{
get_num(n);
a = one();
b = two();
cout << b << " - " << a << " = ";
d = a > b ? a-b : b-a;
cout << d << "\n";
if(!mp[d])
{
n = d;
cnt++;
mp[d] = true;
}
else
{
cout << "Chain length " << cnt+1 << "\n\n";
break;
}
}
}
return 0;
}
============================== Approach: 2================================
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef map<LL, bool>mpbll;
string asc(string x)
{
sort(x.begin(), x.end());
return x;
}
string desc(string x)
{
sort(x.rbegin(), x.rend());
return x;
}
int main()
{
ios_base::sync_with_stdio(0);
string s , tmpstr;
mpbll mp;
while(cin >> s and s!="0")
{
tmpstr = s;
LL a, b, d=-1 , cnt=0;
mp[d] = true;
//s = asc(s);
//cout << s << "\n";
//tmpstr = desc(tmpstr);
//cout << tmpstr << "\n";
cout << "Original number was " << s << "\n";
while(mp[d])
{
s = asc(s);
stringstream ss1(s);
ss1 >> a;
tmpstr = desc(tmpstr);
stringstream ss2(tmpstr);
ss2 >> b;
cout << b << " - " << a << " = ";
d = a > b ? a-b : b-a;
if(!mp[d])
{
stringstream ss3;
ss3 << d;
ss3 >> s;
tmpstr = s;
cnt++;
cout << d << "\n";
mp[d] = true;
}
else
{
cout << d << "\n";
cout << "Chain length " << cnt+1 << "\n\n";
break;
}
}
mp.clear();
}
return 0;
}
Comments
Post a Comment