#include<iostream>
#include<conio.h>
#include<math.h>
#include<vector>
#include<iterator>
#include<string>
using namespace std;
int main() {
int k=0;
string s;
cout<<"string ";
getline(cin,s); //taking in a string from the user
float n=s.size(); //storing size of string
int f=floor((sqrt(n))); //floor of square root of input string
int c=ceil((sqrt(n))); //ceiling
int m=f*c; //storing product of f and c
vector< vector<string> > vec(n<=m?f:++f, vector<string>(c)); //makes a 2d vector
//depending on user's
//string length
for(int i=0;n<=m?i<f:i<++f;i++) //looping acc to user's input and assigning
{
for(int j=0;j<c;j++) //string to a matrix
{
if(k<s.size())
{
vec[i][j]=s[k];
k++;
}
}
}
for(int j=0;j<c;j++) //printing the vector
{
{
for(int i=0;n<=m?i<f:i<++f;i++)
cout<<vec[i][j];
}cout<<" ";
}
getch();
}
它不工作爲N> M作爲用於長度爲8個字符的字符串它使2 * 3從而不能括在基體中的整個字符串的矢量和這就是爲什麼我使用三元以便在遇到像這樣的情況時製作更大尺寸的矢量。 。那麼我做錯了什麼?基本模糊處理程序
我只會寫出整個問題。
One classic method for composing secret messages is called a square code. The spaces are removed from the english text and the characters are written into a square (or rectangle). The width and height of the rectangle have the constraint,
floor(sqrt(word)) <= width, height <= ceil(sqrt(word))
The coded message is obtained by reading down the columns going left to right. For example, the message above is coded as:
imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau
Sample Input:
chillout
Sample Output:
clu hlt io
你可以a)很好地格式化代碼,b)解釋算法的原理思想。猜測出代碼是耗時的(即使工作不正常,但) – dornhege
這段代碼是不可讀的Phylulu mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn –
爲Cthulu參考+1。 ;) – abelenky