Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > C/C++ > 将给定的字符串里面的每个字符左移N位(TopCoder)
【标  题】:将给定的字符串里面的每个字符左移N位(TopCoder)
【关键字】:TopCoder
【来  源】:http://blog.csdn.net/hxxiaopei/archive/2006/09/25/1281905.aspx

将给定的字符串里面的每个字符左移N位(TopCoder)

Your Ad Here

对于给定的字符串,如"ABC",如果每个字符左移N=1位,则为"ZAB",在TopCoder上作的,时间不长,感觉自己的方法已经很简练了,但是只有160分(满250),郁闷~给个更好的解法~

Problem Statement

    

Julius Caesar used a system of cryptography, now known as Caesar Cipher, which shifted each letter 2 places further through the alphabet (e.g. 'A' shifts to 'C', 'R' shifts to 'T', etc.). At the end of the alphabet we wrap around, that is 'Y' shifts to 'A'.

We can, of course, try shifting by any number. Given an encoded text and a number of places to shift, decode it.

For example, "TOPCODER" shifted by 2 places will be encoded as "VQREQFGT". In other words, if given (quotes for clarity) "VQREQFGT" and 2 as input, you will return "TOPCODER". See example 0 below.

代码:

#include <string>
using namespace std;
class CCipher
{public:
 string decode(string cipherText, int shift)
 {
  string ret = "";
  int len = cipherText.size();
  for(int i=0; i<len; i++)
  {
   char tmp='Z'-cipherText[i]+shift;
   tmp=tmp%26;//移位后于Z的距离~
   ret+=('Z'-tmp);//计算出对应的字符
  }
  return ret;   
 }
}; 

同样可以计算出右移,只需要将char tmp=cipherText[i]-'A'+shift  ret+=('A'+tmp)就可以了~

看了一个别人的代码:

ret+=cipherText[i]-shift<'A'?(cipherText[i]+'Z'-'A'+1-shift):(cipherText[i]-shift)确实简单~

实验1 创建简单的C++程序:【上一篇】
第4代白盒测试方法介绍--理论篇:【下一篇】
【相关文章】
  • 针对C程序员的TopCoder C++
  • TopCoder--计算矩形的公有面积
  • TopCoder--2006年GOOGLE算法竞赛练习赛的题目第一题
  • TopCoder--镜子反射路径问题.
  • TopCoder--计算矩形的公有面积2
  • 如何参与TopCoder比赛简介
  • 总奖金15万美元的TopCoder公开赛
  • 参加TopCoder公开赛有机会进入Yahoo工作
  • GOOGLE TOPCODER programming competition 2005
  • 【随机文章】
  • IP地址分类ABC
  • 流量调配隧道的配置
  • [分享]FreeBSD5.4升级到6.0笔记
  • Spring框架学习一----基本配置
  • 构造函数
  • desert this place.
  • 设置 MySQL 数据同步
  • 使用索爱V800开发流媒体应用程序
  • 下拉框的onchange事件的用法
  • 利用MyIM来做MP3播放器
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.