首页 > 专题系列 > Java解POJ > POJ 3650 The Seven Percent Solution [解题报告] Java
2013
11-13

POJ 3650 The Seven Percent Solution [解题报告] Java

The Seven Percent Solution

问题描述 :

Uniform Resource Identifiers (or URIs) are strings like http://icpc.baylor.edu/icpc/, mailto:[email protected], ftp://127.0.0.1/pub/linux, or even just readme.txt that are used to identify a resource, usually on the Internet or a local computer. Certain characters are reserved within URIs, and if a reserved character is part of an identifier then it must be percent-encoded by replacing it with a percent sign followed by two hexadecimal digits representing the ASCII code of the character. A table of seven reserved characters and their encodings is shown below. Your job is to write a program that can percent-encode a string of characters.

Character Encoding
” ” (space) %20
!” (exclamation point) %21
$” (dollar sign) %24
%” (percent sign) %25
(” (left parenthesis) %28
)” (right parenthesis) %29
*” (asterisk) %2a

输入:

The input consists of one or more strings, each 1–79 characters long and on a line by itself, followed by a line containing only “#” that signals the end of the input. The character “#” is used only as an end-of-input marker and will not appear anywhere else in the input. A string may contain spaces, but not at the beginning or end of the string, and there will never be two or more consecutive spaces.

输出:

For each input string, replace every occurrence of a reserved character in the table above by its percent-encoding, exactly as shown, and output the resulting string on a line by itself. Note that the percent-encoding for an asterisk is %2a (with a lowercase “a”) rather than %2A (with an uppercase “A”).

样例输入:

Happy Joy Joy!

http://icpc.baylor.edu/icpc/

plain_vanilla
(**)
?
the 7% solution
#

样例输出:

Happy%20Joy%20Joy%21

http://icpc.baylor.edu/icpc/

plain_vanilla
%28%2a%2a%29
?
the%207%25%20solution

解题代码:

//* @author 洪晓鹏<[email protected]>
import java.util.Scanner;


public class Main {
 public static void main(String[] args)
 {
  Scanner in = new Scanner(System.in);
   while(true)
    {
       String input = in.nextLine();
	if(input.equals("#"))
		break;
	int len = input.length();
	for(int i = 0; i < len; i++)
	{
		if(input.charAt(i)==' ')
			System.out.print("%20");
		else if(input.charAt(i)=='!')
			System.out.print("%21");
		else if(input.charAt(i)=='$')
			System.out.print("%24");
		else if(input.charAt(i)=='%')
			System.out.print("%25");
		else if(input.charAt(i)=='(')
			System.out.print("%28");
		else if(input.charAt(i)==')')
			System.out.print("%29");
		else if(input.charAt(i)=='*')
			System.out.print("%2a");
		else
			System.out.print(input.charAt(i));
	}
	System.out.println();
    }
  }
}

  1. 约瑟夫也用说这么长……很成熟的一个问题了,分治的方法解起来o(n)就可以了,有兴趣可以看看具体数学的第一章,关于约瑟夫问题推导出了一系列的结论,很漂亮

  2. 这道题目的核心一句话是:取还是不取。
    如果当前取,则index+1作为参数。如果当前不取,则任用index作为参数。

  3. Thanks for using the time to examine this, I truly feel strongly about it and enjoy finding out far more on this subject matter. If achievable, as you achieve knowledge

  4. 这道题目虽然简单,但是小编做的很到位,应该会给很多人启发吧!对于面试当中不给开辟额外空间的问题不是绝对的,实际上至少是允许少数变量存在的。之前遇到相似的问题也是恍然大悟,今天看到小编这篇文章相见恨晚。