跳到主要內容

發表文章

目前顯示的是 2月, 2015的文章

UVA136

  Ugly Numbers   Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ... shows the first 11 ugly numbers. By convention, 1 is included. Write a program to find and print the 1500'th ugly number. Input and Output There is no input to this program. Output should consist of a single line as shown below, with <number> replaced by the number computed. Sample output The 1500'th ugly number is <number>. ----------------------------------------------------------------------------------------------------- import java.util.HashSet; import java.util.PriorityQueue; import java.util.Set; public class Main{ public static void main(String[] args) { PriorityQueue<Long> pq = new PriorityQueue<Long>(); int[] store = new int[] { 2, 3, 5 }; Set st = new HashSet(); pq.offer((long) 1); st.add(1); for (int i = 1;; i++) { long num = pq.peek();

UVA101

The Blocks Problem   Background   Many areas of Computer Science use simple, abstract domains for both analytical and empirical studies. For example, an early AI study of planning and robotics (STRIPS) used a block world in which a robot arm performed tasks involving the manipulation of blocks. In this problem you will model a simple block world under certain rules and constraints. Rather than determine how to achieve a specified state, you will ``program'' a robotic arm to respond to a limited set of commands. The Problem   The problem is to parse a series of commands that instruct a robot arm in how to manipulate blocks that lie on a flat table. Initially there are  n  blocks on the table (numbered from 0 to  n -1) with block  b i  adjacent to block  b i +1  for all   as shown in the diagram below:   Figure:  Initial Blocks World The valid commands for the robot arm that manipulates blocks are: move  a  onto  b where  a  and  b  are block numbers, puts

UVA10815

Problem B: Andy's First Dictionary Time limit: 3 seconds Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy task for him, as the number of words that he knows is, well, not quite enough. Instead of thinking up all the words himself, he has a briliant idea. From his bookshelf he would pick one of his favourite story books, from which he would copy out all the distinct words. By arranging the words in alphabetical order, he is done! Of course, it is a really time-consuming job, and this is where a computer program is helpful. You are asked to write a program that lists all the different words in the input text. In this problem, a word is defined as a consecutive sequence of alphabets, in upper and/or lower case. Words with only one letter are also to be considered. Furthermore, your program must be CaSe InSeNsItIvE. For example, words like "Apple", "apple" or "APPLE" must be considered the same. Input The

UVA133

 The Dole Queue   In a serious attempt to downsize (reduce) the dole queue, The New National Green Labour Rhinoceros Party has decided on the following strategy. Every day all dole applicants will be placed in a large circle, facing inwards. Someone is arbitrarily chosen as number 1, and the rest are numbered counter-clockwise up to N (who will be standing on 1's left). Starting from 1 and moving counter-clockwise, one labour official counts off k applicants, while another official starts from N and moves clockwise, counting m applicants. The two who are chosen are then sent off for retraining; if both officials pick the same person she (he) is sent off to become a politician. Each official then starts counting again at the next available person and the process continues until no-one is left. Note that the two victims (sorry, trainees) leave the ring simultaneously, so it is possible for one official to count a person already selected by the other official. Input Write

UVA489

Hangman Judge   In ``Hangman Judge,'' you are to write a program that judges a series of Hangman games. For each game, the answer to the puzzle is given as well as the guesses. Rules are the same as the classic game of hangman, and are given as follows: The contestant tries to solve to puzzle by guessing one letter at a time. Every time a guess is correct, all the characters in the word that match the guess will be ``turned over.'' For example, if your guess is ``o'' and the word is ``book'', then both ``o''s in the solution will be counted as ``solved.'' Every time a wrong guess is made, a stroke will be added to the drawing of a hangman, which needs 7 strokes to complete. Each unique wrong guess only counts against the contestant once. ______ | | | O | /|\ | | | / \ __|_ | |______ |_________| If the drawing of the hangman is completed before the contestant has succ

UVA1586

An  organic compound  is any member of a large class of chemical compounds whose molecules contain carbon. The  molar mass  of an organic compound is the mass of one mole of the organic compound. The molar mass of an organic compound can be computed from the standard atomic weights of the elements. When an organic compound is given as a  molecular formula , Dr. CHON wants to find its molar mass. A molecular formula, such as  C 3   H 4   O 3   , identifies each constituent element by its chemical symbol and indicates the number of atoms of each element found in each discrete molecule of that compound. If a molecule contains more than one atom of a particular element, this quantity is indicated using a subscript after the chemical symbol. In this problem, we assume that the molecular formula is represented by only four elements, ` C ' (Carbon), ` H ' (Hydrogen), ` O ' (Oxygen), and ` N ' (Nitrogen) without parentheses. The following table shows that the standard ato

UVA1585

There is an objective test result such as `` OOXXOXXOOO ". An ` O ' means a correct answer of a problem and an ` X ' means a wrong answer. The score of each problem of this test is calculated by itself and its just previous consecutive ` O 's only when the answer is correct. For example, the score of the 10th problem is 3 that is obtained by itself and its two previous consecutive ` O 's. Therefore, the score of `` OOXXOXXOOO " is 10 which is calculated by ``1+2+0+0+1+0+0+1+2+3". You are to write a program calculating the scores of test results. Input   Your program is to read from standard input. The input consists of  T   test cases. The number of test cases  T   is given in the first line of the input. Each test case starts with a line containing a string composed by ` O ' and ` X ' and the length of the string is more than 0 and less than 80. There is no spaces between ` O ' and ` X '. Output   Your program is to write to sta

UVA340

Master-Mind Hints   MasterMind is a game for two players. One of them,  Designer , selects a secret code. The other,  Breaker , tries to break it. A code is no more than a row of colored dots. At the beginning of a game, the players agree upon the length  N  that a code must have and upon the colors that may occur in a code. In order to break the code, Breaker makes a number of guesses, each guess itself being a code. After each guess Designer gives a hint, stating to what extent the guess matches his secret code. In this problem you will be given a secret code   and a guess   , and are to determine the hint. A hint consists of a pair of numbers determined as follows. A  match  is a pair ( i , j ),   and   , such that   . Match ( i , j ) is called  strong  when  i  =  j , and is called  weak  otherwise. Two matches ( i , j ) and ( p , q ) are called  independent  when  i  =  p  if and only if  j  =  q . A set of matches is called  independent  when all of its members are p

開燈問題(節約用電 隨手關燈)

題目描述 : 有n盞燈,編號為1~n,第1個人把所有燈打開,第2個人按下所有編號為2的倍數的開關(這些燈將被關掉),第3個人按下所有編號為3的倍數的開關(其中關掉的燈被打開, 開著燈將被關閉),依此類推。一共有k個人,問最後有哪些燈開著?( k <=n <=1000) input 7  3 output 1  5  6  7 -------------------------------------------------------------------------------------------------------------------------- #include<stdio.h> #include<string.h> #define maxn 1010 int a[maxn]; int main() { int n, k, first = 1; memset(a, 0, sizeof(a)); scanf_s("%d%d", &n, &k); for (int i = 1; i <= k; i++) for (int j = 1; j <= n; j++) if (j%i == 0)a[j] = !a[j];         for (int i = 1; i <= n; i++) if (a[i]){ if (first)first = 0; else printf(" "); printf("%d", i); }         printf("\n"); return 0; }

韓信點兵(今有物不知其數,三三數之剩二,五五數之剩三,七七數之剩二,問物幾何?──《孫子算經》)

描述:         相傳韓信才智過人,從不直接清點自己軍隊的人數,只要讓士兵先後以三人一排、五人一排、七人一排地變換隊形,而他每次只掠一眼隊伍的排尾就知道總人數了。輸入3個非負整數a,b,c ,表示每種隊形排尾的人數(a<3,b<5,c<7),輸出總人數的最小值(或報告無解)。已知總人數不小於10,不超過100 。 輸入             2  1  6         2  1  3 輸出         Case  1:  41         Case  2:  No  answer -------------------------------------------------------------------------------------------------------------------------- #include<stdio.h> #include<math.h> int main() { int a = 0, b = 0, c = 0; int Case = 0; while (scanf("%d %d %d", &a, &b, &c) == 3 && a != 0 && b != 0 && c != 0) { int x1 = 5*7, x2 = 3*7, x3 = 3*5, N = 0; for (; x1 % 3 != 1; x1 += x1); for (; x2 % 5 != 1; x2 += x2); for (; x3 % 7 != 1; x3 += x3); N = (x1 * a + x2 * b + x3 * c) % 105; printf("Case %d: ", ++Case); 10 <= N && N <= 100 ? printf("%d\n", N) : printf("No answer\n"); } return 0