我如何解决方码程序?

时间:2014-11-11 09:30:42

标签: php

编写秘密消息的一种经典方法称为方形代码。从英文文本中删除空格,并将字符写入正方形(或矩形)。例如,句子“如果男人意味着留在 地面上帝会给我们根“是54个字符长,所以它被写成一个7行8列的矩形                 ifmanwas
                meanttos

                tayonthe
                groundgo
                dwouldha
                vegivenu
                sroots
通过向下读取从左到右的列来获得编码消息。例如,上面的消息编码为:
imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau
在您的程序中,让用户输入英文消息,单词之间不能有空格。最大消息长度为81 字符。显示编码信息。
(注意不要打印“垃圾”字符。)以下是一些例子:

 输入输出
hasaniceday hae and via ecy
feedthedog fto ehg ee dd
chillout clu hlt io

我怎么能解决这个问题,任何人都可以给我解决方案。

2 个答案:

答案 0 :(得分:1)

#include <stdio.h>
#include <iostream>
#include <string>
#include <math.h>

using namespace std;

int main()
{
string s;
cin>>s;
int len=s.length();
int width,height,i,j,k;

width=floor(sqrt(len)); //rows
height=ceil(sqrt(len)); //columns

for(i=0;i<height;i++)
{
for(j=0;j<height;j++)
{
k=(j*height)+i;
cout<<s[k];
}
cout<<"\t";
}
return 0;
}

答案 1 :(得分:0)

这是你的作业,希望你下次自己努力尝试

$plaintext = 'If man was meant to stay on the ground, god would have given us roots';

$message = strtolower(implode('', str_word_count($plaintext,2)));
$data = str_split($message, ceil(sqrt(strlen($message))));
array_walk($data,function(&$value) {$value = str_split($value,1);});
$data = call_user_func_array('array_map',array_merge(array(NULL),$data));
$ciphertext = implode(' ',array_map('implode',$data));

echo $ciphertext;