上海交通大学计算机考研复试 2007年(java)

2007年问题1
题目描述

    Among grandfather's papers a bill was found.     72 turkeys $_679_     The first and the last digits of the number that obviously represented the total price of those turkeys are replaced here by blanks (denoted _), for they are faded and are illegible. What are the two faded digits and what was the price of one turkey?     We want to write a program that solves a general version of the above problem.     N turkeys $_XYZ_     The total number of turkeys, N, is between 1 and 99, including both. The total price originally consisted of five digits, but we can see only the three digits in the middle. We assume that the first digit is nonzero, that the price of one turkeys is an integer number of dollars, and that all the turkeys cost the same price.     Given N, X, Y, and Z, write a program that guesses the two faded digits and the original price. In case that there is more than one candidate for the original price, the output should be the most expensive one. That is, the program is to report the two faded digits and the maximum price per turkey for the turkeys.

输入描述:

    The first line of the input file contains an integer N (0<N<100), which represents the number of turkeys. In the following line, there are the three decimal digits X, Y, and Z., separated by a space, of the original price $_XYZ_.

输出描述:

    For each case, output the two faded digits and the maximum price per turkey for the turkeys.

示例1
输入

72
6 7 9
5
2 3 7
78
0 0 5

输出

3 2 511
9 5 18475
0
import java.util.*;
import java.io.*;
import java.text.* ;
public class Main
{
    public static void main(String[] args){
    	try {
	        BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
	        String str;
	        while((str = br.readLine()) != null) {
	        	int n = Integer.parseInt(str);
	        	String[] prices = br.readLine().split(" ");
	        	int max = 0;
	        	int start = 0;
	        	int end = 0;
	        	for(int i = 1; i <= 9; i++) {
	        		for(int j = 0; j <= 9; j++) {
	        			int originalPrice = Integer.parseInt(i+prices[0]+prices[1]+prices[2]+j);
	        			if(originalPrice%n == 0) {
	        				start = i;
	        				end = j;
	        				max = originalPrice/n;
	        			}
	        		}
	        	}
	        	if(max == 0) System.out.println(max);
	        	else {
	        		System.out.println(start+" "+end+" "+max);
	        	}
	        }
 	    } catch (IOException e) {
	        e.printStackTrace();
	    }
    }
}

问题二:
题目描述

    John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, was a Hungarian-American mathematician who made important contributions to the foundations of mathematics, logic, quantum physics, meteorology, science, computers, and game theory. He was noted for a phenomenal memory and the speed with which he absorbed ideas and solved problems. In 1925 he received a B.S. diploma in chemical engineering from Zurich Institute and in 1926 a Ph.D. in mathematics from the University of Budapest, His Ph.D. dissertation on set theory was an important contributions to the subject.     At the age of 20, von Neumann proposed a new definition of ordinal numbers that was universally adopted. While still in his twenties, he made many contributions in both pure and applied mathematics that established him as a mathematician of unusual depth. His Mathematical Foundation of Quantum Mechanics (1932) built a solid framework for the new scientific discipline.     During this time he also proved the mini-max theorem of GAME THEORY. He gradually expanded his work in game theory, and with coauthor Oskar Morgenstern he wrote Theory of Games and Economic Behavior (1944).     There are some numbers which can be expressed by the sum of factorials. For example 9, 9 = 1! + 2! + 3! . Dr. von Neumann was very interested in such numbers. So, he gives you a number n, and wants you to tell whether or not the number can be expressed by the sum of some factorials. Well, it is just a piece of case. For a given n, you will check if there are some xi, and let n equal to Σt (上标) i=1(下标) xi! (t≥1, xi≥0, xi = xj <==> i = j)            t      即 Σ  xi! (t≥1, xi≥0, xi = xj <==> i = j)           i=1     If the answer is yes, say "YES"; otherwise, print out "NO".

输入描述:

    You will get a non-negative integer n (n≤1,000,000) from input file.

输出描述:

    For the n in the input file, you should print exactly one word ("YES" or "NO") in a single line. No extra spaces are allowed.

示例1
输入

9
2

输出

YES
YES
import java.util.*;
import java.io.*;
import java.text.* ;
public class Main
{
    public static void main(String[] args){
    	try {
	        BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
	        String str;
	        int[] array = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800};
	        while((str = br.readLine()) != null) {
	        	int n = Integer.parseInt(str);
	        	boolean res = false;
	        	for(int i = 10; i >= 0; i--) {
	        		if(n >= array[i]) n -= array[i];
	        		if(n == 0) {
	        			res = true;
	        			break;
	        		}
	        	}
	        	if(res == true) System.out.println("YES");
	        	else System.out.println("NO");
	        }
 	    } catch (IOException e) {
	        e.printStackTrace();
	    }
    }
}

发布了332 篇原创文章 · 获赞 31 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43306331/article/details/104946154