2013年12月14日 星期六

[UVA] 10071 - Back to High School Physics

/*20131215 hanting*/
//****************************************//
//                                        //
//  (速度)|        /                     //
//        |      /b.                     //
//       v|..../....                     //
//        |a /.    .                     //
//      vo|/c . d  .                     //
//        |____.____.___                  //
//             t   2t  (時間)             //
//                                        //
//   所求等於  c+b+d = a+c+d = v*2t       //
//****************************************//
#include <iostream>
using namespace std;
int main()
{
    int v,t;
    while(cin>>v>>t)
    {
        cout<<v*t*2<<endl;
    }
    return 0;
}

[UVA] 12468 - Zapping

/*20131215 hanting*/
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    int a,b;
    while(cin>>a>>b && a!=-1 && b!=-1)
    {
        int tem1=abs(b-a);
        int tem2=100-abs(b-a);
        if(tem1>tem2)
        {
            cout<<tem2<<endl;
        }
        else
        {
            cout<<tem1<<endl;
        }
    }
    return 0;
}

[UVA] 11364 - Parking

/*20131215 hanting*/
#include <iostream>
using namespace std;
void change(int &a,int &b)//兩數交換
{
    int tema=a;
    a=b;
    b=tema;
}
void compare(int a[],int n)//排大小
{
    for(int i=1;i<n;i++)
    {
        if(a[i]<a[i-1])
        {
            change(a[i],a[i-1]);
            i=0;
        }
    }
}
int main()
{
    int N;
    cin>>N;
    while(N--)
    {
        int n;
        cin>>n;
        int address[n];
        for(int i=0;i<n;i++)
        {
            cin>>address[i];
        }
        compare(address,n);
        int total=0;
        for(int i=1;i<n;i++)
        {
            total+=address[i]-address[i-1];
        }
        cout<<2*total<<endl;
    }
    return 0;
}

2013年11月30日 星期六

[UVA] 10038 - Jolly Jumpers

/*20131130 hanting*/
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
    int N;
    while(cin>>N)
    {

        int tem[N],x=N,n=N;
        int a[N];
        cin>>a[0];
        if(N==1) //特殊測資
        {
            cout<<"Jolly"<<endl;
        }
        else
        {
            for(int i=1;i<N;i++)
            {
                cin>>a[i];
                tem[i-1]=abs(a[i]-a[i-1]);
            }
            N--;
            while(x) //找出每個數之間的差是否是1到N-1
            {
                for(int i=0;i<=n-1;i++)
                {
                    if(i==n-1)
                    {
                        cout<<"Not jolly"<<endl;
                        x=0;
                        break;
                    }
                    if(tem[i]==N)
                    {
                        N--;
                        break;
                    }
                }
                if(N==0)
                {
                    cout<<"Jolly"<<endl;
                    break;
                }
            }
        }
    }
    return 0;
}

2013年11月29日 星期五

[UVA] 113 - Power of Cryptography

/*20131129 hanting*/
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
    double n,p;
    while(cin>>n>>p)
    {
        cout<<fixed<<setprecision(0)<<pow(p,1/n)<<endl; // 要記得四捨五入不然會wa
    }
    return 0;
}

2013年11月23日 星期六

[UVA] 477 - Points in Figures: Rectangles and Circles

/*20131123 hanting*/
//與476幾乎一樣 只是多一個在圓形內的判斷//
#include <iostream>
using namespace std;
int main()
{
    double x,y; // 測試的兩點
    double r[100][400]; // 設置的矩形對角兩點的座標
    double c[100][400]; // 設置的圓形圓心座標與半徑
    char ch;
    int k=1; //r[][]陣列中的位址
    int s=0; //第s個點
    while(cin>>ch && ch=='r'|| ch=='c')
    {
        if(ch=='r')
        {
           cin>> r[k][1] >> r[k][2] >> r[k][3] >> r[k][4]; //第k個矩形的對角兩點 // 左上 //右下 // r[k][1] 和 r[k][3]是x座標 //r[k][2] 和 r[k][4]是y座標
           k++;
        }
        else if(ch=='c')
        {
           cin>> c[k][1] >> c[k][2] >> c[k][3]; //第k個圓形的圓心座標 // c[k][3]表示半徑
           k++;
        }
    }
    while(cin>>x>>y)
    {
        int ex=1; //判斷點是否在矩形或圓形內
        if(x==9999.9 && y==9999.9) return 0;
        s++; //第s個點
        for(int i=1;i<=k;i++)
        {
            if(x>r[i][1] && x<r[i][3]  &&  y<r[i][2] && y>r[i][4])// 點不能在邊上
            {
                cout<<"Point "<<s<<" is contained in figure "<<i<<endl;
                ex=0;//ex=0時,表示點已經有在其中一矩形或圓形內
            }
            else if( (x-c[i][1])*(x-c[i][1]) + (y-c[i][2])*(y-c[i][2]) <c[i][3]*c[i][3] )// 點不能在邊上
 //x與圓心x座標的距離平方 + y與圓心y座標的距離平方 < 半徑平方 即可說(x,y)在圓內
            {
                cout<<"Point "<<s<<" is contained in figure "<<i<<endl;
                ex=0;//ex=0時,表示點已經有在其中一矩形內
            }
        }
        if(ex==1)
                cout<<"Point "<<s<<" is not contained in any figure"<<endl;
    }
    return 0;
}

[UVA] 476 - Points in Figures: Rectangles

/*20131123 hanting*/
#include <iostream>
using namespace std;
int main()
{
    double x,y; // 測試的兩點
    double r[100][400]; // 設置的矩形對角兩點
    char c;
    int k=1; //r[][]陣列中的位址
    int s=0; //第s個點
    while(cin>>c && c=='r')
    {
        cin>> r[k][1] >> r[k][2] >> r[k][3] >> r[k][4]; //第k個矩形的對角兩點 // 左上 //右下
        k++;
    }
    while(cin>>x>>y)
    {
        int ex=1; //判斷點是否在矩形內
        if(x==9999.9 && y==9999.9) return 0;
        s++; //第s個點
        for(int i=1;i<=k;i++)
        {
            if(x>r[i][1] && x<r[i][3]  &&  y<r[i][2] && y>r[i][4])// 點不能在邊上
            {
                cout<<"Point "<<s<<" is contained in figure "<<i<<endl;
                ex=0;//ex=0時,表示點已經有在其中一矩形內
            }
        }
        if(ex==1)
                cout<<"Point "<<s<<" is not contained in any figure"<<endl;
    }
    return 0;
}

2013年11月22日 星期五

[UVA] 10035 - Primary Arithmetic

/*20131123 hanting*/
#include <iostream>
using namespace std;
int main()
{
    int a,b;
    while(cin>>a>>b)
    {
        if(a==0 && b==0) return 0;
        int Q=0;  //  Q次進位
        int s=0;  //進位時s=1 //  下一位數相加+1
        while(a!=0 || b!=0)
        {
            if((a%10+b%10+s)>=10)
            {
                a/=10;b/=10;
                s=1;
                Q++;
            }
            else
            {
                a/=10;b/=10;
                s=0;
            }
        }
        if(Q==0) cout<<"No carry operation."<<endl;
        else if(Q==1) cout<<"1 carry operation."<<endl;
        else cout<<Q<<" carry operations."<<endl;
    }
    return 0;
}

2013年11月5日 星期二

[UVA] 10696 - f91

/*20131105 hanting*/
#include <iostream>
using namespace std;
int f91(int N)
{
    if(N>=101)
    {
        return N-10;
    }
    else
    {
         return f91( f91( N+11) );
    }
}
int main()
{
    int N;
    while(cin>>N)
    {
    if(N==0) return 0;
    cout<<"f91("<<N<<") = "<<f91(N)<<endl;
    }
    return 0;
}

2013年11月4日 星期一

[UVA] 10783 - Odd Sum

/*20131104 hanting*/
#include<iostream>
using namespace std;
int main()
{
    int T;//T筆測資
    int a,b;//a頭b尾
    cin>>T;
    for(int i=1;i<=T;i++)
    {
        int total=0;//total總
        cin>>a>>b;
        if(a%2 == 0) a+=1;//a若是偶數 取小於它的最大奇數 如a=8  變成 a=9
        if(b%2 == 0) b-=1;//b若是偶數 取大於它的最小偶數 如b=16 變成 b=15
        for(a=a;a<=b;a+=2)
        {
            total+=a;
        }
        cout<<"Case "<<i<<": "<<total<<endl;
    }
    return 0;
}

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;
}

2013年9月21日 星期六

[UVA] 10300 - Ecological Premium

/*20130921 hanting*/
#include <iostream>
using namespace std;
int main()
{
    int n,f,a,b,c ,i=0,j=0,m=0;
    cin>>n;
    int x[n];
    do
    {
       i=0;
       j++;
       m=0;
       cin>>f;
       int k[f];
       do
       {
          i++;
          //cout<<"*"<<i<<":";
          cin>>a>>b>>c;
          k[i-1]=a*c;
       }while(i<f);
       for(i=0;i<f;i++)
       {
          m=k[i]+m;
       }
       x[j-1]=m;
   }while(j<n);
   for(i=0;i<n;i++)
   {
      cout<<x[i]<<endl;
   }
   return 0;
}
/*另*//*20140307 hanting*/
#include<iostream>
using namespace std;
int main()
{
    int N;
    cin>>N;
    while(N--)
    {
        int ans=0;
        int m;
        cin>>m;
        int a,b;
        while(m--)
        {
            cin>>a>>b>>b;
            ans+=a*b;
        }
        cout<<ans<<endl;
    }
    return 0;
}

[UVA] 10055 - Hashmat the Brave Warrior

/*20130920 hanting*/
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
long long int a,b;
while(cin>>a>>b)
{
cout<<abs(a-b)<<endl;
}
return 0;
}