跳到主要內容

UVA10409

Problem G: Die Game

Life is not easy. Sometimes it is beyond your control. Now, as contestants of ACM ICPC, you might be just tasting the bitter of life. But don't worry! Do not look only on the dark side of life, but look also on the bright side. Life may be an enjoyable game of chance, like throwing dice. Do or die! Then, at last, you might be able to find the route to victory.
This problem comes from a game using a die. By the way, do you know a die? It has nothing to do with "death." A die is a cubic object with six faces, each of which represents a different number from one to six and is marked with the corresponding number of spots. Since it is usually used in pair, "a die" is a rarely used word. You might have heard a famous phrase "the die is cast," though.
When a game starts, a die stands still on a flat table. During the game, the die is tumbled in all directions by the dealer. You will win the game if you can predict the number seen on the top face at the time when the die stops tumbling.
Now you are requested to write a program that simulates the rolling of a die. For simplicity, we assume that the die neither slips nor jumps but just rolls on the table in four directions, that is, north, east, south, and west. At the beginning of every game, the dealer puts the die at the center of the table and adjusts its direction so that the numbers one, two, and three are seen on the top, north, and west faces, respectively. For the other three faces, we do not explicitly specify anything but tell you the golden rule: the sum of the numbers on any pair of opposite faces is always seven.
Your program should accept a sequence of commands, each of which is either "north", "east", "south", or "west". A "north" command tumbles the die down to north, that is, the top face becomes the new north, the north becomes the new bottom, and so on. More precisely, the die is rotated around its north bottom edge to the north direction and the rotation angle is 90 degrees. Other commands also tumble the die accordingly to their own directions. Your program should calculate the number finally shown on the top after performing the commands in the sequence. Note that the table is sufficiently large and the die never falls off during the game.

        

Input

The input consists of one or more command sequences, each of which corresponds to a single game. The first line of a command sequence contains a positive integer, representing the number of the following command lines in the sequence. You may assume that this number is less than or equal to 1024. A line containing a zero indicates the end of the input. Each command line includes a command that is one of northeastsouth, and west. You may assume that no white space occurs in any lines.

Output

For each command sequence, output one line containing solely the number on the top face at the time when the game is finished.

Sample Input

1
north
3
north
east
south
0

Output for the Sample Input

5
1








大意:在一張很大的桌子上,骰一顆骰子,骰子的頂端是1點,前方是2點,左方是3點,給幾個轉動的方向,求最後在頂端的點數是多少(每一面與他的對面總合一定是7 EX:點數1的對面一定是6)

解法: 模擬題



import java.util.Scanner;

public class UVA10409 {

 public static void main(String[] args) {

  Scanner sc = new Scanner(System.in);

  while (sc.hasNext()) {

   int count = Integer.parseInt(sc.nextLine());

   if (count == 0)
    break;

   int top = 1, north = 2, west = 3;

   int temp = 0;

   for (int i = 0; i < count; i++) {

    String s = sc.nextLine();

    switch (s) {

    // temp = top(replace all repeat)
    case ("north"): {

     temp = top;
     top = 7 - north;
     north = temp;
     break;

    }
    case ("east"): {

     temp = top;
     top = west;
     west = 7 - temp;
     break;

    }
    case ("south"): {

     temp = top;
     top = north;
     north = 7 - temp;
     break;

    }
    case ("west"): {

     temp = top;
     top = 7 - west;
     west = temp;
     break;

    }

    }

   }
   System.out.println(top);

  }

  sc.close();
 }

}



留言

這個網誌中的熱門文章

UVA11349

J - Symmetric Matrix Time Limit: 1 sec Memory Limit: 16MB You`re given a square matrix M. Elements of this matrix are M ij : {0 < i < n, 0 < j < n}. In this problem you'll have to find out whether the given matrix is symmetric or not. Definition: Symmetric matrix is such a matrix that all elements of it are non-negative and symmetric with relation to the center of this matrix. Any other matrix is considered to be non-symmetric. For example: All you have to do is to find whether the matrix is symmetric or not. Elements of a matrix given in the input are -2 32  <= M ij  <= 2 32  and 0 < n <= 100. INPUT: First line of input contains number of test cases T <= 300. Then T test cases follow each described in the following way. The first line of each test case contains n - the dimension of square matrix. Then n lines follow each of then containing row i. Row contains exactly n elements separated by a space character. j-th number in row i is the elem

UVA11461

A square number is an integer number whose square root is also an integer. For example 1, 4, 81 are some square numbers. Given two numbers a and b you will have to find out how many square numbers are there between a and b (inclusive). Input The input file contains at most 201 lines of inputs. Each line contains two integers a and b (0 < a ≤ b ≤ 100000). Input is terminated by a line containing two zeroes. This line should not be processed. Output For each line of input produce one line of output. This line contains an integer which denotes how many square numbers are there between a and b (inclusive). Sample Input 1 4 1 10 0 0 Sample Output 2 3 大意:給兩個數字 求範圍內平方不大於第二個數字的數量 解法: 以最大數取根號後往回看 import java.util.Scanner; public class UVA11461 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int start = sc.nextInt(); int last = sc.nextInt(); if (last == 0) break; System.o

UVA11005

Problem B Cheapest Base Input:  Standard Input Output:  Standard Output When printing text on paper we need ink. But not every character needs the same amount of ink to print: letters such as 'W', 'M' and '8' are more expensive than thinner letters as ' i ', 'c' and '1'. In this problem we will evaluate the cost of printing numbers in several bases. As you know, numbers can be expressed in several different bases. Well known bases are binary (base 2; digits 0 and 1), decimal (base 10; digits 0 to 9) and hexadecimal (base 16; digits 0 to 9 and letters A to F). For the general base  n  we will use the first  n  characters of the string "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", which means the highest base in this problem is 36. The lowest base is of course 2. Every character from this string has an associated cost, represented by an integer value between 1 and 128. The cost to print a number in a certain base is the s