Tuesday, May 3, 2016

Published May 03, 2016 by with 0 comment

UVA 10008 - What's Cryptanalysis? -solution

#include<cstdio>
#include<string.h>
using namespace std;

int main()
{
     char s[100000];
     int t,a[95];
     memset(a,0,sizeof(a));
     scanf("%d",&t);
     getchar();
     while (t--)
     {
          gets(s);
          int l=strlen(s);
          for (int i=0;i<l;i++)
          {
               if (s[i]>64 && s[i]<91)
                    a[s[i]]++;
               else if (s[i]>96 &&s[i]<123)
                    a[s[i]-32]++;
          }
     }
     for (int i=0;i<26;i++)
     {
          int max=0;
          for (int j=65;j<91;j++)
            if (max<a[j]) max=a[j];
          if (max==0) break;
          for (int j=65;j<91;j++)
          {
               if (max==a[j])
               {
                    printf("%c %d\n",j,max);
                    a[j]=0;
               }
          }
     }
     return 0;
}
Read More
      edit

Monday, May 2, 2016

Published May 02, 2016 by with 1 comment

UVA. 499 - What's The Frequency, Kenneth? -solution

#include<cstdio>
#include<string.h>
using namespace std;

int main()
{
     char s[100000];
     int a[125];
     while (gets(s))
     {
          memset(a,0,sizeof(a));
          int l=strlen(s);
          for (int i=0;i<l;i++)
          {
               if ((s[i]>64 && s[i]<91) || (s[i]>96 &&s[i]<123))
                a[s[i]]++;
          }
          int max=0;
          for (int i=65;i<123;i++)
             if (max<a[i]) max=a[i];
          for (int i=65;i<123;i++)
             if (a[i]==max) printf("%c",i);
          printf(" %d\n",max);
     }
     return 0;
}
Read More
      edit
Published May 02, 2016 by with 0 comment

UVA 10019 - Funny Encryption Method - solution

#include<cstdio>
using namespace std;

int ones(int n)
{
     int cnt=0;
     while (n)
     {
          if (n%2==1) cnt++;
          n/=2;
     }
     return cnt;
}

int hex_to_dec(int n)
{
     int temp=0,t=1;
     while (n)
     {
          temp+=(n%10)*t;
          t*=16;
          n/=10;
     }
     return temp;
}

int main()
{
   int t,n,b1,b2;
   scanf("%d",&t);
   while (t--)
   {
      scanf("%d",&n);
      b1=ones(n);
      n=hex_to_dec(n);
      b2=ones(n);
      printf("%d %d\n",b1,b2);
  }
  return 0;
}
Read More
      edit
Published May 02, 2016 by with 0 comment

UVA 11991: Easy Problem from Rujia Liu? - Slution

#include<cstdio>
#include<vector>
using namespace std;

#define MX 1000005

int main()
{
    int size, query,b;
    while (scanf("%d%d",&size,&query)!=EOF)
    {
        vector <int> arr[MX];
        for (int i=1;i<=size;i++)
        {
        scanf("%d",&b);
        arr[b].push_back(i);
        }

        while (query--)
        {
            int occur,value;
            scanf("%d%d",&occur,&value);
            if (occur>arr[value].size()) printf("0\n");
            else printf("%d\n",arr[value][occur-1]);
        }
    }
    return 0;
}
Read More
      edit

Sunday, May 1, 2016

Published May 01, 2016 by with 0 comment

UVA 11494 : Queen - solution

#include<cstdio>
using namespace std;

int main()
{
    int x1,x2,y1,y2;
    while (scanf("%d%d%d%d",&x1,&y1,&x2,&y2)==4)
    {
        if (!x1 && !x2 && !y1 && !y2) break;
        int delx=x1-x2;
        int dely=y1-y2;
        if (delx<0) delx*=-1;
        if (dely<0) dely*=-1;
        if (x1==x2 && y1==y2) printf("0\n");
        else if (delx==dely || x1==x2||y1==y2 )
        printf("1\n");
        else printf("2\n");
    }
    return 0;
}
Read More
      edit
Published May 01, 2016 by with 0 comment

uva 10209 - Is This Integration ? - solution

#include<stdio.h>
#include<math.h>
#define pi acos(-1)

int main()
{
    double a;
    while (scanf("%lf",&a)!=EOF)
    {
        double x,y,z,a2;
        a2=a*a;
        z=(a2*(12-2*pi-3*sqrt(3)))/3.0;
        y=(a2*(pi+6*sqrt(3)-12))/3.0;
        x=a2-y-z;
        printf("%.3lf %.3lf %.3lf\n",x,y,z);
    }
    return 0;
}
Read More
      edit

Friday, April 29, 2016

Published April 29, 2016 by with 0 comment

UVA10163 : Last Digit -solution

#include<cstdio>
#include<math.h>
#include<string.h>
using namespace std;

int main()
{
    int a[21]={4,1,5,2,8,3,9,2,8,7,7,8,4,7,3,8,4,1,5,4,4};
    int a4[5]={0,4,8,2,6};
    char n[111];
    while (scanf("%s",n)!=EOF)
    {
        int l=strlen(n);
        if (l==1 && n[0]=='0') return 0;
        int d,r,ans;
        if (l==1) d=n[0]-48;
        else if (l==2) d=(n[0]-48)*10+n[1]-48;
        else d=(n[l-3]-48)*100+(n[l-2]-48)*10+n[l-1]-48;
        if (d==0)
        {
        printf("0\n");
        continue;
        }
        r=d/20;
        d=d%20;
        if (d==0) r--;
        r=r%5;
        ans=(a[d]+a4[r])%10;
        printf("%d\n",ans);
    }
    return 0;
}
Read More
      edit