AOJ_0217 Walking on the Hospital

問題


解法

一番大きいのを選ぶだけ。
C++Haskell で書いてみた
Haskellはまだ勉強中なので、もっといいやり方があったら誰か教えてください。

実装

c++

#include <cstdio>
#include <algorithm>
using namespace std;

int main(){
	int n, ans, d;
	while(scanf("%d", &n) && n){
		int p, d1, d2;
		ans = 0; d = 0;
		for(int i = 0; i < n; i++){
			scanf("%d %d %d", &p, &d1, &d2);
			if(d < d1 + d2){ ans = p; d = d1 + d2; }
		}
		printf("%d %d\n", ans, d);
	}
	return 0;
}

Haskell

main = do cs <- getContents
          putStr $ solve $ map (\cs -> (read cs :: Int)) $ words cs

solve :: [Int] -> String
solve (0:_) = ""
solve (n:ns) = sol' ns n 0 0

sol' :: [Int] -> Int -> Int -> Int -> String
sol' ns 0 maxp maxd = (show maxp) ++ " " ++ (show maxd) ++ "\n" ++ solve ns
sol' (p:d1:d2:ns) n maxp maxd =
	if maxd < d1 + d2
		then sol' ns (n - 1) p (d1 + d2)
		else sol' ns (n - 1) maxp maxd