-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzoj 1006.cpp
87 lines (75 loc) · 1.61 KB
/
zoj 1006.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <stdio.h>
#include <string.h>
char plain[71];
int plaincode[71];
char cipher[71];
int ciphercode[71];
int length;
int key;
void encryption(int i);
int toint(char c);
char tochar(int i);
void tocode(char *text,int *code);
void totext(int *code,char *text);
int main()
{
int i;
while(scanf("%d",&key)&&key)
{
scanf("%s",cipher);
for(length=0;cipher[length]!='\0';length++);
tocode(cipher,ciphercode);
for(i=0;i<length;i++)
{
encryption(i);
}
totext(plaincode,plain);
plain[length]='\0';
printf("%s\n",plain);
}
return 0;
}
int toint(char c)
{
if(c=='_')
return 0;
if(c=='.')
return 27;
return (int)c-96;
}
char tochar(int i)
{
if(i==0)
return '_';
if(i==27)
return '.';
return (char)(i+96);
}
void tocode(char *text,int *code)
{
int i;
for(i=0;i<length;i++)
code[i]=toint(text[i]);
}
void totext(int *code,char *text)
{
int i;
for(i=0;i<length;i++)
text[i]=tochar(code[i]);
}
int mod(int p_code,int c_i,int c_code)
{
int m=(p_code-c_i)%28;
if(m < 0)
m+=28;
return m==c_code?1:0;
}
void encryption(int i)
{
int c_i=i,p_i;
int c_code,p_code,pa,pb;
p_i=key*c_i%length;
c_code=ciphercode[i];
for(p_code=0;!mod(p_code,c_i,c_code);p_code++);
plaincode[p_i]=p_code;
}