發表文章

目前顯示的是有「知識學習-程式語言-線上學習-Uva」標籤的文章

121 - Pipe Fitters

圖片
UVa網站題目連結 My Solved Problems Performance Background Filters, or programs that pass ``processed'' data through in some changed form, are an important class of programs in the UNIX operating system. A pipe is an operating system concept that permits data to ``flow'' between processes (and allows filters to be chained together easily.) This problem involves maximizing the number of pipes that can be fit into a storage container (but it's a pipe fitting problem, not a bin packing problem). The Problem A company manufactures pipes of uniform diameter. All pipes are stored in rectangular storage containers, but the containers come in several different sizes. Pipes are stored in rows within a container so that there is no space between pipes in any row (there may be some space at the end of a row), i.e., all pipes in a row are tangent, or touch. Within a rectangular cross-section, pipes are stored in either a grid pattern or a skew pattern as shown below: the t...

10929 - You can say 11

UVa網站題目連結 My Solved Problems Performance Introduction to the problem Your job is, given a positive number N, determine if it is a multiple of eleven. Description of the input The input is a file such that each line contains a positive number. A line containing the number 0 is the end of the input. The given numbers can contain up to 1000 digits. Description of the output The output of the program shall indicate, for each input number, if it is a multiple of eleven or not. Sample input: 112233 30800 2937 323455693 5038297 112234 0 Sample output 112233 is a multiple of 11. 30800 is a multiple of 11. 2937 is a multiple of 11. 323455693 is a multiple of 11. 5038297 is a multiple of 11. 112234 is not a multiple of 11. Solution import java . io . BufferedInputStream ; import java . math . BigInteger ; import java . util . Scanner ; public class Main { public static void main ( String [] args ) { Scanner in = new Scanner ( new BufferedInputStream ( System . in )); ...

10071 - Back to High School Physics

UVa網站題目連結 My Solved Problems Performance A particle has initial velocity and constant acceleration. If its velocity after certain time is v then what will its displacement be in twice of that time? Input The input will contain two integers in each line. Each line makes one set of input. These two integers denote the value of v (-100 Output For each line of input print a single integer in one line denoting the displacement in double of that time. Sample Input 0 0 5 12 Sample Output 0 120 Solution import java . io . BufferedInputStream ; import java . util . Scanner ; public class Main { public static void main ( String args []) { Scanner in = new Scanner ( new BufferedInputStream ( System . in )); while ( in . hasNext ()) { System . out . println ( 2 * in . nextInt ()* in . nextInt ()); } } } 關鍵字:UVa Online Judge, ACM. Java

10055 - Hashmat the Brave Warrior

UVa網站題目連結 My Solved Problems Performance Hashmat is a brave warrior who with his group of young soldiers moves from one place to another to fight against his opponents. Before fighting he just calculates one thing, the difference between his soldier number and the opponent's soldier number. From this difference he decides whether to fight or not. Hashmat's soldier number is never greater than his opponent. Input The input contains two integer numbers in every line. These two numbers in each line denotes the number of soldiers in Hashmat's army and his opponent's army or vice versa. The input numbers are not greater than 2^32. Input is terminated by End of File. Output For each line of input, print the difference of number of soldiers between Hashmat's army and his opponent's army. Each output should be in seperate line. Sample Input: 10 12 10 14 100 200 Sample Output: 2 4 100 Solution import java . io . BufferedInputStream ; import java . ...

264 - Count on Cantor

圖片
UVa網站題目連結 My Solved Problems Performance One of the famous proofs of modern mathematics is Georg Cantor's demonstration that the set of rational numbers is enumerable. The proof works by using an explicit enumeration of rational numbers as shown in the diagram below. In the above diagram, the first term is 1/1, the second term is 1/2, the third term is 2/1, the fourth term is 3/1, the fifth term is 2/2, and so on. Input and Output You are to write a program that will read a list of numbers in the range from 1 to and will print for each number the corresponding term in Cantor's enumeration as given below. No blank line should appear after the last number. The input list contains a single number per line and will be terminated by end-of-file. Sample input 3 14 7 Sample output TERM 3 IS 2/1 TERM 14 IS 2/4 TERM 7 IS 1/4 Solution import java . io . BufferedInputStream ; import java . util . Scanner ; public class Main { public static void main ( String ...

113 - Power of Cryptography

圖片
UVa網站題目連結 My Solved Problems Performance Background Current work in cryptography involves (among other things) large prime numbers and computing powers of numbers modulo functions of these primes. Work in this area has resulted in the practical use of results from number theory and other branches of mathematics once considered to be of only theoretical interest. This problem involves the efficient computation of integer roots of numbers. The Problem Given an integer and an integer you are to write a program that determines , the positive root of p . In this problem, given such integers n and p , p will always be of the form for an integer k (this integer is what your program must find). The Input The input consists of a sequence of integer pairs n and p with each integer on a line by itself. For all such pairs , and there exists an integer k , such that . The Output For each integer pair n and p the value should be printed, i.e., the num...

623 - 500!

UVa網站題目連結 My Solved Problems Performance In these days you can more and more often happen to see programs which perform some useful calculations being executed rather then trivial screen savers. Some of them check the system message queue and in case of finding it empty (for examples somebody is editing a file and stays idle for some time) execute its own algorithm. As an examples we can give programs which calculate primary numbers. One can also imagine a program which calculates a factorial of given numbers. In this case it is the time complexity of order O ( n ) which makes troubles, but the memory requirements. Considering the fact that 500! gives 1135-digit number no standard, neither integer nor floating, data type is applicable here. Your task is to write a programs which calculates a factorial of a given number. Assumptions: Value of a number `` n " which factorial should be calculated of does not exceed 1000 (although 500! is the name of the problem, 500! is a ...

326 - Extrapolation Using a Difference Table

圖片
UVa網站題目連結 My Solved Problems Performance A very old technique for extrapolating a sequence of values is based on the use of a difference table. The difference table used in the extrapolation of a sequence of 4 values, say 3, 6, 10, and 15, might be displayed as follows: The original sequence of values appears in the first column of the table. Each entry in the second column of the table is formed by computing the difference between the adjacent entries in the first column. These values (in the second column) are called first differences. Each entry in the third column is similarly the difference between the adjacent entries in the second column; the third column entries are naturally called second differences. Computation of the last column in this example should now be obvious (but beware that this value is not always zero). Note that the last column will always contain only a single value. If we begin with a sequence of n values, the completed diffe...

324 - Factorial Frequencies

圖片
UVa網站題目連結 My Solved Problems Performance In an attempt to bolster her sagging palm-reading business, Madam Phoenix has decided to offer several numerological treats to her customers. She has been able to convince them that the frequency of occurrence of the digits in the decimal representation of factorials bear witness to their futures. Unlike palm-reading, however, she can't just conjure up these frequencies, so she has employed you to determine these values. Recall that the definition of n ! (that is, n factorial) is just . As she expects to use either the day of the week, the day of the month, or the day of the year as the value of n , you must be able to determine the number of occurrences of each decimal digit in numbers as large as 366 factorial (366!), which has 781 digits. Input and Output The input data for the program is simply a list of integers for which the digit counts are desired. All of these input value...

305 - Joseph

UVa網站題目連結 My Solved Problems Performance The Joseph's problem is notoriously known. For those who are not familiar with the original problem: from among n people, numbered 1, 2, ..., n , standing in circle every m th is going to be executed and only the life of the last remaining person will be saved. Joseph was smart enough to choose the position of the last remaining person, thus saving his life to give us the message about the incident. For example when n = 6 and m = 5 then the people will be executed in the order 5, 4, 6, 2, 3 and 1 will be saved. Suppose that there are k good guys and k bad guys. In the circle the first k are good guys and the last k bad guys. You have to determine such minimal m that all the bad guys will be executed before the first good guy. Input The input file consists of separate lines containing k . The last line in the input file contains 0. You can suppose that 0 < k < 14. Output The output file will consist of separate lines c...