hdu1736

来源:互联网 发布:node.js和php哪个简单 编辑:程序博客网 时间:2024/05/16 17:57

题目来源:http://acm.hdu.edu.cn/diy/contest_showproblem.php?pid=1001&cid=31396&problem=Problem%20%20A

解法或类型:循环结构。

Description

请看下面两段文字:

Kenny喊道:"我来了!"

Kenny喊道:“我来了!”

前面一段文字中由于在中文中使用了英文标点,显得不太美观。本题中你的任务是让任意一段文字美观化。美观化具体要求为将以下字符(串)转换为对应的中文字符:


英文 中文
 ,   ,
 .   。
 !   !
 "   “或”
<<   《
>>    》
 ?   ?

Input

输入文字只有一段。文字中可能含有中英文、符号以及控制符(换行、空格、制表符等)。  

Output

按照要求输出美化后的文字段。你不用翻译以上指定的符号之外的所有字符 

Sample Input

Kenny喊道:"我来了!"

Sample Output

Kenny喊道:“我来了!”

题意描述
      把说给文段中的指定的英文标点换成中文标点。
解题思路
      用循环判断输入的每个字符,如果是题中制定的英文标点则直接输出对应的中文标点;其他的按原样输出。
 
时空分析: 

  

程序代码:

#include<stdio.h>

#include<iostream>

#include<string.h>

usingnamespace std;

intmain()

{

       char str[1000];

       int c,i,t;

       while(gets(str)!=NULL)

       {

              c=0;

              for(i=0;str[i]!='\0';i++)

              {  

                     stringtem="";

                     t=0;

                     tem=tem+str[i]+str[i+1];

           if(tem=="“"||tem=="”")

            { 

                c++;  

            }

            if(str[i]=='"') 

            { 

                if(c%2==0) 

                   {

                                    printf("“"); 

                      }

                            else 

                    {

                                  

                                   printf("”"); 

                       }

                            c++; 

                          t++;

            } 

                     if(str[i]==',') 

                     {     

                     printf(",");t++;

                     }

                      if(str[i]=='.')

                     {

                     printf("。");t++;

                     }

                      if(str[i]=='!')

                     {

                     printf("!");t++;

                     }

                     if(str[i]=='<'&&str[i+1]=='<')   

                     {

                     printf("《");t++;

                     i++;

                     }

                     if(str[i]=='>'&&str[i+1]=='>')

                     {

                     printf("》");t++;i++;

                     }

                     if(str[i]=='?')

                     {

                     printf("?");

                     t++;

                     }

                     if(t==0)

                     printf("%c",str[i]);

              }     

              printf("\n");

             

       }     

      

       return 0;

}  }

 

错误分析:

 1、遇到str[i]==’<’&& str[i+1]==’<’输出《,这样当后面循环到那个<时会输出一个<,在输出《之后需要跳过一个字符。

2、如果输入的有“而与之对应的是",此时"应转换为”而不是“。“不是一个字符需要用string定义的标准库中的字符串存入比较

3、

原创粉丝点击