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