ラベル AOJ_Volume1 の投稿を表示しています。 すべての投稿を表示
ラベル AOJ_Volume1 の投稿を表示しています。 すべての投稿を表示

2012年7月15日日曜日

AOJ Volume1 0158: Collatz's Problem

  • n が偶数の時は 2 で割る。
  • n が奇数の時は 3 倍し、1 を足す。
  • 整数 n は1以上でかつ上記の計算を繰り返す途中の値が1000000以下となる程度の整数とあったのでint からlongに変更。

/******************************
* 2012/07/15                  *
* AOJ_Volume1_0158            *
* Collatz's Problem           *
* crane                       *
*******************************/
#include<iostream>
using namespace std;
int main(){
    long long int n;
    while(cin >> n, n){
        int count = 0;
        while(1){
            if(n == 1) break;
            if(n % 2 == 0) n /= 2;
            else          n = (n*3) + 1;
            count++;
        }
        cout << count << endl;
    }
    return 0;
}

2012年7月14日土曜日

AOJ Volume1 0149: Eye Test

判定関数においてA~Dそれぞれの範囲をわかりやすくするため
すべての下限上限を書いた。
A~Dの配列0~4だけどmap<char, int>なんかで扱っても良かったかも。

/****************************
* 2012/07/14                *
* AOJ_Volume1_0149          *
* Eye Test                  *
* crane                     *
*****************************/
//--------------------------------------//
//        判定    視力                    //
//    A    1.1以上                            //
//    B    0.6以上1.1未満                    //
//    C    0.2以上0.6未満                    //
//    D    0.2未満                            //
//--------------------------------------//

#include<iostream>
#include<map>
using namespace std;

int solve(double x);

int main(){
    int ldata[4] = {0, 0, 0, 0};
    int rdata[4] = {0, 0, 0, 0};
   
    double left, right;
    while(cin >> left >> right){
        int le = solve(left);
        int ri = solve(right);

        ldata[le]++;
        rdata[ri]++;
    }

    for(int i=0; i<4; i++)
        cout << ldata[i] << " " << rdata[i] << endl;
   return 0;
}

int solve(double x){
    if(1.1 <= x)                       return 0;
    else if(0.6<=x && x<1.1)  return 1;
    else if(0.2<=x && x<0.6)  return 2;
    else if(x<0.2)                     return 3;
}

2012年7月13日金曜日

AOJ Volume1 0134: Exit Survey

int sumでは足りないのでlong longで対応
/*****************************
* 2012/07/13                 *
* AOJ_Volume1_0134           *
* Exit_Survey                *
* crane                      *
******************************/
#include<iostream>
using namespace std;

int main(){
    int n, input;
    cin >> n;
    long long int sum = 0;
    for(int i=0; i<n; i++){
        cin >> input;
        sum += input;
    }
    cout << sum/n << endl;
    return 0;
}

AOJ Volume1 0127: Pocket Pager Input

/*********************************
* 2012/07/13                     *
* AOJ_Volume1_0127               *
* Pocket_Pager_Input             *
* crane                          *
**********************************/

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

char data[7][6] ={
    {},
    {' ','a','b','c','d','e'},
    {' ','f','g','h','i','j'},
    {' ','k','l','m','n','o'},
    {' ','p','q','r','s','t'},
    {' ','u','v','w','x','y'},
    {' ','z','.','?','!',' '}
};

int main(){

    string input;
    while(1){
        getline(cin, input);
        if(input.empty())break;

        bool frag = true;
        string output = "";
   
        if(input.length() % 2 != 0)
            frag = false;

        for(int i=0; i<input.length() && frag==true; i+=2){
                int in1 = (int) input.at(i) - '0';
                int in2 = (int) input.at(i+1) - '0';
                if(0<in1 && in1 < 7 && 0 < in2 && in2 < 6)
                    output += data[in1][in2];
                else
                    frag = false;
        }
        if(frag) cout << output << endl;
        else     cout << "NA" << endl;
    }
    return 0;
}

2012年7月10日火曜日

AOJ Volume1 0139: Snakes

A種は,">'"の後に"="が1個以上並んだ後、"#"が来て、さらに前と同じ個数の"="が来た後、"~"(半角チルダ)で終わる。
B種は,">^"の後に "Q="が1個以上並んだ後、"~~"で終わる。
A種の例: >'====#====~        >'==#==~
B種の例: >^Q=Q=Q=Q=~~        >^Q=Q=~~
 
 /************************
* 2012/07/10            *
* AOJ_Volume1_0139      *
* Snakes                *
* crane                 *
*************************/

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


int main(){

    int n;
    cin >> n;

    while(n--){
        string input;
        cin >> input;
        if(input.length() <5){
            cout << "NA" << endl;
        }else{
            string s = input.substr(0,2);        //どの蛇か
            char e = input.at(input.length()-1); //A B尾判定
            char e2 = input.at(input.length()-2);//B 尾判定

            if(input.length() % 2 != 0){//lengthが偶数出ないものはAでもBでもない
                cout << "NA" << endl;
            }else if(s == ">'" && e == '~'){            //SnakeA
                bool frag_A = true;
                for(int i=2; i<input.length()/2; i++){  
                    char c1 = input.at(i);               //前から
                    char c2 = input.at(input.length()-i);//後ろから
                    if(c1 != '=' || c2 != '='){
                        cout << "NA" << endl;
                        frag_A = false;
                        break;
                     }
                }
                char a = input.at(input.length()/2 );
                if(a == '#' && frag_A) cout << "A" << endl;

            }else if(s == ">^" && e == '~' && e2 == '~'){  //SnakeB
                bool frag_B = true;
                for(int i=2; i<input.length()-2; i+=2){
                    string s1 = input.substr(i, 2);

                    if(s1 != "Q="){
                        cout << "NA" << endl;
                        frag_B = false;
                        break;
                    }
            }
                if(frag_B)cout << "B" << endl;
            }else cout << "NA"<< endl;
        }
    }
    return 0;
}

2012年7月5日木曜日

AOJ Volume1 0117: A reward for a Carpenter

街の数が20以下なのでワーシャル-フロイド法で

/************************************
* 2012/07/05                        *
* AOJ Volume1 0117                  *
* A reward for a Carpenter          *
* crane                             *
*************************************/

#include <iostream>
#include <cstdio>
using namespace std;
#define MAX_N 1000
#define MAX_M 21
int field[MAX_M][MAX_M];
int n,m;
void warshall_floyd();

int main(){
  
    int a,b,c,d;
    cin >> n >> m;
  
    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
            field[i][j]=MAX_N;
    for(int i=0; i<m; i++){
        scanf("%d,%d,%d,%d",&a,&b,&c,&d);
        field[a][b] = c;
        field[b][a] = d;
    }
    warshall_floyd();
    int x1,x2,y1,y2;
    scanf("%d,%d,%d,%d",&x1,&x2,&y1,&y2);
    cout << y1-field[x1][x2]-field[x2][x1]-y2 << endl;
  
}

void warshall_floyd(){
        for(int k=1; k<=n; k++)
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
                field[i][j]=min(field[i][j],field[i][k]+field[k][j]);

}

2012年7月4日水曜日

AOJ Volume1 0112: A Milk Shop

昇順にソートした状態が最も待ち時間が少なくなる。
 「お客さんは 10,000 人以下で 1 人あたりに要する時間は 60 分以下とします。」
とあるのでlong long int で対応。

 /******************************
* 2012/07/04                  *
* AOJ_Volume1_0112            *
* A_Milk_Shop                 *
* crane                       *
*******************************/
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;

#define MAX_N 100001

int a[MAX_N];

int main(){
    int n;
    while(cin >> n,n){
        for(int i=0; i<n; i++)
            cin >> a[i];

        sort(a, a+n);//昇順にソート   

        long long int sum=0, tmp=0;
        for(int i=0; i<n; i++){
                sum += tmp;
                tmp += a[i];
        }
        cout << sum << endl;
    }
    return 0;
}

AOJ Volume1 0104:Magical Tiles

/*********************
* 2012/07/04         *
* AOJ_Volume1_0104   *
* Magical_Tiles      *
* crane              *
**********************/

#include<iostream>
using namespace std;
#define MAX_N 101

char field[MAX_N][MAX_N];
bool f[MAX_N][MAX_N];
int main(){
  
    int h,w; //縦,横
    while(cin >> h >> w, (h||w)){
        //field make
        for(int i=0; i<h; i++)
            for(int j=0; j<w; j++){
                cin >> field[i][j];
                f[i][j] = false;
            }

        int x = 0, y = 0;
        while(1){
          
            if(field[y][x] == '.' || f[y][x] == true) break;
          
            f[y][x] = true;
            switch(field[y][x]){
            case '>':
                x++;
                break;
            case '<':
                x--;
                break;
            case '^':
                y--;
                break;
            case 'v':
                y++;
                break;
            }

        }
        if(f[y][x]) cout << "LOOP" << endl;
        else        cout << x << " " << y << endl;
    }
    return 0;
}

AOJ Volume1 0101:Aizu PR

サンプルを実行すると、データセット数が入力された後に入力が勝手に一つ入る現象が発生。対策としてcin.get()を入れた。


/****************************************************************************************
*    2012/04/29
*   AIZU ONLINE JUDGE volume1_0101                                                          *
*    AIZU PR                                                                                *
*    HoshinoをHoshinaに置き換えて出力する                                                *
*    英文は1000文字以下                                                                    *
*                                                                                        *
*    Input                                                                                *
*    最初にデータセット数nが与えられる。続いてn行の英文が与えられる。                    *
*    各データセットは1行に英文が与えられる。英文は半角英数字・記号を含む。                *
*    Output                                                                                *
*    各データセットについて”Hoshino”を”Hoshina”に変換した英文(半角)を1行に出力する。*
*****************************************************************************************/


/***********************************************
****************Time limit:1sec*****************
****************Memory limit:32768KB************
************************************************/

#include<iostream>
#include<string>

int main(){
    //入力1    データセット数nを受け取る
    int n;    //データセット数受け取り用変数
    std::cin >> n;  std::cin.get();
    for(int i=0; i<n; i++){   
        std::string input;    //データセット
        std::getline(std::cin, input);    //データ入力
        int input_size = input.length();//データ長
        for(int i=0; i<input_size; i++){
            if(input.substr(i,7) == "Hoshino"){
                input.replace(i,7,"Hoshina");
            }
        }
        std::cout << input << std::endl;
    }
    return 0;
}

AOJ Volume1 0100 Sale Result

大きな値を扱うためlong long intで対応。

/**************************
*  2012/07/04             *
*  AOJ Volume1 0100       *
*  Sale Result            *
*  crane                  *
***************************/

#include<iostream>
#include<vector>
#include<map>
using namespace std;

vector<int> num;
map<int, long long int> data;

int main(){
    int n;
    while(cin >> n,n){
        num.clear();
        data.clear();
        for(int i=0; i<n; i++){
            long long int dn, dp, dc;
            cin >> dn >> dp >> dc;
            if(!data[dn]) num.push_back(dn);
            data[dn] += dp * dc;
        }
        bool frag = true;
        for(int i=0; i<num.size(); i++){
            if(1000000 <= data[num[i]] ){
                frag = false;
                cout <<num[i] << endl;
            }
        }
        if(frag) cout << "NA" << endl;
    }
    return 0;
}