here is only basic implementation of problems for beginners. If you have any problem with any solution or any basic concept of programming or you want more efficient solution you can mail me.
my suggestion is not to copy and paste codes from here try to understand the logic and think why you were not able to solve it.

Tuesday 9 June 2015

Amusing numbers

problem statement is here


#include<stdio.h>
#include<math.h>

int main()
{
    long long int a,t,i,j,n,b;
    scanf("%lld",&t);
    while(t--){
               scanf("%lld",&a);
               for(i=1; ;i++){
                        if(a>(pow(2,i)-2) && a<=(pow(2,i+1)-2)){
                                          b=a-(pow(2,i)-2);
                                           break;
                                           }
                        }
                        n=i;
               for(j=1;j<=n;j++){
                                 if(b>pow(2,i-1)){
                                                  printf("6");
                                                  b=b-pow(2,i-1);
                                                  }else{
                                                        printf("5");
                                                        }
                                 i--;
                                 }
               printf("\n");
               }
    return 0;
}

Playing with isosceles triangle

problem statement is here


#include<stdio.h>
#include<math.h>
 int main() {
long int n,t,s,flag,i,c;
scanf("%ld", &t);
while(t--) {
scanf("%ld",&s);
flag=0;
if(s%2==0){
while(s%2==0)
s=s/2;
}
n=(sqrt(s));
for(i=3;i<=n;i+=2){
if(s%i==0){
if(i%4==1)
flag = 1;
while(s%i==0)
s=s/i;
}
if(s==1)
break;
}
if(s!=1&&s%4==1)
flag=1;
if(flag==0){
printf("NO\n");
} else {
printf("YES\n");
}
}
return 0;
}

Counting Triangles

problem statement is here



#include<stdio.h>
int main(){
        long long int totel,x,y;
        scanf("%lld",&x);
        while(x--){
                scanf("%lld",&y);
                totel=(y*(y+2)*(2*y+1))/8;
                printf("%lld\n",totel);
        }
        return 0;
}

Traversing Grid

problem statement is here



#include<stdio.h>
int main(){
    long long int n,m;
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%lld %lld",&n,&m);
        if(n%2==0 && m%2==0){
            if(n>m){
            printf("U\n");
            }else if(m>=n)
                    printf("L\n");
        }
        else if(n%2!=0 && m%2!=0){
            if(n>m)
                printf("D\n");
            else if(m>=n)
                printf("R\n");
        }
        else{
            if(n%2==0 && m%2!=0)
                {
                    if(n>m)
                        printf("D\n");
                    else
                        printf("L\n");
                }
            if(n%2!=0 && m%2==0)
                {
                    if(n<m)
                        printf("R\n");
                    else
                        printf("U\n");
                }
        }
    }
    return 0;
}

To and Fro

problem statement is here



#include<stdio.h>
#include<string.h>

int main()
{
    int a,i,j,n,b,k;
    char ar[210];
    while(1){  
                 scanf("%d",&a);
                 if(a==0)
                 break;
                 else{
                      scanf("%s",ar);
                      b=strlen(ar);
                      j=a;
                      for(i=0;i<a;i++){
                                       printf("%c",ar[i]);
                                       for(k=2; ;k+=2){
                                                if((k*j-i-1)<b){
                                                                printf("%c",ar[k*j-i-1]);
                                                                }else{
                                                                      break;
                                                                      }
                                                if((k*j+i)<b){
                                                              printf("%c",ar[k*j+i]);
                                                              }else{
                                                                    break;
                                                                    }
                                                }
                                       }
                      printf("\n");
                      }
                 }
    return 0;
}
   

Movie Theatre Madness

problem statement is here



#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;

struct compare
{
  bool operator()(const long long & l, const long long & r)
  {
      return l > r;
  }
};

int main(){
priority_queue<long long,vector<long long>, compare > pq;
int n,i,a;
long long ar[100005],max=1;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%lld",&ar[i]);
}
for(i=0;i<n;i++){
pq.push(ar[i]);
while(1){
if(pq.top()==ar[i]){
break;
}else{
max=(max*(ar[i]))%1000000007;
pq.pop();
}
}
}
max=max%1000000007;
printf("%lld\n",max);

return 0;
}

Finding the Tesserect

problem statement is here

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main(){
    int t;
    scanf("%d\n",&t);
    while(t--)
    {
        int n,i;
        scanf("%d\n",&n);
        long long int a,b;
        scanf("%lld",&a);
        char ar[1000000],br[1000000];
        for(i=1;i<n;i++){
            scanf("%lld",&b);
            if(b>a){
                ar[i-1]='G';
            }else if(b==a){
                ar[i-1]='E';
            }else{
                ar[i-1]='L';
            }
            a=b;
        }
        ar[n]='\0';
        scanf("%s",br);
        if(strstr(ar,br))
            printf("YES\n");
        else
        printf("NO\n");
    }
    return 0;
}

A Famous ICPC Team


problem statement is here


#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
   long long int a[4],p;
   int j,i=1;
   while(scanf("%lld ",&a[0])!=EOF)
    {
       
        for(j=1;j<4;j++)
        {  
            scanf("%lld",&a[j]);
           
        }
        sort(a,a+4);
        p=a[3]+a[2];
        printf("Case %d: %lld\n",i,p);
        i++;
    }
    return 0;
}


Balanced base-3


problem statement is here


#include<stdio.h>
int main() {
int t,y,z,n,ar[10000],i;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
z=0;
while(n){
ar[z]=n%3;
z++;
n/=3;
}
ar[z]=0;
for(i=0;i<=z;i++){
if(ar[i]==2){
ar[i]=-1;
ar[i+1]++;
}else if(ar[i]==3){
ar[i]=0;
ar[i+1]++;
}
}
y=0;
for(i=z;i>=0;i--){
if(ar[i]!=0){
y=1;
if(ar[i]==1)
printf("+");
else if(ar[i]==-1)
printf("-");
}
else if(y==1)
printf("0");
}
printf("\n");
}
return 0;
}

War


problem statement is here


#include<stdio.h>
#include<algorithm>
using namespace std;
int main(){
long long int s,ar[100009],br[100009],i,j,z,p,a;
scanf("%lld",&s);
for(i=0;i<s;i++){
scanf("%lld",&ar[i]);
}
for(i=0;i<s;i++){
scanf("%lld",&br[i]);
}
sort(ar,ar+s);
sort(br,br+s);
z=s-1;p=0;
for(i=s-1;i>=0;i--){
if(ar[i]<br[z]){
p++;
z--;
}
}
printf("%lld\n",p);
return 0;
}

WHAT A CO-ACCIDENT


problem statement is here


#include<stdio.h>
int main(){
long long int a,b,t;
scanf("%lld",&t);
while(t--){
scanf("%lld %lld",&a,&b);
if(a%2==0 || b%2==0){
printf("Suresh\n");
}else{
printf("Ramesh\n");
}
}
return 0;
}