2078 Problem H Secret Message

题目描述

Jack and Jill developed a special encryption method, so they can enjoy conversations without worrrying about eavesdroppers. Here is how: let L be the length of the original message, and M be the smallest square number greater than or equal to L. Add (M − L) asterisks to the message, giving a padded message with length M. Use the padded message to fill a table of size K × K, where K2= M. Fill the table in row-major order (top to bottom row, left to right column in each row). Rotate the table 90 degrees clockwise. The encrypted message comes from reading the message in row-major order from the rotated table, omitting any asterisks.

For example, given the original message ‘iloveyouJack’, the message length is L = 12. Thus the padded message is ‘iloveyouJack****’, with length M = 16. Below are the two tables before and after rotation.
Then we read the secret message as ‘Jeiaylcookuv’.

输入

The first line of input is the number of original messages, 1 ≤ N ≤ 100. The following N lines each have a message to encrypt. Each message contains only characters a–z (lower and upper case), and has length 1 ≤ L ≤ 10 000.

输出

For each original message, output the secret message.

样例输入

2
iloveyoutooJill
TheContestisOver

样例输出

iteiloylloooJuv
OsoTvtnheiterseC

解题心得:
  题意是输入字符串(长度L),然后将每个字符从上到下,从左到右挨着放到一个k*k的方格里,剩余的用*补上,然后将方格顺时针旋转一下(90度),然后在按照从上到下,从左到右的顺序输出(*省略掉)。K值:先把L开方,然后向上取整再加1,然后平方,即得到K的值。
  我在做的时候忘记了把X清零,结果又是浪费了时间。。。

代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;

int main()
{
    int n;
    int x=0;
    int size=0;
    double k=0;
    int k1=0;
    int s;
    char a[10000];
    char c[100][100];
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        cin>>a;
        size=strlen(a);
        k=sqrt(size);
        k1=ceil(k);
        s=k1*k1;
        if(size!=s){
            int add=s-size;
            for(int i1=0;i1<add;i1++){
                a[size+i1]='*';
            }
            a[size+add]='\n';
        }
         for(int j=0;j<k1;j++){
                for(int j1=0;j1<k1;j1++){
                    c[j][j1]=a[x++];
                }
            }
            x=0; //做的时候被我落掉了,结果好久才找到错误!
            for(int r=0;r<k1;r++){
                for(int p=k1-1;p>=0;p--){
                    if(c[p][r]!='*'){
                        printf("%c",c[p][r]);
                    }
                }
            }
            printf("\n");

    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值