算法练习(1)

来源:互联网 发布:网络悲伤情歌 编辑:程序博客网 时间:2024/06/03 16:56

Sumsets(2709)

The cows are thrilled because they’ve just learned about encrypting messages. They think they will be able to use secret messages to plot meetings with cows on other farms.
Cows are not known for their intelligence. Their encryption method is nothing like DES or BlowFish or any of those really good secret coding methods. No, they are using a simple substitution cipher.
The cows have a decryption key and a secret message. Help them decode it. The key looks like this:

yrwhsoujgcxqbativndfezmlpk

Which means that an ‘a’ in the secret message really means ‘y’; a ‘b’ in the secret message really means ‘r’; a ‘c’ decrypts to ‘w’; and so on. Blanks are not encrypted; they are simply kept in place.
Input text is in upper or lower case, both decrypt using the same decryption key, keeping the appropriate case, of course.
Input
* Line 1: 26 lower case characters representing the decryption key
* Line 2: As many as 80 characters that are the message to be decoded
Output
* Line 1: A single line that is the decoded message. It should have the same length as the second line of input.

Sample Input
eydbkmiqugjxlvtzpnwohracsf
Kifq oua zarxa suar bti yaagrj fa xtfgrj

Sample Output
Jump the fence when you seeing me coming

大致意思:原26位字母按照你第一列的输入进行对应,然后输出你第二列输入的对应结果,大小写也要对应。
解决思路:将输入拆成字母字符,存入数组,即用数组的下标当作原字母顺序(a~z)来对应第一列输入。然后要求结果的字符串,拆成字符,利用字符和ASCII值的对应找到其的数组下标,就知道对应的的字母了。其中要注意大小写要处理。

package com.lq.exercise011;import java.util.Scanner;public class Exercise01 {    public static void main(String[] args) {        Scanner scan=new Scanner(System.in);        int n=scan.nextInt();        int a='J';        System.out.println(a-'A');        int[] d=new int[n+1];        d[1]=1;        d[2]=2;        for(int i=3;i<d.length;i++) {            if(i%2!=0) {                d[i]=d[i-1];            }else {                d[i]=d[i-2]+d[i/2];            }        }        System.out.println(d[n]);    }}

Message Decowding(2716)

Farmer John commanded his cows to search for different sets of numbers that sum to a given number. The cows use only numbers that are an integer power of 2. Here are the possible sets of numbers that sum to 7:

1) 1+1+1+1+1+1+1
2) 1+1+1+1+1+2
3) 1+1+1+2+2
4) 1+1+1+4
5) 1+2+2+2
6) 1+2+4

Help FJ count all possible representations for a given integer N (1 <= N <= 1,000,000).
Input
A single line with a single integer, N.
Output
The number of ways to represent N as the indicated sum. Due to the potential huge size of this number, print only last 9 digits (in base 10 representation).

Sample Input
7
Sample Output
6
大致意思:输入一个数,求所有加数都是2的几次幂的和等于这个数,共有多少总加法。
思路:能找出规律:n为奇数:d[n]=d[n-1],//n为奇数时,就是n-1的所有可能方式上+1.
n为偶数:d[n]=d[n-2]+d[n/2] //直接理解没理解太清楚…..找规律可以找到……

package com.lq.exercise011;import java.util.Scanner;public class Exercise02 {    public static void main(String[] args) {        Scanner scan=new Scanner(System.in);        String str=scan.nextLine();        char[] key=str.toCharArray();        String p=scan.nextLine();        for(int i=0;i<p.length();i++) {            if(Character.isUpperCase(p.charAt(i))) {                System.out.print((char)(key[p.charAt(i)-'A']-'a'+'A'));            }else if( Character.isLowerCase(p.charAt(i))){                  System.out.print(key[p.charAt(i) - 'a']);              }else{                  System.out.print(p.charAt(i));              }          }    }}