Submitted solution with Run Time = 0.010
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> nos;
int t,sum,i;
cin>>t;
while(t!=0)
{
nos.push_back(t);
cin>>t;
}
nos.push_back(0);
t=0;
sum=1;
cout<<"PERFECTION OUTPUT"<<endl;
while(nos[t]!=0)
{
if(nos[t] == 1)
{
cout<<" "<<nos[t]<<" ";
// printf("%5d ",nos[t]);
cout<<"DEFICIENT"<<endl;
t++;
}
else
{
for(i=2;i<nos[t];i++)
{
if(nos[t]%i==0)
{
sum=sum+i;
}
}
if(nos[t]/10==0)
cout<<" "<<nos[t]<<" ";
else if(nos[t]/100==0)
cout<<" "<<nos[t]<<" ";
else if(nos[t]/1000==0)
cout<<" "<<nos[t]<<" ";
else if(nos[t]/10000==0)
cout<<" "<<nos[t]<<" ";
else if(nos[t]/100000==0)
cout<<nos[t]<<" ";
// printf("%5d ",nos[t]);
if(sum==nos[t])
{
cout<<"PERFECT"<<endl;
}
else if(sum>nos[t])
{
cout<<"ABUNDANT"<<endl;
}
else if(sum<nos[t])
{
cout<<"DEFICIENT"<<endl;
}
sum=1;
t++;
}
}
cout<<"END OF OUTPUT"<<endl;
//system("pause");
return 0;
}
Comment for better algorithm.
Wednesday, February 6, 2008
Ecological Bin Packing : Problem No. 102
Submitted solution with Run Time = 0.310
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
using namespace std;
int main()
{
int i,x[9],mini,t;
int mov[6];
vector<string> data;
while(cin>>t)
{
x[0]=t;
for(i=1;i<9;i++)
{
cin>>x[i];
}
// original is bgc
//bcg
mov[0]=x[3]+x[6]+x[2]+x[8]+x[1]+x[4];
data.push_back("BCG");
//bgc
mov[1]=x[3]+x[6]+x[1]+x[7]+x[2]+x[5];
data.push_back("BGC");
//cbg
mov[2]=x[5]+x[8]+x[0]+x[6]+x[1]+x[4];
data.push_back("CBG");
//cgb
mov[3]=x[8]+x[5]+x[1]+x[7]+x[0]+x[3];
data.push_back("CGB");
//gbc
mov[4]=x[4]+x[7]+x[0]+x[6]+x[2]+x[5];
data.push_back("GBC");
//gcb
mov[5]=x[4]+x[7]+x[2]+x[8]+x[0]+x[3];
data.push_back("GCB");
mini=mov[0];
for(i=0;i<6;i++)
mini=min(mini,mov[i]);
for(i=0;i<6;i++)
{
if(mov[i]==mini)
{
cout<<data[i]<<" "<<mov[i]<<endl;
break;
}
}
}
return 0;
}
Comment for better algorithm.
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
using namespace std;
int main()
{
int i,x[9],mini,t;
int mov[6];
vector<string> data;
while(cin>>t)
{
x[0]=t;
for(i=1;i<9;i++)
{
cin>>x[i];
}
// original is bgc
//bcg
mov[0]=x[3]+x[6]+x[2]+x[8]+x[1]+x[4];
data.push_back("BCG");
//bgc
mov[1]=x[3]+x[6]+x[1]+x[7]+x[2]+x[5];
data.push_back("BGC");
//cbg
mov[2]=x[5]+x[8]+x[0]+x[6]+x[1]+x[4];
data.push_back("CBG");
//cgb
mov[3]=x[8]+x[5]+x[1]+x[7]+x[0]+x[3];
data.push_back("CGB");
//gbc
mov[4]=x[4]+x[7]+x[0]+x[6]+x[2]+x[5];
data.push_back("GBC");
//gcb
mov[5]=x[4]+x[7]+x[2]+x[8]+x[0]+x[3];
data.push_back("GCB");
mini=mov[0];
for(i=0;i<6;i++)
mini=min(mini,mov[i]);
for(i=0;i<6;i++)
{
if(mov[i]==mini)
{
cout<<data[i]<<" "<<mov[i]<<endl;
break;
}
}
}
return 0;
}
Comment for better algorithm.
Hashmat the Brave Warrior : Problem No. 10055
Solution submitted with Run Time = 1.210
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
long long m,n;
while(cin>>m>>n)
{
if((m-n)>0)
cout<<m-n<<endl;
else
cout<<n-m<<endl;
}
return 0;
}
Comment for better algorithm.
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
long long m,n;
while(cin>>m>>n)
{
if((m-n)>0)
cout<<m-n<<endl;
else
cout<<n-m<<endl;
}
return 0;
}
Comment for better algorithm.
Tuesday, February 5, 2008
Train Swapping : Problem No. 299
Submitted solution with Rum Time = 0.010
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int count=0,n,t,i,j,k,m;
vector<int> car;
cin>>n;
for(i=0;i<n;i++)
{
cin>>t;
if(t==1)
{cin>>j;
cout<<"Optimal train swapping takes 0 swaps."<<endl;
}
else
{
car.resize(t);
for(j=0;j<t;j++)
{
cin>>car[j];
}
for(j=0;j<t;j++)
{
for(k=0;k<t-j-1;k++)
{
if(car[k+1]<car[k])
{
m=car[k+1];
car[k+1]=car[k];
car[k]=m;
count++;
}
}
}
cout<<"Optimal train swapping takes "<<count<<" swaps."<<endl;
count=0;
}
}
return 0;
}
Comment for better algorithm or lesser run time.
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int count=0,n,t,i,j,k,m;
vector<int> car;
cin>>n;
for(i=0;i<n;i++)
{
cin>>t;
if(t==1)
{cin>>j;
cout<<"Optimal train swapping takes 0 swaps."<<endl;
}
else
{
car.resize(t);
for(j=0;j<t;j++)
{
cin>>car[j];
}
for(j=0;j<t;j++)
{
for(k=0;k<t-j-1;k++)
{
if(car[k+1]<car[k])
{
m=car[k+1];
car[k+1]=car[k];
car[k]=m;
count++;
}
}
}
cout<<"Optimal train swapping takes "<<count<<" swaps."<<endl;
count=0;
}
}
return 0;
}
Comment for better algorithm or lesser run time.
Kindergarten Counting Game : Problem No. 494
Submitted solution with Run Time = 0.000
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str;
int i,count;
while(getline(cin,str))
{
i=0;
count=0;
while(str[i]!='\0')
{
if((str[i]>=65 && str[i]<=90) || (str[i]>=97 && str[i]<=122))
{
count++;
while((str[i]>=65 && str[i]<=90) || (str[i]>=97 && str[i]<=122))
{
i++;
}
}
i++;
}
cout<<count<<endl;
}
return 0;
}
Comment for better algorithm.
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str;
int i,count;
while(getline(cin,str))
{
i=0;
count=0;
while(str[i]!='\0')
{
if((str[i]>=65 && str[i]<=90) || (str[i]>=97 && str[i]<=122))
{
count++;
while((str[i]>=65 && str[i]<=90) || (str[i]>=97 && str[i]<=122))
{
i++;
}
}
i++;
}
cout<<count<<endl;
}
return 0;
}
Comment for better algorithm.
Sunday, February 3, 2008
The Decoder : Problem No. 458
Submitted solution with Run Time = 0.330
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str,nstr;
int i;
while(getline(cin,str))
{
i=0;
nstr.resize(str.size());
while(str[i])
{
nstr[i]=str[i]-7;
i++;
}
cout<<nstr<<endl;
}
return(0);
}
Do comment for better solution or any query.
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str,nstr;
int i;
while(getline(cin,str))
{
i=0;
nstr.resize(str.size());
while(str[i])
{
nstr[i]=str[i]-7;
i++;
}
cout<<nstr<<endl;
}
return(0);
}
Do comment for better solution or any query.
Saturday, February 2, 2008
Tex Quotes : Problem No. 272
My submitted solution :-
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str;
int i,j,t=0;
while(getline(cin,str))
{
for(i=0;i<str.length();i++)
{
if(str[i]=='"' && (t+1)%2==1)
{
str=str+str[str.length()-1];
for(j=str.length();j>i;j--)
{
str[j]=str[j-1];
}
str[i]=str[i+1]='`';
t++;
}
else if(str[i]=='"' && (t+1)%2==0)
{
str=str+str[str.length()-1];
for(j=str.length();j>i;j--)
{
str[j]=str[j-1];
}
str[i]=str[i+1]='\'';
t++;
}
}
cout<<str<<endl;
}
return 0;
}
Comment for better algorithm or any query you have.
Run Time = 0.020
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str;
int i,j,t=0;
while(getline(cin,str))
{
for(i=0;i<str.length();i++)
{
if(str[i]=='"' && (t+1)%2==1)
{
str=str+str[str.length()-1];
for(j=str.length();j>i;j--)
{
str[j]=str[j-1];
}
str[i]=str[i+1]='`';
t++;
}
else if(str[i]=='"' && (t+1)%2==0)
{
str=str+str[str.length()-1];
for(j=str.length();j>i;j--)
{
str[j]=str[j-1];
}
str[i]=str[i+1]='\'';
t++;
}
}
cout<<str<<endl;
}
return 0;
}
Comment for better algorithm or any query you have.
Run Time = 0.020
Thursday, January 24, 2008
Back To High School Physics : Problem No.10071
#include<iostream>
using namespace std;
int main()
{
int a,b;
cin>>a>>b;
while(cin>>a>>b)
{
cout<<2*a*b<<endl;
if(a==-1 && b==-1)
{
break;
}
}
}
Problem submitted with runtime = 0.410
Do comment for better algorithm or lesser runtime solution.
using namespace std;
int main()
{
int a,b;
cin>>a>>b;
while(cin>>a>>b)
{
cout<<2*a*b<<endl;
if(a==-1 && b==-1)
{
break;
}
}
}
Problem submitted with runtime = 0.410
Do comment for better algorithm or lesser runtime solution.
Box of bricks : Problem No.591
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int total,x,k=0,t=0,j=1,i=0;
int nos[50];
cin>>total;
while(total!=0)
{
for(i=0;i<total;i++)
{
cin>>x;
nos[i]=x;
t=t+nos[i];
}
t=t/total;
for(i=0;i<total;i++)
{
if((nos[i]>t) || (nos[i]<t))
{
k = k + abs(nos[i]-t);
}
}
cout<<"Set #"<<j<<endl<<"The minimum number of moves is "<<k/2<<"."<<endl<<endl;
t=0;
k=0;
j++;
cin>>total;
}
}
Problem submitted with runtime = 0.000
Do comment for better algorithm.
#include<cmath>
using namespace std;
int main()
{
int total,x,k=0,t=0,j=1,i=0;
int nos[50];
cin>>total;
while(total!=0)
{
for(i=0;i<total;i++)
{
cin>>x;
nos[i]=x;
t=t+nos[i];
}
t=t/total;
for(i=0;i<total;i++)
{
if((nos[i]>t) || (nos[i]<t))
{
k = k + abs(nos[i]-t);
}
}
cout<<"Set #"<<j<<endl<<"The minimum number of moves is "<<k/2<<"."<<endl<<endl;
t=0;
k=0;
j++;
cin>>total;
}
}
Problem submitted with runtime = 0.000
Do comment for better algorithm.
3n+1 problem : Problem No.100
#include<iostream>
using namespace std;
int cyc(int);
int main()
{
int a,b,i,t;
while(cin>>a>>b)
{
if(a==0 && b==0)
{
break;
}
else
{
t=0;
if(b>a)
{
for(i=a;i<=b;i++)
{
if(cyc(i)>t)
{
t=cyc(i);
}
}
}
else
{
for(i=b;i<=a;i++)
{
if(cyc(i)>t)
{
t=cyc(i);
}
}}
cout<<a<<" "<<b<<" "<<t<<endl;
}
}
}
int cyc(int n)
{
int i=1;
while(n!=1)
{
if((n%2)!=0)
{
n=3*n+1;
}
else
{
n=n/2;
}
i++;
}
return i;
}
Problem submitted with runtime = 0.860
Do comment for better algorithm or lesser runtime solution.
What this blog is all about ??
In this blog I will discuss problems from ACM archive, both from 'Problem Set Volumes' and 'Contest Volumes'. I will discuss here the problems I submitted and their solutions. Any other or better algorithm for any problem is always welcome.
Problem Set Archive
Problem Set Archive can be found at --->
http://acm.uva.es/problemset/
Register at new site --->
http://icpcres.ecs.baylor.edu/onlinejudge/index.php
http://acm.uva.es/problemset/
Register at new site --->
http://icpcres.ecs.baylor.edu/onlinejudge/index.php
What is acm?
ACM(Association for computing machinery) is widely recognized as the premier membership organization for computing professionals, delivering resources that advance computing as a science and a profession; enable professional development; and promote policies and research that benefit society.
Know more about it at
http://www.acm.org
or
http://www.en.wikipedia.org/wiki/Association_for_Computing_Machinery
Know more about it at
http://www.acm.org
or
http://www.en.wikipedia.org/wiki/
Subscribe to:
Comments (Atom)