TopCoder 250 points 8-SRM 147 DIV 2 176.36/250 70.54%

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.

Definition

 
Class:CCipher
Method:decode
Parameters:String, int
Returns:String
Method signature:String decode(String cipherText, int shift)
(be sure your method is public)
 
 

Constraints

-cipherText has between 0 to 50 characters inclusive
-each character of cipherText is an uppercase letter 'A'-'Z'
-shift is between 0 and 25 inclusive

Examples

0) 
 
"VQREQFGT"
2
Returns: "TOPCODER"
 
1) 
 
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
10
Returns: "QRSTUVWXYZABCDEFGHIJKLMNOP"
 
2) 
 
"TOPCODER"
0
Returns: "TOPCODER"
 
3) 
 
"ZWBGLZ"
25
Returns: "AXCHMA"
 
4) 
 
"DBNPCBQ"
1
Returns: "CAMOBAP"
 
5) 
 
"LIPPSASVPH"
4
Returns: "HELLOWORLD"
 

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.     

这道也非常简单,DIV 2里面简单的练手题


public class CCipher {

	public  String decode(String cipherText, int shift) {
		int len = cipherText.length();

		if (len == 0)
			return "";
		StringBuilder sb = new StringBuilder();
		char array[] = cipherText.toCharArray();
		for (int i = 0; i < len; i++) {
			int a = array[i] - shift;
			int b = a + 26;
			array[i] = array[i] - 'A' >= shift ? (char) a : (char) b;
			sb.append(array[i]);
		}
		return sb.toString();

	}
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值