poj1317解题报告

来源:互联网 发布:太极拳教学软件 编辑:程序博客网 时间:2024/06/05 00:32

题意:对密文进行解码,就按题目中给出的公式

虽然有些感觉没那个必要那么写,感觉是多余的,但我感觉那样会增加可读性,看起来也很清晰,所以~~~

以前不知道在那里见过,今天写了还是贡献了一个wa。错误点在代码中已说明

#include<iostream>#include<string>using namespace std;

int k;char plainText[80];int plainCode[80];int cipherCode[80];char cipherText[80];

void toCipherCode(){ int i; int len = strlen(cipherText); for(i = 0;i < len;i++) {  if(cipherText[i] == '.')   cipherCode[i] = 27;  else if(cipherText[i] == '_')   cipherCode[i] = 0;  else   cipherCode[i] = (cipherText[i] - 'a' + 1); }

}void toPlainCode(){ int i; int len = strlen(cipherText); for(i = 0;i < len;i++) {  int kk = (k*i)%len;  plainCode[kk] = (cipherCode[i] + i)%28; }}void toPlainText(){ int i; int len = strlen(cipherText); for(i = 0;i < len;i++) {  if(plainCode[i] == 0)  {   plainText[i] = '_';  }  else if(plainCode[i] == 27 )      plainText[i] = '.';  else   plainText[i] = (plainCode[i] + 'a' - 1);

 } plainText[i] = '\0'; //一开始忘了加这行代码了,贡献了一个wa。吃完饭在回来的路上猛然想起来了。

}void solve(){ toCipherCode(); toPlainCode(); toPlainText();}int main(){ while(cin>>k && k) {  cin>>cipherText;  solve();  cout<<plainText<<endl; } return 0;}

原创粉丝点击