2.7 The Seven Percent Solution(七个百分比编码)

                         2.7 The Seven Percent Solution 

2.7.1 时空限制

        Time limit: 1 Seconds Memory limit: 32768K 

2.7.2 题目内容

     Uniform Resource Identifiers (or URIs) are strings like http://icpc.baylor.edu/icpc/, mailto: foo@bar.org, 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. 

       Input 

         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. 

      Output 

         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").

      Sample Input 

            Happy Joy Joy! 
            http://icpc.baylor.edu/icpc/ 
            plain_vanilla 
            (**) 
            ? 
            the 7% solution 
            #

      Sample Output 

            Happy%20Joy%20Joy%21 
            http://icpc.baylor.edu/icpc/ 
            plain_vanilla 
            %28%2a%2a%29 
            ? 
            the%207%25%20solution 

2.7.3 题目来源

        The 2007 ACM Mid-Central USA Programming Contest 

2.7.4 汉语翻译

     1.题目

                                                 七个百分比编码

       统一资源标识(或URI)是这样一些字符串“http://icpc.baylor.edu/icpc/”,“mailto:foo @bar.org”,“ftp://127.0.0.1/pub/linux”,甚至是只是“readme.txt”,它们通常是用来标识互联网上或本地计算机上的一个资源。
       在 URI 中有一些保留字符,如果在 URI 中包含了这些保留字符,那么,这些保留字符就要被一个百分符号“%”加两位十进制数来代替。7 种百分比编码列在下表中。你的任务就是编制一个程序将保留字符用它们来替代。

     2.输入描述

       输入由一个或多个字符串组成,每个字符串由 1~79 个字符构成且在一行上,以“#”号结束。全部输入数据中,只在最后包含一个“#”号,不会出现在其他地方。一个字符串可能包含多个空格,但空格不出现在字符串的开头和结尾位置,也不会连续出现两个或两个以上的空格。

     3.输出描述

     对于每个测试案例,使用表中的 7 种百分比将保留字符替代,每个测试案例输出一行。注意,星号的百分比替代符是%2a(这里是小写字母 a),而不是%2A(这里是大写字母 A)。

      4.输入样例

            Happy Joy Joy! 
            http://icpc.baylor.edu/icpc/ 
            plain_vanilla 
            (**) 
            ? 
            the 7% solution 
            # 

       5.输出样例

            Happy%20Joy%20Joy%21 
            http://icpc.baylor.edu/icpc/ 
            plain_vanilla 
            %28%2a%2a%29 
            ? 
            the%207%25%20solution 

#include <iostream>
#include <string>  
using namespace std; 
int main(int argc, char* argv[]) 
{ 
    char ch[256];
    while(cin.getline(ch,256))
	{
		if(ch[0]=='#') break;
		for(int i=0;ch[i]!='\0';i++)
		{
			switch(ch[i])
			{
			    case ' ':  cout<<"%20"; continue;
			    case '!':  cout<<"%21"; continue;
				case '$':  cout<<"%24"; continue;
				case '%':  cout<<"%25"; continue;
				case '(':  cout<<"%28"; continue;						
				case ')':  cout<<"%29"; continue;
			    case '*':  cout<<"%2a"; continue;
				default: cout<<ch[i]; continue;
			}
		}
		cout<<endl;
	}
    return 0; 
} 

2.7.5 解题思路

      本题要求将一个字符串中的 7 个保留字符替换成 7 种百分比,由于一个字符串在一行上,一行中也存在若干个空格,所以,需要采用 cin.getline()方法一次读入一行,再去处理。
      输出的时候特别要注意,由于%在 C 语言中是保留字符,使用 C 语言的 printf 函数输出,应当输出“%%”这个转义符。当然,如果采用 C++语言的 cout 输出,就不必考虑这个问题。这是两种输出的一个不同之处。

2.7.6   参考答案

#include <iostream> 
#include <string> 
using namespace std; 
int main(int argc, char* argv[]) 
{  
    string s; 
    char ss[80]; 
    while(cin.getline(ss,80)) 
	{ 
		if(ss[0]=='#')break; 
        s=ss; 
		for(int i=0;i<s.size();i++) 
		{ 
			//if(s[i]==' ')cout<<"%20"; 
			if(s[i]==' ')printf("%%20"); 
			//else if(s[i]=='!')cout<<"%21"; 
			else if(s[i]=='!')printf("%%21"); 
			//else if(s[i]=='$')cout<<"%24"; 
			else if(s[i]=='$')printf("%%24"); 
			//else if(s[i]=='%')cout<<"%25"; 
			else if(s[i]=='%')printf("%%25"); 
			//else if(s[i]=='(')cout<<"%28"; 
			else if(s[i]=='(')printf("%%28"); 
			//else if(s[i]==')')cout<<"%29"; 
			else if(s[i]==')')printf("%%29"); 
			//else if(s[i]=='*')cout<<"%2a"; 
			else if(s[i]=='*')printf("%%2a"); 
			//else cout<<s[i]; 
			else printf("%c",s[i]); 
		} 
		cout<<endl; 
	} 
	return 0; 
} 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值