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

2006年
问题一
题目描述

    The Fibonacci Numbers{0,1,1,2,3,5,8,13,21,34,55...} are defined by the recurrence:     F0=0 F1=1 Fn=Fn-1+Fn-2,n>=2     Write a program to calculate the Fibonacci Numbers.

输入描述:

    Each case contains a number n and you are expected to calculate Fn.(0<=n<=30) 。

输出描述:

   For each case, print a number Fn on a separate line,which means the nth Fibonacci Number.

示例1
输入

1

输出

1
import java.io.*;
import java.util.*;
public class Main
{
    public static void main(String[] args){
    	try {
	        BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
	        int n = Integer.parseInt(br.readLine());
	        System.out.println(fibonacci(n));
	    } catch (IOException e) {
	        e.printStackTrace();
	    }
    }
    public static int fibonacci(int n) {
    	if(n == 0) return 0;
    	if(n == 1) return 1;
        int f1 = 0;
        int f2 = 1;
        for(int i = 2; i <= n; i++) {
            int tmp = f2;
            f2 = f2+f1;
            f1 = tmp;
        }
        return f2;
    } 
}

问题二
题目描述

A common typing error is to place the hands on the keyboard one row to the right of the correct position. So "Q" is typed as "W" and "J" is typed as "K" and so on. You are to decode a message typed in this manner.

输入描述:

Input consists of several lines of text. Each line may contain digits, spaces, upper case letters (except Q, A, Z), or punctuation shown above [except back-quote (`)]. Keys labelled with words [Tab, BackSp, Control, etc.] are not represented in the input.

输出描述:

You are to replace each letter or punctuation symbol by the one immediately to its left on the QWERTY keyboard shown above. Spaces in the input should be echoed in the output.

示例1
输入

O S, GOMR YPFSU/

输出

I AM FINE TODAY.
import java.io.*;
import java.util.*;
public class Main
{
    public static void main(String[] args){
    	try {
	        BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
	        String str = br.readLine();
	        HashMap<Character, Character> map = new HashMap<>();
	        map.put('W', 'Q');
	        map.put('E', 'W');
	        map.put('R', 'E');
	        map.put('T', 'R');
	        map.put('Y', 'T');
	        map.put('U', 'Y');
	        map.put('I', 'U');
	        map.put('O', 'I');
	        map.put('P', 'O');
	        map.put('[', 'P');
	        map.put(']', '[');
	        map.put('\\', ']');
	        map.put('S', 'A');
	        map.put('D', 'S');
	        map.put('F', 'D');
	        map.put('G', 'F');
	        map.put('H', 'G');
	        map.put('J', 'H');
	        map.put('K', 'J');
	        map.put('L', 'K');
	        map.put(';', 'L');
	        map.put('\'', ';');
	        map.put('X', 'Z');
	        map.put('C', 'X');
	        map.put('V', 'C');
	        map.put('B', 'V');
	        map.put('N', 'B');
	        map.put('M', 'N');
	        map.put(',', 'M');
	        map.put('.', ',');
	        map.put('/', '.');
            map.put('1', '`');
            map.put('2', '1');
	        map.put('3', '2');
	        map.put('4', '3');
	        map.put('5', '4');
	        map.put('6', '5');
	        map.put('7', '6');
	        map.put('8', '7');
            map.put('9', '8');
            map.put('0', '9');
	        map.put('-', '0');
	        map.put('=', '-');
	        String[] parts = str.split(" ");
            char[] ch0 = parts[0].toCharArray();
            for(int j = 0; j < ch0.length; j++)
                ch0[j] = map.get(ch0[j]);
            System.out.print(new String(ch0));
	        for(int i = 1; i < parts.length; i++) {
	        	char[] ch = parts[i].toCharArray();
	        	for(int j = 0; j < ch.length; j++)
	        		ch[j] = map.get(ch[j]);
	        	System.out.print(" "+new String(ch));
	        }
        	System.out.print("\n");
	    } catch (IOException e) {
	        e.printStackTrace();
	    }
    }
}

题目描述

扫描二维码关注公众号,回复: 10246078 查看本文章
    Finding all occurrences of a pattern in a text is a problem that arises frequently in text-editing programs.     Typically,the text is a document being edited,and the pattern searched for is a particular word supplied by the user.       We assume that the text is an array T[1..n] of length n and that the pattern is an array P[1..m] of length m<=n.We further assume that the elements of P and  T are all alphabets(∑={a,b...,z}).The character arrays P and T are often called strings of characters.       We say that pattern P occurs with shift s in the text T if 0<=s<=n and T[s+1..s+m] = P[1..m](that is if T[s+j]=P[j],for 1<=j<=m).       If P occurs with shift s in T,then we call s a valid shift;otherwise,we calls a invalid shift.      Your task is to calculate the number of vald shifts for the given text T and p attern P.

输入描述:

   For each case, there are two strings T and P on a line,separated by a single space.You may assume both the length of T and P will not exceed 10^6.

输出描述:

    You should output a number on a separate line,which indicates the number of valid shifts for the given text T and pattern P.
import java.util.*;
import java.io.*;

public class Main {
	static int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
	static int[][] nums = new int[6][6];
	static int startX;
	static int startY;
	static int endX;
	static int endY;
	static int res = Integer.MAX_VALUE;
	static boolean[][] visited = new boolean[6][6];
	public static void main(String[] args) {
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			String[] parts = br.readLine().split(" ");
			int count = 0;
			for(int i = 0; i < parts[0].length() - parts[1].length()+1; i++) {
				int n = parts[0].indexOf(parts[1], i);
				if(n != -1) {
					count++;
					i = n;
				}
			}
			System.out.println(count);
		} catch(IOException e) {
			e.printStackTrace();
		}
	}
}

问题三:
题目描述

    Every positive number can be presented by the exponential form.For example, 137 = 2^7 + 2^3 + 2^0。     Let's present a^b by the form a(b).Then 137 is presented by 2(7)+2(3)+2(0). Since 7 = 2^2 + 2 + 2^0 and 3 = 2 + 2^0 , 137 is finally presented by 2(2(2)+2 +2(0))+2(2+2(0))+2(0).        Given a positive number n,your task is to present n with the exponential form which only contains the digits 0 and 2.

输入描述:

    For each case, the input file contains a positive integer n (n<=20000).

输出描述:

    For each case, you should output the exponential form of n an a single line.Note that,there should not be any additional white spaces in the line.

示例1
输入

1315

输出

2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2(0))+2+2(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));
	        int num = Integer.parseInt(br.readLine());
            if(num == 1) System.out.println("2(0)");
            else {
                StringBuilder res = getRes(num);
                int len = res.length();
                res.setLength(len-1);
                res.reverse();
                res.setLength(len-2);
                res.reverse();
                System.out.println(res);	                
            }	        
 	    } catch (IOException e) {
	        e.printStackTrace();
	    }
    }
    public static StringBuilder getRes(int num) {
    	if(num == 0) return new StringBuilder("(0)");
    	else if(num == 1) return new StringBuilder("");
    	else if(num == 2) return new StringBuilder("(2)");
    	ArrayList<Integer> list = new ArrayList<>();
    	int k = 0;
    	while(num > 0) {
    		if((num&1) == 1) list.add(k);
    		num >>= 1;
    		k++;
    	}
    	StringBuilder str = new StringBuilder();
    	str.append("(");
    	for(int i = list.size()-1; i > 0; i--) {
    		str.append("2");
    		str.append(getRes(list.get(i)));
    		str.append("+");
    	}
    	str.append("2");
    	str.append(getRes(list.get(0)));
    	str.append(")");
    	return str;
    }
}

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

猜你喜欢

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