Fork me on GitHub

NYOJ题目770仿射密码

------------------------------------------------

跟另一道题的解法是一样的,因为考虑到是解密使用所以做映射表的时候就要方便解密的做。

AC代码:

 1 import java.io.BufferedReader;
 2 import java.io.IOException;
 3 import java.io.InputStreamReader;
 4 
 5 public class Main {
 6 
 7     public static void main(String[] args) throws NumberFormatException, IOException {
 8         
 9         BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
10         
11         boolean first=true;
12         while(first || reader.ready()){
13             first=false;
14             
15             String ss[]=reader.readLine().split(" ");
16             
17             initMapping(Integer.parseInt(ss[1]),Integer.parseInt(ss[2]));
18             char ans[]=decoding(ss[0].toCharArray());
19             
20             System.out.println(new String(ans));
21         }
22         
23     }
24     
25     public static int mapping[]=new int[26];
26     
27     public static void initMapping(int k1,int k2){
28         for(int i=0;i<26;i++){
29             mapping[(k1*i+k2)%26]=i;
30         }
31     }
32     
33     public static char[] decoding(char cs[]){
34         for(int i=0;i<cs.length;i++){
35             cs[i]=(char) (mapping[cs[i]-'A']+'A');
36         }
37         return cs;
38     }
39     
40 }

 

题目来源: http://acm.nyist.net/JudgeOnline/problem.php?pid=770

posted @ 2016-08-29 17:19  CC11001100  阅读(447)  评论(0编辑  收藏  举报