画竜点睛を衝く@mapyo

日々やった事をつらつらと書くブログです

TOPCORDER初参戦

かなーり前に登録だけして、過去問題ちょこちょこっとだけ解いたのだが、
それ以降全く問題とかなくなったし、
本番にも参戦した事なくて、今回初参戦する事にしたので、
思い出す為に関連記事をまとめた。




topcoder入門
http://d.hatena.ne.jp/gnarl/20080828/1219893152



topcorderの解説
http://d.hatena.ne.jp/cou929_la/20091005/1254725798



Eclipseな人向け
http://d.hatena.ne.jp/gnarl/20100201/1264971398









初参戦。。。ドキドキ。
ただ、問題の英語をまず理解するのに時間がかかるのと、
javaで参戦するんだが、基本的な事がマスター出来ていないので、
1問解くのにもかなり時間がかかるだろうと自分に言い訳しておくw
グーグル先生と、グーグル翻訳にお世話になりっぱなしの予感がしたw




実際の進め方は以下の通り。

  • CODING PHASE

ガシガシプログラム解く時間。75分。

  • INTERMISSION

休憩。5分くらいだったかな。

  • CHALLENGE PHASE

人のコード見て間違いを指摘する時間。15分くらい?

  • SYSTEM TESTING PHASE

システムでテストして、もし間違いを見つけたら解いた問題が0点になる。




全部で3問出題されるのだが、1問目を解いた段階で、残りが5分くらい。。。
諦めてだらだら2問目を眺めていたww






1問目の問題はこんな感じ。

Problem Statement

     
Fox Jiro and Eel Saburo are good friends. One day Jiro received a letter from Saburo. The letter contains four integers representing an encrypted message. Please help Jiro to decrypt the message.



You are given four ints: AminusB, BminusC, AplusB, and BplusC. Your task is to find three integers A, B, and C such that:


   * AminusB = A - B
   * BminusC = B - C
   * AplusB = A + B
   * BplusC = B + C
There is always at most one triplet of integers A, B, C that satisfies all four equalities.



If it exists, return a int[] with exactly three elements: { A, B, C }. If there are no such integers, return an empty int[] instead.


Definition

     

Class: FoxAndIntegers 
Method: get 
Parameters: int, int, int, int 
Returns: int[] 
Method signature: 
int[] get(int AminusB, int BminusC, int AplusB, int BplusC) 

(be sure your method is public) 

     


Constraints

- AminusB will be between -30 and 30, inclusive. 
- BminusC will be between -30 and 30, inclusive. 
- AplusB will be between -30 and 30, inclusive. 
- BplusC will be between -30 and 30. inclusive. 
Examples

0) 
     


1


-2


3


4



Returns: {2, 1, 3 }



A - B = 2 - 1 = 1


B - C = 1 - 3 = -2


A + B = 2 + 1 = 3


B + C = 1 + 3 = 4 



1) 
     


0


0


5


5



Returns: { }



A = B = C = 2.5 satisfy all four equalities, but A, B, and C must all be integers. 



2) 
     


10


-23


-10


3



Returns: {0, -10, 13 }



A, B, and C may be negative or zero. 



3) 
     


-27


14


13


22



Returns: { }






4) 
     


30


30


30


-30



Returns: {30, 0, -30 }






This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.








これに対する俺の答えはこんな感じ。

publicclass FoxAndIntegers {



publicint[] get(int AminusB, int BminusC, int AplusB, int BplusC) {
        int[] x = newint[]{};

        int a,b,c;


        if(Math.ceil(((double)AminusB+(double)AplusB)/2)!=Math.floor(((double)AminusB+(double)AplusB))/2){

        System.out.println("a");
        return x;
        }
        if(AminusB+AplusB==0){

        a=0;
        }
        else{

        a=(AminusB + AplusB)/2;
        }

        

        b=AplusB-a;

        c= b-BminusC;



        if(a<-30 || 30<a) return x;

        if(b<-30 || 30<b) return x;

        if(c<-30 || 30<c) return x;

        if(b+c!=BplusC) return x; 

        

        x= newint[]{a,b,c};

        System.out.println(x[0] + "," + x[1] + "," + x[2]);

 return x;
}


}



ちょっと余分なソースも入ってるけどもw





解く過程で詰まったところや感じた事を覚えている範囲でメモしておく。




  • 空の配列の返し方がわからなかったw

ある条件の時には、int型の空の配列を返す必要があったのだが、

普通に返し方がわからなかったwググっていろいろ値入れてみて時間食われてしまったw


  • 0を2で割ったらいけないよな。。。と思い込んでたw

完全に勘違いだ。。。2を0で割ったらダメなんだった。。。

アホだな。。。

途中でif〜elseを使ってるが、意味ないソースになっている気がするw


  • 他の人は、例題を全てクリアするソースを記述していない。

CHALLENGE PHASEでは何も他人のソースの間違いを指摘出来なかったが、

終わった後にじっくり見てみると、問題に書いてある例題がクリア出来てないソースがいくつかあった。

これに気がついていればもう少し指摘出来たのにー!ちょっと残念。

俺はEclipseのTOPCORDER用の拡張機能使ってるから、例題のユニットテストを行うコードが自動生成されて、

すぐにテストが可能なので、ものすんごい楽。ツールって、すごい。







順位はだいたい1069位だった。。。

何人いたかは、、、どこで見れるかわからないw



でも、1位の人も3問中2問しか出来ていなかったから、多分難しかったんだろう。

そういう事にしておくw











しかし、見返してみると、この程度の問題に1時間以上かかってしまうとは、、

残念過ぎる。

まずはjavaの基本的な内容をググらずに書けるようにならないとスピードアップは出来ないなぁ。



残りの2問も頑張って問いてみたい。。。

何時間かかるんだろうかw