发布网友
共2个回答
热心网友
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
const int MAXSIZE = 100;
char *encrypt(char *essay) {
int i,j,n,u,v,m,len = strlen(essay);
char *result;
m = n = (int)sqrt(len);
if(m * m < len) ++n;
result = (char *)malloc(m * n * sizeof(char) + 1);
for(i = 0; i < m; ++i) {
for(j = 0; j < n; ++j) {
u = n * i + j;
v = m * j + i;
if(u < len) result[v] = essay[u];
else result[v] = ' ';
}
}
result[m * n] = 0;
return result;
}
int main() {
char s[MAXSIZE],*u;
printf("输入明文:\n");
fgets(s,MAXSIZE,stdin);
s[strlen(s) - 1] = 0; // 去除尾部的\n
u = encrypt(s);
printf("密文是:\n%s\n",u);
free(u);
return 0;
}
热心网友
#include <stdio.h>
int main(){
char plain[20][10], ch;
int i=0, j=0, len=0;
ch=getchar();
while(ch!=EOF){
if(ch!=' '){
plain[i][j%10]=ch;
j++;
if(j%10==0)
i++;
len++;
}
ch=getchar();
}
for(j=0;j<10;j++){
for(i=0;i<len/10;i++)
printf("%c",plain[i][j]);
}
}
if(len%10 !=0){
for(j=0;j<len%10;j++)
printf("%c",plain[i][j]);
}