Wednesday, 4 February 2015
Friday, 30 January 2015
Solution of spoj Naya Shatranj (New Chess) CODCHESS
#include<iostream>
using namespace std;
int main()
{
long int t,n;
cin>>t;
while(t--)
{
cin>>n;
if(n%2==0)
cout<<"1"<<endl;
else
cout<<"0"<<endl;
}
return 0;
}
using namespace std;
int main()
{
long int t,n;
cin>>t;
while(t--)
{
cin>>n;
if(n%2==0)
cout<<"1"<<endl;
else
cout<<"0"<<endl;
}
return 0;
}
SPOJ solution of CHOTU ( FARIDI AND YADAV)
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main()
{
int t;
double x,y,s;
cin>>t;
while(t--)
{
cin>>x>>y;
s=sqrt(pow(x,2)-pow(y,2));
s=s*2;
cout<<fixed<<setprecision(3)<<s<<endl;
}
return 0;
}
#include<cmath>
#include<iomanip>
using namespace std;
int main()
{
int t;
double x,y,s;
cin>>t;
while(t--)
{
cin>>x>>y;
s=sqrt(pow(x,2)-pow(y,2));
s=s*2;
cout<<fixed<<setprecision(3)<<s<<endl;
}
return 0;
}
SPOJ CANDY 3 solution
#include<iostream>
using namespace std;
int main()
{ long long int t,n,sum;
cin>>t;
while(t--)
{
cout<<endl;
sum=0;
cin>>n;
long long int a[n];
for(int i=0;i<n;i++)
{ cin>>a[i];
sum=(sum+a[i])%n ;
}
if(sum%n==0)
cout<<"YES"<<endl ;
else
cout<<"NO" <<endl;
}
return 0;
}
using namespace std;
int main()
{ long long int t,n,sum;
cin>>t;
while(t--)
{
cout<<endl;
sum=0;
cin>>n;
long long int a[n];
for(int i=0;i<n;i++)
{ cin>>a[i];
sum=(sum+a[i])%n ;
}
if(sum%n==0)
cout<<"YES"<<endl ;
else
cout<<"NO" <<endl;
}
return 0;
}
Spoj CANDY I solution problem code candy
#include<iostream>
using namespace std;
int main()
{
int n,need,count,i;
while(5)
{
need=0,count=0;
cin>>n;
if(n==-1)
{
break;
}
else
{
int a[n];
for(i=0;i<n;i++)
{
cin>>a[i];
need=need+a[i] ;
cout<<endl;
}
if(need%n!=0)
{
cout<<"-1" ;
}
else
{
for(i=0;i<n;i++)
{
if(a[i]<need/n)
{
count=count+(need/n)-a[i];
}
}
cout<<endl<<count;
}
}
}
return 0;
}
using namespace std;
int main()
{
int n,need,count,i;
while(5)
{
need=0,count=0;
cin>>n;
if(n==-1)
{
break;
}
else
{
int a[n];
for(i=0;i<n;i++)
{
cin>>a[i];
need=need+a[i] ;
cout<<endl;
}
if(need%n!=0)
{
cout<<"-1" ;
}
else
{
for(i=0;i<n;i++)
{
if(a[i]<need/n)
{
count=count+(need/n)-a[i];
}
}
cout<<endl<<count;
}
}
}
return 0;
}
Solution Of Candy Distribution Spoj CADYDIST
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{ long long int n,i,j,s;
while(1)
{ s=0;
scanf("%lld",&n);
if(n==0)
break;
long long int a[n],b[n];
for(i=0;i<n;i++)
scanf("%lld",&a[i]);
for(i=0;i<n;i++)
scanf("%lld",&b[i]);
sort(a,a+n);
sort(b,b+n);
for(i=0,j=n-1;i<n,j>=0;i++,j--)
s=s+a[i]*b[j];
printf("%lld\n",s);
}
return 0;
}
#include<algorithm>
using namespace std;
int main()
{ long long int n,i,j,s;
while(1)
{ s=0;
scanf("%lld",&n);
if(n==0)
break;
long long int a[n],b[n];
for(i=0;i<n;i++)
scanf("%lld",&a[i]);
for(i=0;i<n;i++)
scanf("%lld",&b[i]);
sort(a,a+n);
sort(b,b+n);
for(i=0,j=n-1;i<n,j>=0;i++,j--)
s=s+a[i]*b[j];
printf("%lld\n",s);
}
return 0;
}
Solution of spoj black widow rings ,bwidow
#include<iostream>
using namespace std;
int main()
{
long int t,n,i,j,count;
cin>>t;
while(t--)
{
cin>>n;
long int a[n][2];
for(i=0;i<n;i++)
{
for(j=0;j<2;j++)
cin>>a[i][j];
}
for(i=0;i<n;i++)
{
count=0;
for(j=0;j<n;j++)
{
if(i!=j)
{
if(a[i][0]>a[j][1])
count++;
}
}
if(count==n-1)
break;
}
if(count==n-1)
cout<<i+1<<endl;
else
cout<<"-1"<<endl;
}
return 0;
}
using namespace std;
int main()
{
long int t,n,i,j,count;
cin>>t;
while(t--)
{
cin>>n;
long int a[n][2];
for(i=0;i<n;i++)
{
for(j=0;j<2;j++)
cin>>a[i][j];
}
for(i=0;i<n;i++)
{
count=0;
for(j=0;j<n;j++)
{
if(i!=j)
{
if(a[i][0]>a[j][1])
count++;
}
}
if(count==n-1)
break;
}
if(count==n-1)
cout<<i+1<<endl;
else
cout<<"-1"<<endl;
}
return 0;
}
Tuesday, 13 January 2015
spoj solution of Beehive Numbers problem BEENUMS
solution of Beehive Numbers problem
#include<iostream>
using namespace std;
int main()
{
long long int n,s,i;
while(1)
{
cin>>n;
if(n==-1)
break;
else
{
i=0;s=1;
while(1)
{
s=s+6*i;
if(s>n)
{
cout<<"N"<<endl;
break;
}
else if(s==n)
{
cout<<"Y"<<endl;
break;
}
else
i=i+1;
}
}
}
return 0;
}
spoj solution of Break the Chocolate problem BC
solution of Break the Chocolate problem
#include<iostream>
#include<cmath>
int knife(int);
using namespace std;
int main()
{
long long int l,w,h,t,s1,s2,j;
cin>>t;
j=1;
while(t--)
{
cin>>l>>w>>h;
s1=(l*w-1)+(l*w*(h-1));
s2=knife(l)+knife(w)+knife(h);
cout<<"Case"<<" "<<"#"<<j<<":"<<" "<<s1<<" "<<s2<<endl;
j++;
}
return 0;
}
int knife( int s)
{
int i;
i=0;
while(1)
{
if(s<=pow(2,i))
break;
else
i++;
}
return i;
}
spoj solution of atoms in the lab problem ATOMS
solution of atoms in the lab
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
long long int n,k,m,t,s,p;
cin>>p;
while(p--)
{
cin>>n>>k>>m;
t=0;
if(m<n)
cout<<"0"<<endl;
else
{
while(1)
{
s=m/(n*pow(k,t));
if(s==0)
break;
else
t++;
}
cout<<t-1<<endl;
}
}
return 0;
}
Spoj solution of Army Strength problem army
Solution of Army Strength problem
#include<iostream>
using namespace std;
int main()
{
int t,a,b,temp,j,k;
cin>>t;
while(t--)
{
cin>>a>>b;
int g[a],m[b];
for(j=0;j<a;j++)
{
cin>>g[j] ;
}
for( j=0;j<b;j++)
{
cin>>m[j];
}
for(j=0,k=j+1;j<a-1;j++,k++)
{
if (g[j]>g[k])
{
temp=g[k];
g[k]=g[j];
g[j]=temp;
}
}
for(j=0,k=j+1;j<b-1;j++,k++ )
{
if(m[j]>m[k])
{
temp=m[k];
m[k]=m[j] ;
m[j] =temp;
}
}
if(g[a-1]>=m[b-1])
{
cout<<"Godzilla"<<endl;
}
else
{
cout<<"MechaGodzilla"<<endl;
}
}
return 0;
}
Solution of AP - Complete The Series (Easy) problem AP2
AP - Complete The Series (Easy) problem
#include<iostream>
using namespace std;
int main()
{
long long int a3,l,s,t,n,d,a,m,i;
cin>>t;
while(t--)
{
cin>>a3>>l>>s;
n=(2*s)/(a3+l) ;
d=(l-a3)/(n-5);
a=a3-2*d;
cout<<n<<endl;
for(i=1;i<=n;i++)
{
m=a+(i-1)*d;
cout<<m<<" ";
}
}
return 0;
}
Spoj solution of Christmas Play problem AMR10G
Christmas Play problem
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
long long int i,k,n,t,temp,temp2;
cin>>t;
while(t--)
{
cin>>n>>k;
long long int a[n];
for(i=0;i<n;i++)
cin>>a[i];
sort(a,a+n);
temp=(a[k-1]-a[0]);
if(k==1)
cout<<temp<<endl;
else
{
for(i=0;i<n-k;i++)
{
temp2=(a[i+k]-a[i+1]);
if(temp>temp2)
temp=temp2;
}
cout<<temp<<endl;
}
}
return 0;
}
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
long long int i,k,n,t,temp,temp2;
cin>>t;
while(t--)
{
cin>>n>>k;
long long int a[n];
for(i=0;i<n;i++)
cin>>a[i];
sort(a,a+n);
temp=(a[k-1]-a[0]);
if(k==1)
cout<<temp<<endl;
else
{
for(i=0;i<n-k;i++)
{
temp2=(a[i+k]-a[i+1]);
if(temp>temp2)
temp=temp2;
}
cout<<temp<<endl;
}
}
return 0;
}
Spoj solution of Alice Sieve problem ALICESIE
Alice Sieve problem
#include<iostream>using namespace std;
int main()
{
long int t,n,m;
cin>>t;
while(t--)
{
cin>>n;
if(n%2==0)
{
m=n/2;
cout<<m<<endl;
}
else
{
m=n/2+1;
cout<<m<<endl;
}
}
return 0;
}
Spoj solution of Adding Reversed Numbers problem ADDREV
Adding Reversed Numbers
#include<iostream>
using namespace std;
int main()
{
int t;
cin>>t;
while(t!=0)
{
int m,n,revm=0,revn=0,sum=0,rev=0;
cin>>m>>n;
while(m!=0)
{
revm=revm*10+m%10;
m=m/10;
}
while(n!=0)
{
revn=revn*10+n%10;
n=n/10;
}
sum=revm+revn;
while(sum!=0)
{
rev=rev*10+sum%10;
sum=sum/10;
}
cout<<"\n"<<rev;
t--;
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int t;
cin>>t;
while(t!=0)
{
int m,n,revm=0,revn=0,sum=0,rev=0;
cin>>m>>n;
while(m!=0)
{
revm=revm*10+m%10;
m=m/10;
}
while(n!=0)
{
revn=revn*10+n%10;
n=n/10;
}
sum=revm+revn;
while(sum!=0)
{
rev=rev*10+sum%10;
sum=sum/10;
}
cout<<"\n"<<rev;
t--;
}
return 0;
}
Solution of What’s Next spoj problem ACPC10A
Solution of What’s Next spoj problem ACPC10A
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int a[3],n,i,d,r;
while(1)
{
for( i=0;i<3;i++)
{
cin>>a[i];
}
if(a[0]==0&&a[1]==0&&a[2]==0)
break;
else
{ d=a[1]-a[0];
r=a[2]-a[1];
if(d==r)
{
n=a[0]+3*d;
cout<<" AP "<<n<<endl;
}
else
{
n=a[0]*pow((a[1]/a[0]),3);
cout<<" GP "<<n<<endl;
}
}
}
return 0;
}
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int a[3],n,i,d,r;
while(1)
{
for( i=0;i<3;i++)
{
cin>>a[i];
}
if(a[0]==0&&a[1]==0&&a[2]==0)
break;
else
{ d=a[1]-a[0];
r=a[2]-a[1];
if(d==r)
{
n=a[0]+3*d;
cout<<" AP "<<n<<endl;
}
else
{
n=a[0]*pow((a[1]/a[0]),3);
cout<<" GP "<<n<<endl;
}
}
}
return 0;
}
Spoj solution abs(a-b) I problem ABSP1
Spoj easiest solution of ABSP1
#include<iostream>
using namespace std;
int main()
{
long long int t,n,m,s,count,i;
cin>>t;
while(t--)
{
count=0;
cin>>n;
long long int a[n];
s=0;
m=0;
for(i=0;i<n;i++)
{
cin>>a[i];
s=s+a[i];
}
for(i=0;i<n-1;i++)
{
count=count+(s-m)-((n-i)*a[i]);
m=m+a[i];
}
cout<<count<<endl;
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
long long int t,n,m,s,count,i;
cin>>t;
while(t--)
{
count=0;
cin>>n;
long long int a[n];
s=0;
m=0;
for(i=0;i<n;i++)
{
cin>>a[i];
s=s+a[i];
}
for(i=0;i<n-1;i++)
{
count=count+(s-m)-((n-i)*a[i]);
m=m+a[i];
}
cout<<count<<endl;
}
return 0;
}
Sunday, 11 January 2015
Subscribe to:
Posts (Atom)