[解題] 高中生程式解題系統(解答篇1)

網址:http://zerojudge.tw/
網站介紹: http://it-easy.tw/zerojudge/

目前解答有以下:
a005 a006 a007 a008 a009 a010 a011 a012
a015 a016 a020 a022 a024 a034 a038 a040
a044 a053 

a005: Eva 的回家作業 http://zerojudge.tw/ShowProblem?problemid=a005

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int a[20];
    int t;
    cin >> t ;

    while(t >> 0){

        for (int i = 0 ; i <=3 ; i++)
        {
            cin >> a[i];
        }

        //等差級數
        if((a[3]-a[2]) == (a[2]-a[1]) &&(a[2]-a[1]) == (a[1]-a[0]))
        {
            a[4]= a[3]+(a[3]-a[2]);
            for (int i = 0 ; i <= 4; i ++)
                cout << a[i] <<" " ;
        }
        //等比級數
        else if( (a[3]/a[2]) == (a[2]/a[1]) && (a[2]/a[1]) == (a[1]/a[0]))
        {
            a[4]= a[3]*(a[3]/a[2]);
            for (int i = 0 ; i <= 4; i ++)
                cout << a[i] << " ";
        }

        cout << endl;
        t--;
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}

a006: 一元二次方程式 http://zerojudge.tw/ShowProblem?problemid=a006
#include <cstdlib>
#include <math.h>
#include <iostream>

#define   CHECK(x)   ((x)-(int)(x)== 0   ?   1   :   0)
using namespace std;

int main(int argc, char *argv[])
{
    int a,b,c;

    while(cin >> a >> b >> c){
        int x,y;

        if(CHECK((-b + sqrt(b*b - (4*a*c))) / (2*a))==1)
        {
            x= (-b + sqrt(b*b - (4*a*c))) / (2*a);
            y= (-b - sqrt(b*b - (4*a*c))) / (2*a);

        if (x == y)
            cout <<"Two same roots x=" << x << endl;
        else
            cout << "Two different roots x1=" << x
            cout << " , x2=" << y <<endl;
        }else
        {
            cout<< "No real root" << endl;
        }

    }
    system("PAUSE");
    return EXIT_SUCCESS;
}


a007: 判斷質數 http://zerojudge.tw/ShowProblem?problemid=a007
#include 
#include 

using namespace std;

int main(int argc, char *argv[])
{
    long int a;

    while(cin >> a){
        if(a >= 2 && a <= 2147483647 )
        {
        int j = 0;
        for(int i = 2 ; i <=(a/2);i++)
        {
            if(( a % i  )== 0)
            {
                j++;
                break;
            }
        }
        switch (j){

        case 0:
            cout << "質數" << endl;
            break;
        default:
            cout << "非質數" << endl;
            break;
        }
        }
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}

a009: 解碼器 http://zerojudge.tw/ShowProblem?problemid=a009
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

int main(int argc, char *argv[])
{
    string a ;
    while(getline(cin,a))
    {
        for (int i = 0 ; i<a.length() ; i++)
        {
            a[i]=a[i] - 7;
            cout << a[i];
        }
        cout << endl;
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}

a010: 因數分解 http://zerojudge.tw/ShowProblem?problemid=a010
#include 
#include 
#include 
#include 
using namespace std;

string int2str(int &i) {
    string s;
    stringstream ss(s);
    ss << i;
    return ss.str();
}


int main(int argc, char *argv[])
{
    int a ;
    string b[256];
    while(cin >> a)
    {
        int x,y=0;
        int z=0;
        for (int i = 2 ; i <= a ; i++)
        {
            while( a % i == 0)
            {
                x = i;
                y++;
                a /= i ;
            }
            if(y!=0)
            {
                if(y==1)
                {
                    //cout << x << " * ";
                    b[z++]=int2str(x) ;
                    b[z++]=' ';
                    b[z++]='*';
                    b[z++]=' ';
                    y=0;
                }else
                {
                    b[z++]=int2str(x) ;
                    b[z++]='^';
                    b[z++]=int2str(y) ;
                    b[z++]=' ';
                    b[z++]='*';
                    b[z++]=' ';
                    y=0;
                }
            }
        }
        for(int i = 0 ; i < z-2 ; i ++)
            cout << b[i];
        cout << endl;
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}


a011: 幼稚園的算數遊戲 http://zerojudge.tw/ShowProblem?problemid=a011
#include 
#include 
#include 
using namespace std;

int main(int argc, char *argv[])
{
    string a ;
    while(getline(cin,a))
    {
        int x=0;
        int y=0;

        for (int i = 0 ; i<=a.length() ; i++)
        {
            if((a[i] >= 'A' && a[i] <= 'Z') || (a[i] >= 'a' && a[i] <= 'z'))
                x = 1;
            else
            {
                if( x==1)
                {
                    y++;
                    x=0;
                }
            }
        }
        cout << y << endl;
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}

a012: Hashmat的戰役 http://zerojudge.tw/ShowProblem?problemid=a012
#include 
#include 

using namespace std;

long long int avg(long long int i)
{
    if( i < 0)
        i= i*(-1);
    return i;
}

int main(int argc, char *argv[])
{
    long long int  a,b ;

    while(cin >> a >> b)
    {
        cout << avg(a-b) ;
        cout << endl;
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}

a015: 矩陣的翻轉 http://zerojudge.tw/ShowProblem?problemid=a015
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
    int a,b;
    while(cin >> a >> b)
    {
        int d[a][b];
        for(int i=0; i<a;i++)
            for(int j=0 ; j < b; j++)
                cin>>d[i][j];

        for(int i = 0; i < b ; i++)
        {
            for(int j =0 ; j < a; j++)
                cout << d[j][i] << " ";
            cout << endl;
        }
        cout << endl;
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}

a016: 數獨(SUDOKU) http://zerojudge.tw/ShowProblem?problemid=a016
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
    int x= 0;
    while(x==0)
    {
        int check_num=0;
        int check=0;
        int d[9][9];

        for(int i=0; i<9 ;i++)
            for(int j=0; j<9 ; j++)
                cin>>d[i][j];

        if( cin.eof() )
            break;

        for(int i=0; i<9 ; i++)
        {
            for(int j=0 ; j < 9 ; j++)
                check_num = check_num + d[i][j];

            if (check_num != 45)
            {
                check = 1;
                break;
            }
            check_num=0;
        }

        for(int i=0; i <9; i++)
        {
            for(int j=0 ; j < 9 ; j++)
                check_num = check_num + d[j][i];
            if (check_num != 45)
            {
                check = 1;
                break;
            }
            check_num=0;
        }

        check_num=0;
        int a=0;
        int b=0;
        for(int i=0; i < 3; i++)
        {
            int c;
            c=a+3;
            for( a  ; a < c ; a++)
            {
                for( b = 0 ; b < 3 ; b++)
                {
                    check_num = check_num + d[a][b];
                }
            }

            if (check_num != 45)
            {
                check = 1;
                break;
            }
            check_num=0;
        }

        a=0;
        for(int i=0; i < 3; i++)
        {
            int c;
            c=a+3;
            for( a  ; a < c ; a++)
            {
                for( b=3 ; b < 6 ; b++)
                {
                    check_num = check_num + d[a][b];
                }
            }

            if (check_num != 45)
            {
                check = 1;
                break;
            }
            check_num=0;
        }

        a=0;
        for(int i=0; i < 3; i++)
        {
            int c;
            c=a+3;
            for( a  ; a < c ; a++)
            {
                for( b=6 ; b < 9 ; b++)
                {
                    check_num = check_num + d[a][b];
                }
            }

            if (check_num != 45)
            {
                check = 1;
                break;
            }
            check_num=0;
        }

        switch (check)
        {
            case 1:
                cout << "no" << endl;
                break;
            case 0:
                cout << "yes" << endl;
                break;
        }
        cout << endl;
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}

a020: 身分證檢驗 http://zerojudge.tw/ShowProblem?problemid=a020
#include <cstdlib>
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
int main(int argc, char *argv[])
{
    string a;
    while(getline(cin,a))
    {
        int check=0;
        switch (a[0])
        {
           case 'A': check = 1 + 0 * 9; break;
           case 'B': check = 1 + 1 * 9; break;
           case 'C': check = 1 + 2 * 9; break;
           case 'D': check = 1 + 3 * 9; break;
           case 'E': check = 1 + 4 * 9; break;
           case 'F': check = 1 + 5 * 9; break;
           case 'G': check = 1 + 6 * 9; break;
           case 'H': check = 1 + 7 * 9; break;
           case 'I': check = 3 + 4 * 9; break;
           case 'J': check = 1 + 8 * 9; break;
           case 'K': check = 1 + 9 * 9; break;
           case 'L': check = 2 + 0 * 9; break;
           case 'M': check = 2 + 1 * 9; break;
           case 'N': check = 2 + 2 * 9; break;
           case 'O': check = 3 + 5 * 9; break;
           case 'P': check = 2 + 3 * 9; break;
           case 'Q': check = 2 + 4 * 9; break;
           case 'R': check = 2 + 5 * 9; break;
           case 'S': check = 2 + 6 * 9; break;
           case 'T': check = 2 + 7 * 9; break;
           case 'U': check = 2 + 8 * 9; break;
           case 'V': check = 2 + 9 * 9; break;
           case 'W': check = 3 + 2 * 9; break;
           case 'X': check = 3 + 0 * 9; break;
           case 'Y': check = 3 + 1 * 9; break;
           case 'Z': check = 3 + 3 * 9; break;
        }

        int j=8;
        for (int i=1; i <= 8 ; i++)
        {
            check = check + (a[i]-48)*j ;
            j--;
        }
        check = check + (a[9]-48);
        if(check % 10 ==0)
            cout << "real"  << endl;
        else
            cout << "fake" << endl;
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}

a022: 迴文 http://zerojudge.tw/ShowProblem?problemid=a022
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
    string a;
    while(getline(cin,a))
    {
        int check=0;
        for(int i =0 ; i < (a.length() /2 ); i++)
        {
            if( a[i] != a[a.length()-1-i])
            {
                check = 1;
                break;
            }
        }
        if(check ==1)
            cout << "no";
        else
            cout << "yes";
        cout << endl;
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}

a024: 最大公因數(GCD) http://zerojudge.tw/ShowProblem?problemid=a024
#include <cstdlib>
#include <iostream>

using namespace std;

 int max(long long int i,long long int j)
{
    if( i < j)
        return i;
    else
        return j;
}

int main(int argc, char *argv[])
{
    long long int a,b,c,d ;

    while(cin >> a >> b)
    {
        c=1;
        d=1;
        for (c=1 ; c <=(max(a,b));c++)
        {
            if( ( a % c ==0 )&& (b % c ==0))
            {
                d = c;
            }
        }
        cout << d;
        cout << endl;
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}

a034: 二進位制轉換http://zerojudge.tw/ShowProblem?problemid=a034
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

int main(int argc, char *argv[])
{
    int a =0;
    while(cin >> a)
    {
        int b[99];
        int c = 0;
        while(a!=0)
        {
            if(a % 2 == 1)
                b[c] = 1 ;
            else
                b[c] = 0 ;
            a=a/2;
            c++;
        }
        for (int i = c-1 ; i >= 0 ; i--)
            cout << b[i];

        cout <<  endl;
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}

a038: 數字翻轉http://zerojudge.tw/ShowProblem?problemid=a038
#include <iostream>

using namespace std;

int main()
{
    string a;
    while (getline(cin ,a))
    {
        int b[a.length()];
        for(int i=0 ; i<a.length();i++)
        {
            b[i]=a[a.length()-1-i]-48;
        }
        int c=0;
        for(int i=0 ; i<a.length();i++)
        {
            if(b[i]+c==0)
                continue;
            else
                c=1;
                cout << b[i];
        }
        cout << endl;
    }
    return 0;
}

a040: 阿姆斯壯數http://zerojudge.tw/ShowProblem?problemid=a040
#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
    int n, m;
    while( cin >> n >> m)
    {
        int k[7];
        int check=0;
        for(int i=n ; i<m; i++)
        {
            int a = 0;
            int b = i;
            while(b > 0)
            {
                k[a]= b%10 ;
                b=b/10;
                a++;
            }

            int c=0;
            for (int j = 0 ; j < a ; j++)
                c = c + pow(k[j],a);

            if (c==i)
            {
                cout<< c << " " ;
                check = 1;
            }
        }
        if(check == 0 )
            cout << "none";
        cout << endl;
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}

a044: 空間切割http://zerojudge.tw/ShowProblem?problemid=a044
#include <cstdlib>
#include <iostream>
using namespace std;

int main()
{
    int a[99999];
    a[1]=2;
    int b[99999];
    b[1]=0;
    for(int i = 2 ; i < 99999 ; i++)
        b[i]=b[i-1]+i;

    for(int i = 2 ; i <99999 ; i++)
        a[i]=a[i-1]+2+b[i-1];

    int n;
    while( cin >> n )
    {
        cout << a[n] << endl;
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}

a053: Sagit's 計分程式http://zerojudge.tw/ShowProblem?problemid=a053
#include <cstdlib>
#include <iostream>
using namespace std;

int main()
{

    int n;
    while( cin >> n )
    {
        int a=0;
        if (n<=10)
            a= 6*n;
        if (n>10 && n<=20)
            a= 60 + 2*(n-10);
        if (n>20 && n<40)
            a= 80 + 1*(n-20);
        if (n>=40)
            a =100;
        cout << a << endl;
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}

提醒

本站內容即日起將轉到另一站上轉跳~