ZOJ 1006 Do the Untwish

时间:2023-03-09 20:10:50
ZOJ 1006 Do the Untwish

Do the Untwish

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1006

题意:给定密文按公式解密

注意点:pcode = (ccode + i)%28;的使用

贴代码:

 1 //Problem Name: Do the Untwish
2 //Source: ZOJ 1006
3 //Author: jinjin18
4 //Main idea: easy to solve
5 //Language: C++
6 //======================================================================
7 #include<stdio.h>
8 #include<string.h>
9 #include<map>
10 using namespace std;
11
12 map<char,int> mp;
13
14
15 void init(){
16 mp['_'] = 0;
17 mp['.'] = 27;
18 for(int i = 97; i < 97+26; i++){
19 mp[i] = i - 96;
20 }
21 return;
22 }
23
24 char Findchar(int v){
25 if(v==0){
26 return '_';
27 }
28 if(v==27){
29 return '.';
30 }
31 if(v<27&&v>0){
32 return v + 96;
33 }
34 return '\0';
35 }
36 int main(){
37
38 init();
39 int k;
40 char ptext[100];
41 char ctext[100];
42 while(scanf("%d",&k)!=EOF && k !=0 ){
43 scanf("%s",ctext);
44 int n = strlen(ctext);
45 ptext[n] = '\0';
46 for(int i = 0; i < n; i++){
47 int ccode = mp[ctext[i]];
48 //printf("%d ",ccode);
49 //int pcode = ccode < 28-i? ccode+i:ccode - 28 + i;
50 int pcode = (ccode + i)%28; //写成上面那行i超过28时会出错
51 //printf("%d\n",pcode);
52 ptext[(k*i)%n] = Findchar(pcode);
53
54
55 }
56 printf("%s\n",ptext);
57 }
58 return 0;
59
60 }