2013年10月26日 星期六

[UVA] 913 - Joana and the Odd Numbers

/*20131026 hanting*/
#include <iostream>
using namespace std;
int main()
{
    long long num;//要用long long //用int會WA
    while(cin>>num)
    {
        long long c=(1+num)/2;//第c行
        long long s=0;//第c行最後一個數
        for(long long i=2;i<=c;i++)
        {
            s+=2*(i*2-1);
        }
        if(num == 1)
        {
            cout<<num<<endl;
        }
        else
        {
            cout<<(s-1)*3<<endl;
        }

    }
    return 0;
}

[UVA] 10340 - All in All

/*20131026 hanting*/
#include <iostream>
using namespace std;
int main()
{
    string s,t;
    while(cin>>s>>t)
    {
        int ch=0,a=0;
        for(int i=0;i<s.size();i++)
        {
            for(int j=a;j<t.size();j++)
            {
                if(s[i] == t[j])
                {
                    ch++;
                    a=j+1;
                    j=t.size();
                }
            }
        }
        if(ch == s.size())
        {
            cout<<"Yes"<<endl;
        }
        else
        {
            cout<<"No"<<endl;
        }
    }
    return 0;
}

2013年10月25日 星期五

[UVA] 272 - TEX Quotes

/*20131026 hanting*/
#include <iostream>
using namespace std;
int main()
{
    int dk=1;
    string s;
    while(cin>>s)
    {
        for(int i=0;i<s.size();i++)
        {

           if(s[i] == '"' && dk==1)
           {
              cout<<"``";dk=2;
           }
           else if (s[i] == '"' && dk==2)
           {
              cout<<"''";dk=1;
           }
           else
           cout<<s[i];
        }
        cout<<endl;

    }
    return 0;
}

[UVA] 100 - The 3n + 1 problem

/*20131026 hanting*/
#include <iostream>
using namespace std;
typedef long long L;
int main()
{
    L a,A,b,m;
    while(cin>>a>>b)
    {
        L MAX=0;
        cout<<a<<" "<<b;
        if(a>b)
        {
            A=a;
            a=b;
            b=A;
        }
        for(L i=a;i<=b;i++)
        {
             A=i;
             L cycle_length=1;
             while(A!=1)
            {
                cycle_length++;
                if(A%2==1)
                {
                    A=A*3+1;
                }
                else
                {
                    A/=2;
                }
            }
            m=cycle_length;
            if(m>MAX)
            {
                MAX=m;
            }
        }

        cout<<" "<<MAX<<endl;
    }
    return 0;
}

[UVA] 458 - The Decoder

/*20131026 hanting*/
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    char Input_Letters[10000];
    while(cin.getline(Input_Letters,10000))
    {
        int L=strlen(Input_Letters);
        for(int i=0;i<L;i++)
        {
               Input_Letters[i]-=7;;
               cout<<Input_Letters[i];
        }
        cout<<endl;
    }
    return 0;
}

2013年10月19日 星期六

[UVA] 591 - Box of Bricks

/*20131019 hanting*/
//這題要把每一個積木堆都變成一樣高度
//我的想法是先算出每一堆積木的數量的平均,然後再考慮哪幾堆少於平均的
//如果有一堆少於平均,那就是高於平均的一堆要把一個積木拿來給少於平均的那一堆,此時移動積木數+1
#include <iostream>
using namespace std;
int main()
{
    int k,t=0;
    while(cin>>k)//輸入次數(共有幾堆)
    {
        int total=0,p,acount=0,again=1;
        t++;
        if(k==0)//題目說輸入0就表示結束
        {
            return 0;
        }
        int a[k];//a[k]表示第幾堆
        for(int i=0;i<k;i++)
        {
            cin>>a[i];//輸入a[i]的積木的數量,其中i=0~k-1,就是有k組積木堆
            total+=a[i];//算出總共多少積木
        }
        p=total/k;//每一堆積木的平均值

        while(again==1)//如果again=1,再跑一次
        {
             again=0;
            for(int i=0;i<k;i++)//如果該堆積木少於平均數,那就將該堆積木堆+1,移動次數+1
            {
                if(a[i]<p)
                {
                    a[i]++;
                    acount++;
                }
            }
            for(int i=0;i<k;i++)//掃描美意堆積木是否還有少於平均數堆,若有again=1
            {
                if(a[i]<p)
                     again=1;
            }
        }
        cout<<"Set #"<<t<<endl;
        cout<<"The minimum number of moves is "<<acount<<"."<<endl<<endl;//這裡要兩個endl喔
    }
    return 0;
}

2013年10月18日 星期五

[UVA] 10018 - Reverse and Add

/*20131019 hanting*/
#include <iostream>
using namespace std;

long long R(long long a)//1234變成4321的函數
{
    long long Ra=0;
    while(a)
    {
        Ra*=10;
        Ra+=a%10;
        a/=10;

    }

    return Ra;
}

int main()
{
    long long a[10000],I_times,i=0,c[10000]={0};
    cin>>I_times;
    for(i=1;i<=I_times;i++)
    {
        cin>>a[i];
        if(a[i] == R(a[i]))
        {
            c[i]++;
            a[i]=a[i]+R(a[i]);
        }
        while(a[i] != R(a[i]))
        {
            c[i]++;
            a[i]=a[i]+R(a[i]);
        }
    }

    for(i=1;i<=I_times ;i++)
    {

        cout<<c[i]<<" "<<a[i]<<endl;
    }
    return 0;
}

2013年10月5日 星期六

[UVA] 488 - Triangle Wave

/*20131006 hanting*/
#include <iostream>
using namespace std;
int main()
{
    int TestQuantity=0;
    int i,j,k,l,m,A[1000],F[1000];
    cin>>TestQuantity;

    for(i=0;i<TestQuantity;i++)
    {
       cin>>A[i]>>F[i];
    }
    for(i=0;i<TestQuantity;i++)
    {
        while(F[i]--)
        {
            for(j=1;j<=A[i];j++)
            {
                for(k=0;k<j;k++)
                {
                    cout<<j;
                }
                cout<<endl;
            }
            for(l=1;l<A[i];l++)
            {
                for(m=A[i]-l;m>0;m--)
                {
                    cout<<A[i]-l;
                }
                cout<<endl;
            }
            if(i !=TestQuantity -1 || F[i]!=0)
            cout<<endl;
        }
    }


    return 0;
}

[UVA] 494 - Kindergarten Counting Game

/*20131005 hanting*/
#include <iostream>
#include <stdio.h>//gets()
#include <string.h>//strlen
using namespace std;

int main()
{
    char wd[1000000];
    while(gets(wd))//讀取字串的每一個字元
    {
        int l,x=0,space=0;
        l=strlen(wd);//wd的長度
        for(int i=0;i<l;i++)
        {
            //判別有多少字串//
            if(wd[i]>='a' && wd[i]<='z'
               || wd[i]>='A' && wd[i]<='Z')
                x=1;
            else
            {
                space=space+x;
                x=0;
            }
        }
        space=space+x;
        cout<<space<<endl;
    }

    return 0;
}