9Google AdSense

ACM Problem Solution : How old are you? - 11219

View Code
View Problem
-------------------------------------------------------------------------------------

  1. #include<cstdio>
  2. #include<iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.     int day,month,year,age,t,day1,month1,year1,i;
  9.     char ch;
  10.     scanf("%d",&t);
  11.     i=0;
  12.     while(t--)
  13.     {
  14.         i++;
  15.         scanf("%d%c%d%c%d",&day,&ch,&month,&ch,&year);
  16.         scanf("%d%c%d%c%d",&day1,&ch,&month1,&ch,&year1);
  17.         age=year-year1;
  18.         if((month1>month)||((month==month1)&&(day<day1)))
  19.             age=age-1;
  20.         if(age<0)
  21.             cout<<"Case #"<<i<<":"<<" "<<"Invalid birth date"<<endl;
  22.         else if(age>130)
  23.             cout<<"Case #"<<i<<":"<<" "<<"Check birth date"<<endl;
  24.         else
  25.             cout<<"Case #"<<i<<":"<<" "<<age<<endl;
  26.     }
  27.     return 0;
  28. }
  29.  
  30.  

ACM Problem Solution : Generating Fast - 10098

View Problem
View Code
-------------------------------------------------------------------------------------------------
  1. #include<cstdio>
  2. #include<iostream>
  3. #include<algorithm>
  4. #include<cstring>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     int t,len;
  11.     char ch;
  12.     char str[10000];
  13.     scanf("%d%c",&t,&ch);
  14.     while(t--)
  15.     {
  16.         gets(str);
  17.         len=strlen(str);
  18.         sort(str,str+len);
  19.         do
  20.         {
  21.             cout<<str<<endl;
  22.         }while(next_permutation(str,str+len));
  23.         cout<<endl;
  24.     }
  25.     return 0;
  26. }

ACM Problem Solution : Pig-Latin - 492

  1. #include<stdio.h>
  2. #include<ctype.h>
  3. int isvowel(char ch)
  4. {
  5.    int flag =0;
  6.    if(ch=='a'|| ch=='A'||ch=='e'|| ch=='E'||ch=='i'|| 
  7. ch=='I'||ch=='u'|| ch=='U'||ch=='o'|| ch=='O')
  8.        flag = 1;
  9.    return flag;
  10. } 
  11. int main()
  12. {
  13.     char ch,save;
  14.     int n;
  15.     while(1)
  16.     {
  17.         n=scanf("%c",&ch);
  18.         if(n!=1)
  19.             break;
  20.         if(isvowel(ch))
  21.         {
  22.             printf("%c",ch);
  23.             while(1)
  24.         {
  25.                 scanf("%c",&ch);
  26.                 if(!isalpha(ch))
  27.                     break;
  28.                 printf("%c",ch);
  29.             }   
  30.             printf("ay%c",ch);
  31.         }
  32.         else if(isalpha(ch))
  33.         {
  34.               save=ch;
  35.               while(1)
  36.               {
  37.                 scanf("%c",&ch);
  38.                 if(!isalpha(ch))
  39.                     break;
  40.                 printf("%c",ch);
  41.               }
  42.               printf("%cay%c",save,ch);
  43.         }
  44.         else
  45.           printf("%c",ch);
  46.     }
  47.     return 0;
  48. }

ACM Problem Solution : ID Codes - 146


View Code
view Problem
----------------------------------------------------------------------------
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<algorithm>
  4. #include<cstring>
  5. using namespace std;
  6. int main()
  7. {
  8.     char str[200];
  9.     int len;
  10.     while(gets(str)!=NULL)
  11.    {
  12.         if(str[0]=='#')
  13.                  break;
  14.         len=strlen(str);
  15.        if(next_permutation(str,str+len)==1)
  16.               printf("%s\n",str);
  17.        else
  18.              printf("No Successor\n");
  19.    }
  20.   return 0;
  21. }

12554 - A Special "Happy Birthday" Song!!!

View Problem
View Code
---------------------------------------------------------------------
  1. #include<stdio.h>
  2. int main()
  3. {
  4.    int t,tt,k,p,ll,h,hh,se,st;
  5.    char happy[20][100]={"Happy","birthday","to","you","Happy","birthday","to",
  6. "you","Happy","birthday","to","Rujia","Happy","birthday","to","you"};
  7.    char inp[200][200],ch;
  8.    scanf("%d%c",&t,&ch);
  9.    for(k=0;k<t;k++)
  10.        gets(inp[k]);
  11.    if(t<16)
  12.    { for(p=0,ll=0;p<16;p++,ll++)
  13.    {
  14.     if(ll==t)
  15.         ll=0;
  16.    printf("%s: %s\n",inp[ll],happy[p]);
  17.    } }
  18.   else
  19.   {
  20.      for(h=0,hh=0;h<t;h++,hh++)
  21.      {  if(hh>15)
  22.         hh=0;
  23.     printf("%s: %s\n",inp[h],happy[hh]);
  24.       }
  25.       if(hh!=0)
  26.       {
  27.      for(st=hh,se=0;st<16;st++,se++)
  28.         printf("%s: %s\n",inp[se],happy[st]);
  29.       }
  30.    }
  31.    return 0;
  32. }
  33.  

424 - Integer Inquiry

View Code
View Problem
---------------------------------------------------------------------
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include<math.h>
  5. #define MAX 300
  6. void rev(char *from,char *to)
  7. {
  8.     int len,i;
  9.     len=strlen(from);
  10.     for(i=0;i<len;i++)
  11.     to[i]=from[len-i-1];
  12.     to[len]='\0';
  13. }
  14. void add(char *first,char *sec,char *result)
  15. {
  16.     char F[MAX],S[MAX],R[MAX];
  17.     int flen,slen,now,extra,sum;
  18.     flen=strlen(first);
  19.     slen=strlen(sec);
  20.     rev(first,F);
  21.     rev(sec,S);
  22.     for(now=0,extra=0;(now<flen&&now<slen);now++)
  23.     {
  24.     sum=(F[now]-'0')+(S[now]-'0')+extra;
  25.     R[now]=sum%10+'0';
  26.     extra=sum/10;
  27.     }
  28.     for(;now<flen;now++)
  29.     {
  30.      sum=F[now]+extra-'0';
  31.     R[now]=sum%10+'0';
  32.     extra=sum/10;
  33.     }
  34.      for(;now<slen;now++)
  35.     {
  36.      sum=S[now]+extra-'0';
  37.     R[now]=sum%10+'0';
  38.     extra=sum/10;
  39.     }
  40.     if(extra!=0)
  41.     R[now++]=extra+'0';
  42.     R[now]='\0';
  43.    if(strlen(R)==0)
  44.     strcpy(R,"0");
  45.     rev(R,result);
  46. }
  47. int main()
  48. {
  49.     static char f[MAX],s[MAX],r[MAX];
  50.     int i,len,t=0;
  51.     while(gets(f)!=NULL)
  52.     {
  53.     if(t==0)
  54.     {
  55.            t++;
  56.            strcpy(s,f);
  57.     }
  58.     else if(f[0]=='0')
  59.           break;
  60.     else{
  61.          add(f,s,r);
  62.          strcpy(s,r);
  63.           }
  64.     }
  65.     len=strlen(r);
  66.     for(i=0;i<len;i++)
  67.         printf("%c",r[i]);
  68.     printf("\n");
  69.     return 0;
  70. }

10931 - Parity

View Problem
View Code

#include<stdio.h>



int main()
{
    long long int num,count,i,s,x,l;
    char  str[200000];
    long long int value[200000];
    while(scanf("%lld",&num)==1)
    {
    if(num==0)
        break;
     if(num==1)
           printf("The parity of 1 is 1 (mod 2).\n");
       else{
    count=0;
    i=0;
    while(num!=0)
    {
        if(num%2==1)
        count++;
        s=num/2;
        value[i++]=num%2;
        num=s;

    }
    x=0;
    for(l=i-1;l>=0;l--)
         str[x++]=value[l]+'0';
       str[x]='\0';
        printf("The parity of %s is %lld (mod 2).\n",str,count);
       }
    }
    return 0;
}

713 Adding Reversed Numbers

View Code
View Problem
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

#define MAX 300


void reverse(char *fnum,char *snum)
{
    int len,i;
    len=strlen(fnum);
    for(i=0;i<len;i++)
    snum[i]=fnum[len-i-1];
    snum[len]='\0';
}

void add(char *first,char *sec,char *result)
{
    char F[MAX],S[MAX],R[MAX];
    int flen,slen,now,extra,sum;
    flen=strlen(first);
    slen=strlen(sec);
    reverse(first,F);
    reverse(sec,S);
    for(now=0,extra=0;(now<flen&&now<slen);now++)
    {
    sum=(F[now]-'0')+(S[now]-'0')+extra;
    R[now]=sum%10+'0';
    extra=sum/10;
    }
    for(;now<flen;now++)
    {
     sum=F[now]+extra-'0';
    R[now]=sum%10+'0';
    extra=sum/10;

    }

     for(;now<slen;now++)
    {
     sum=S[now]+extra-'0';
    R[now]=sum%10+'0';
    extra=sum/10;

    }
    if(extra!=0)
    R[now++]=extra+'0';
    R[now]='\0';
    if(strlen(R)==0)
    strcpy(R,"0");
    reverse(R,result);

}

int main()
{
    char f[MAX],s[MAX],r[MAX],f1[MAX],s1[MAX];
    int i,len,t,flag;
    scanf("%d",&t);
    while(t--)
    {
    scanf("%s%s",&f1,&s1);
    reverse(f1,f);
    reverse(s1,s);
    add(f,s,r);
    len=strlen(r);
    flag=0;
    for(i=len-1;i>=0;i--)
    {
        if((r[i]==48)&&(flag==0));
        else
       {printf("%c",r[i]);
        flag=1;
       }
    }
    printf("\n");

    }
    return 0;
}

12478 - Hardest Problem Ever (Easy)

  1. #include<stdio.h>
  2. int main()
  3. {
  4.     printf("KABIR\n");
  5.     return 0;
  6. }

ACM Problem Solution : 575 Skew Binary

View Code
-------------------------------------------------------------------------
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<math.h>
  4. int main()
  5. {
  6.    char str[20000];
  7.     long long int i,sum,len,n,power,j;
  8.     while(gets(str)!=NULL)
  9.     {
  10.    if(str[0]=='0')
  11.         break;
  12.     else
  13.     {
  14.         len=strlen(str);
  15.         sum=0;
  16.         for(i=len,j=0;i>=1;i--,j++)
  17.         {
  18.          if(str[j]!='0')
  19.          {
  20.           power=(pow(2,i)-1);
  21.           sum=sum+((str[j]-'0')*power);
  22.           }
  23.        }
  24.     printf("%lld\n",sum);
  25.     }
  26.     }
  27.     return 0;
  28. }