Trang

Thứ Sáu, 31 tháng 12, 2010

3780. How Many Islands?

http://acm.tju.edu.cn/toj/showp3780.html

import java.io.*; public class Main { /** * @author hunglee */ static int w, h, a[][]; static int di[] = {-1, -1, -1, 0, 1, 1, 1, 0}; static int dj[] = {-1, 0, 1, 1, 1, 0, -1, -1}; public static void main(String[] args) throws IOException { StreamTokenizer in = new StreamTokenizer(new BufferedReader( new InputStreamReader(System.in))); do { in.nextToken(); w = (int) in.nval; in.nextToken(); h = (int) in.nval; if (w == 0 && h == 0) break; a = new int[h + 2][w + 2]; for (int i = 1; i <= h; ++i) for (int j = 1; j <= w; ++j) { in.nextToken(); a[i][j] = (int) in.nval; } int res = 0; for (int i = 1; i <= h; ++i) for (int j = 1; j <= w; ++j) if (a[i][j] == 1) { ++res; visit(i, j); } System.out.println(res); } while (true); } private static void visit(int i, int j) { if (a[i][j] == 0) return; a[i][j] = 0; for (int k = 0; k < 8; ++k) visit(i + di[k], j + dj[k]); } }

Thứ Năm, 30 tháng 12, 2010

3237. Change Base

3237. Change Base
http://acm.tju.edu.cn/toj/showp3237.html

import java.io.*; public class Main { /** * @author hunglee */ public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader( new InputStreamReader(System.in)); int T, b; String s, st[]; T = Integer.parseInt(in.readLine()); while (T-- > 0) { st = in.readLine().split(" "); b = Integer.parseInt(st[0]); s = st[1]; long res = 0; for (int i = 0; i < s.length(); ++i) res = (res * b + s.charAt(i) - '0') % 10007; System.out.println(res); } } }

1015. Gridland

http://acm.tju.edu.cn/toj/showp1015.html

import java.io.*; public class Main { /** * @author hunglee */ public static void main(String[] args) throws IOException { StreamTokenizer in = new StreamTokenizer(new BufferedReader( new InputStreamReader(System.in))); int T; in.nextToken(); T = (int) in.nval; int m; for (int t = 1; t <= T; ++t) { in.nextToken(); m = (int) in.nval; in.nextToken(); m *= (int) in.nval; System.out.println("Scenario #" + t + ":"); if (m % 2 == 0) System.out.println(m + ".00"); else System.out.println(m + 0.41); System.out.println(); } } }

1002. Maya Calendar

http://acm.tju.edu.cn/toj/showp1002.html

import java.util.*; public class Main { /** * @author hunglee */ static String[] haabMonthName = {"pop", "no", "zip", "zotz", "tzec", "xul", "yoxkin", "mol", "chen", "yax", "zac", "ceh", "mac", "kankin", "muan", "pax", "koyab", "cumhu", "uayet"}; static String[] tzolkinDayName = {"imix", "ik", "akbal", "kan", "chicchan", "cimi", "manik", "lamat", "muluk", "ok", "chuen", "eb", "ben", "ix", "mem", "cib", "caban", "eznab", "canac", "ahau"}; public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); System.out.println(t); while (t-- > 0) { int day = (int) in.nextDouble(); int month = getMonthFromHaab(in.next()); int year = in.nextInt(); int date = year * 365 + month * 20 + day; day = date % 13 + 1; month = date % 20; year = date / 260; System.out.println(day + " " + tzolkinDayName[month] + " " + year); } } private static int getMonthFromHaab(String sval) { for (int i = 0; i < haabMonthName.length; i++) if (sval.equals(haabMonthName[i])) return i; return -1; } }