Showing posts with label AOJ. Show all posts
Showing posts with label AOJ. Show all posts

Monday, February 7, 2011

AOJ 0221 FizzBuzz

Problem F: FizzBuzz

「Fizz Buzz」と言われる数字を使ったゲームがあります。このゲームは複数のプレイヤーで数字を 1 から順にひとつずつ数え上げていくもので、各プレイヤーは直前のプレイヤーが発言した次の数字 をひとつだけ発言します。その時、3 で割り切れる場合は 「Fizz」, 5 で割り切れる場合は 「Buzz」、 両者で割り切れる場合は「FizzBuzz」と数の代わりに発言しなければなりません。

このゲームでは、最初の 16 までの発言は以下のようになります。

1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz, 16, ・・・

太郎君は友達とこの「Fizz Buzz」をして遊ぶことにしました。太郎君たちはルールを次のようにし ました。 「間違えた人は脱落する。その次の人は間違えた数の次の数から始める。つまり、1, 2, 3 と 発言した場合、3 で間違えたので次は 4 から始めることになる。」このルールに従ってゲームを行うの ですが、このゲームに慣れていないため、間違えたことに気付かないことがあり、公平な判断ができ ません。そこであなたは太郎君たちがこのゲームを楽しめるように、決められた発言回数が終わった 時点で残っていた人を出力するプログラムを作成することになりました。

プレイヤー数 m、ゲーム中に発言された回数 n とそれぞれの発言 s を入力とし、 入力が終わった時点で残っているプレイヤーの番号を小さい順に出力するプログラムを作成してくだ さい。ただし、プレイヤーには 1 から番号が割り振られており、発言順番も 1 番目のプレイヤーから 順に行い、一通り発言が終わると、再度 1 番目のプレイヤーから発言することとします。順番の回っ てきたプレイヤーが既に脱落している場合は、その次のプレイヤーが発言します。また、このプログラムは、プレイヤーが一人になった時点で、その後の発言を無視しなければなりません。

さらに、 m は 2 以上 1000 以下の整数、 n は 1 以上 10000 以下の 整数とし、s は数字、Fizz、Buzz、FizzBuzz、あるいは間違った発言を示すその他の文字列です。

Input

複数のデータセットの並びが入力として与えられます。入力の終わりはゼロふたつの行で示されます。 各データセットは以下のとおりです。

1 行目 プレイヤー数 m 発言回数 n (整数 整数;半角空白区切り)
2 行目 第 1 の発言 s1 (半角英数字)
3 行目 第 2 の発言 s2 (半角英数字)
:
n+1 行目 第 n の発言 sn (半角英数字)
Output

入力データセットごとに、指定された発言回数まで入力されたときに残っているプレイヤーの番号を 小さい順に出力します。

Sample Input

5 7
1
2
Fizz
4
Buzz
6
7
3 5
1
2
3
4
5
0 0
Output for the Sample Input

2 3 4 5
1

AOJ 0109 Smart Calculator

Smart Calculator

Your task is to write a program which reads an expression and evaluates it.

The expression consists of numerical values, operators and parentheses, and the ends with '='.
The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division.
Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right.
You may assume that there is no division by zero.
All calculation is performed as integers, and after the decimal point should be truncated
Length of the expression will not exceed 100.
Input

The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression.

Output

For each datasets, prints the result of calculation.

Sample Input

2
4-2*3=
4*(8+4+3)=
Output for the Sample Input

-2
60

AOJ 0030 Sum of Integers

Sum of Integers

Write a program which reads n digits chosen from 0 to 9 and prints the number of combinations where the sum of the digits equals s. You can not use the same digits in a combination. For example, the combinations where n = 3 and s = 6 are as follows:

1 + 2 + 3 = 6
0 + 1 + 5 = 6
0 + 2 + 4 = 6
Input

The input consists of several datasets. Each dataset includes n and s separated by a space.

The input ends with a case where n = 0 and s = 0.

Output

For each dataset, print the number of combination in a line.

Sample Input

3 6
3 1
0 0
Output for the Sample Input

3
0