Thursday, June 9, 2016

Published June 09, 2016 by with 0 comment

uva 10905 solution

#include<string>
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;

bool cmp(string x,string y)
{
    string t1=x+y;
    string t2=y+x;
    if (t1>t2) return true;
    return false;
}

int main()
{
    int n;
    string s[55];
    while (scanf("%d",&n)!=EOF)
    {
        if (n==0) break;
        for (int i=0;i<n;i++)
        cin<<s[i];
        sort(s,s+n,cmp);
        for (int i=0;i<n;i++) cout<<s[i];
        cout<<endl;
    }
    return 0;
}
Read More
      edit
Published June 09, 2016 by with 0 comment

uva 10107 solution

#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
typedef unsigned long long llu;

int main()
{
    vector <llu>v;
    vector <llu>::iterator low,it;
    llu n;
    int t=1;
    while (scanf("%llu",&amp;n)!=EOF)
    {
        if (t==1) v.push_back(n);
        else
        {
            low=lower_bound(v.begin(),v.end(),n);
            v.insert(low,n);
        }
        if (t%2)
        {
            printf("%llu\n",v[t/2]);
        }
        else printf("%llu\n",(v[(t-1)/2]+v[(t+1)/2])/2);
        t++;
    }
    return 0;
}
Read More
      edit
Published June 09, 2016 by with 0 comment

uva 541 - Error Correction -solution

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

int main()
{
    int n,bit[100][100];
   
    while (scanf("%d",&n)==1)
    {
        if (n==0) return 0;
        int a,r,c,cntr,cntc;
        int row[n],col[n];
        memset(row,0,sizeof(row));
        memset(col,0,sizeof(col));
        for (int i=0;i<n;i++)
        {
            for (int j=0;j<n;j++)
            {
                scanf("%d",&a);
                bit[i][j]=a;
                row[i]+=a;
                col[j]+=a;
            }
        }
        cntr=0;
        cntc=0;
        for (int i=0;i<n;i++)
        {
            if (row[i]%2)
            {
                cntr++;
                r=i;
            }
            if (col[i]%2)
            {
                cntc++;
                c=i;
            }
        }
       
        if (cntr==0 && cntc==0) printf("OK\n");
        else if (cntr==1 && cntc==1) printf("Change bit (%d,%d)\n",r+1,c+1);
        else printf("Corrupt\n");
       
    }
    return 0;
   
}
Read More
      edit
Published June 09, 2016 by with 0 comment

uva 101 - The Blocks Problem -solution

#include <cstdio>
#include <cstring>
#include <string>
#include <stack>
using namespace std;

int block[25];

stack<int> s[25], tmp;

void move_onto(int a, int b);
void move_over(int a, int b);
void pile_onto(int a, int b);
void pile_over(int a, int b);

int main()
{
        int n, a, b;
        char c1[10], c2[10];

        while(scanf("%d",&n)==1)
        {
                for(int i = 0; i < n; i++)
                {
                        s[i].push(i);
                        block[i] = i;
                }

                while(scanf("%s",c1)!=EOF)
                {
                         if(c1[0] == 'q')
                                 break;

                         scanf("%d %s %d",&a,c2,&b);

                         if(a != b && block[a] != block[b])
                         {
                                 if(c1[0] == 'm')
                                 {
                                         if(c2[1] == 'n')
                                                 move_onto(a,b);
                                         else
                                                 move_over(a,b);
                                 }
                                 else
                                 {
                                          if(c2[1] == 'n')
                                                  pile_onto(a,b);
                                          else
                                                  pile_over(a,b);
                                  }
                         }
               }

                for(int i = 0; i < n; i++)
               {
                       printf("%d:",i);
                       while(!s[i].empty())
                       {
                                tmp.push(s[i].top());
                                s[i].pop();
                        }

                        while(!tmp.empty())
                        {
                                printf(" %d",tmp.top());
                                tmp.pop();
                        }
                        printf("\n");
                 }
        }
        return 0;
}

void move_onto(int a, int b)
{
        while(s[block[a]].top() != a)
        {
                int t = s[block[a]].top();
                s[t].push(t);
                block[t] = t;
                s[block[a]].pop();
        }

        while(s[block[b]].top() != b)
        {
                int t = s[block[b]].top();
                s[t].push(t);
                block[t] = t;
                s[block[b]].pop();
        }

        s[block[b]].pop();
        block[b] = b;
        s[b].push(b);

        s[b].push(s[block[a]].top());
        s[block[a]].pop();
        block[a] = b;
}

void move_over(int a, int b)
{
        while(s[block[a]].top() != a)
        {
                int t = s[block[a]].top();
                s[t].push(t);
                block[t] = t;
                s[block[a]].pop();
        }

        s[block[b]].push(s[block[a]].top());
        s[block[a]].pop();
        block[a] = block[b];
}

void pile_onto(int a, int b)
{
        while(s[block[b]].top() != b)
        {
                 int t = s[block[b]].top();
                 s[t].push(t);
                 block[t] = t;
                 s[block[b]].pop();
         }

          s[block[b]].pop();
          block[b] = b;
          s[b].push(b);

          while(s[block[a]].top() != a)
          {
                  tmp.push(s[block[a]].top());
                  s[block[a]].pop();
          }

          tmp.push(s[block[a]].top());
          s[block[a]].pop();

          while(!tmp.empty())
          {
                  s[block[b]].push(tmp.top());
                  block[tmp.top()] = block[b];
                  tmp.pop();
          }
}

void pile_over(int a, int b)
{
        while(s[block[a]].top() != a)
        {
                tmp.push(s[block[a]].top());
                s[block[a]].pop();
        }

        tmp.push(s[block[a]].top());
        s[block[a]].pop();

        while(!tmp.empty())
        {
                s[block[b]].push(tmp.top());
                block[tmp.top()] = block[b];
                tmp.pop();
        }
}
Read More
      edit

Wednesday, June 8, 2016

Published June 08, 2016 by with 0 comment

UVA 10050 : Hartals - solution

#include<cstdio>
using namespace std;

int main()
{
    int t;
    scanf("%d",&t);
    while (t--)
    {
        int n,q,p,r,count;
        scanf("%d",&n);
        int a[n+1]={0};
        scanf("%d",&p);
        count=0;
        while (p--)
        {
            scanf("%d",&q);
            for (int i=q;i<=n;i+=q)
            {
                r=i%7;
                if (a[i]==0 && r!=0 && r!=6)
                {
                    count++;
                    a[i]=1;
                }
            }
        }
        printf("%d\n",count); 
    }
    return 0;
}
Read More
      edit

Tuesday, June 7, 2016

Published June 07, 2016 by with 0 comment

uva 12578 solution

#include<cstdio>
#include<cmath>
#define pi acos(-1)
using namespace std;
int main()
{
    int t,l;
    scanf("%d",&t);
    while (t--)
    {
        scanf("%d",&l);
        double green,red,len,wid,r;
        len=(double)l;
        wid=len*0.60;
        r=len/5.0;
        red=pi*r*r;
        green=len*wid-red;
        printf("%.2lf %.2lf\n",red,green);
    }
    return 0;
}
Read More
      edit

Sunday, June 5, 2016

Published June 05, 2016 by with 0 comment

uva 490 - Rotating Sentences - solution

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

int main()
{
    char s[105][105];
    int k=0;
    int max_len=-1;
    while (gets(s[k]))
    {
        int len=strlen(s[k]);
        if (len>max_len) max_len=len;
        for (int i=len;i<102;i++)
        s[k][i]=' ';
        k++;
    }
    for (int i=0;i<max_len;i++)
    {
        for ( int j=k-1;j>=0;j--)
        {
            printf("%c",s[j][i]);
        }
        printf("\n");
    }
    return 0;
}
Read More
      edit