读书人

ACM题求解!请帮帮忙啦!

发布时间: 2012-03-05 11:54:02 作者: rapoo

ACM题求解!!!!请各位高手帮帮忙啦!!!
一道acm的题目,请给出具体的程序!呵呵,先谢谢啦!!

Julia has decided to encrypt her notes so that nobody else could understand
them. The code is based on the so-called Caesar Cipher where each letter is
shifted a certain number of places left or right through the alphabet. In this
context, the alphabet is treated as being circular so that the first letter follows
after the last letter, and the last letter precedes the first letter.
Julia applies these ideas separately to uppercase letters, lower case letters, and
digits. For example, with a shift of 1, ‘A’ becomes ‘B’, ‘Z’ becomes ‘A’, ‘a’ becomes
‘b’, ‘z’ becomes ‘a’, ‘0’ becomes ‘1’, ‘9’ becomes ‘0’. Spaces, punctuation, and
any other symbols are not affected in this scheme.
Your task is to help Julia encrypt her notes.
INPUT FORMAT
Each line of input begins with a number representing the shift. The number will be
in the range -1,000,000,000 to 1,000,000,000. The number is followed by a colon
(‘:’). The rest of the line consists of a string of 1 to 200 arbitrary characters and
represents a fragment of the text to be encrypted. A single ‘#’ on a line by itself
indicates the end of input.
SAMPLE INPUT:
0:Clear text!
1:David and Jane’s wedding, March 2002, Alexandria
-1:Bahamas Holiday August 2001
53:ACMZ, acmz, 0379!
26000000:ACMZ, acmz, 0379!
26000001:ACMZ, acmz, 0379!
#

SAMPLE OUTPUT:
Clear text!
Ebwje boe Kbof’t xfeejoh, Nbsdi 3113, Bmfyboesjb
Azgzlzr Gnkhczx Ztftrs 1990
BDNA, bdna, 3602!
ACMZ, acmz, 0379!
BDNA, bdna, 1480!

[解决办法]
把number分别对26,10求模,
然后判断字符的范围就可以了

int i_alpha=number % 26;
int i_number=number % 10;
if (i_alpha <0) i_alpha+=i_alpha+26;
if (i_number <0) i_number+=i_number+10;

while(s[i]!= '\0 ')
{
if (s[i]> = 'A ' and s[i] <= 'Z ')
{
s[i]= 'A '+(s[i]- 'A '+i_alpha)%26;
}
else if (s[i]> = 'a ' and s[i] <= 'z ')
{
s[i]= 'a '+(s[i]- 'a '+i_alpha)%26;
}
else if (s[i]> = '0 ' and s[i] <= '9 ')
{
s[i]= '0 '+(s[i]- '0 '+i_number)%10;
}



++i;
}

读书人网 >软件架构设计

热点推荐