hdu1200--To and Fro(找规律输入输出就行了)

本文介绍了一种名为ToandFro的加密方法,该方法通过改变字符串的存储规律进行加密。文章详细解析了加密过程,并提供了两种实现代码,帮助读者理解和实践这种独特的加密技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1200
To and Fro
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7828 Accepted Submission(s): 5319

Problem Description
Mo and Larry have devised a way of encrypting messages. They first decide secretly on the number of columns and write the message (letters only) down the columns, padding with extra random letters so as to make a rectangular array of letters. For example, if the message is “There’s no place like home on a snowy night” and there are five columns, Mo would write down

t o i o y
h p k n n
e l e a i
r a h s g
e c o n h
s e m o t
n l e w x

Note that Mo includes only letters and writes them all in lower case. In this example, Mo used the character ‘x’ to pad the message out to make a rectangle, although he could have used any letter.

Mo then sends the message to Larry by writing the letters in each row, alternating left-to-right and right-to-left. So, the above would be encrypted as

toioynnkpheleaigshareconhtomesnlewx

Your job is to recover for Larry the original message (along with any extra padding letters) from the encrypted one.

Input
There will be multiple input sets. Input for each set will consist of two lines. The first line will contain an integer in the range 2. . . 20 indicating the number of columns used. The next line is a string of up to 200 lower case letters. The last input set is followed by a line containing a single 0, indicating end of input.

Output
Each input set should generate one line of output, giving the original plaintext message, with no spaces.

Sample Input
5
toioynnkpheleaigshareconhtomesnlewx
3
ttyohhieneesiaabss
0

Sample Output
theresnoplacelikehomeonasnowynightx
thisistheeasyoneab

题意:找出字符串的存储规律,输出就行了:
如图模样存储,按列输出就行了。

见代码(注意空间的分配,不然会出错):

#include<stdio.h>
#include<string.h>
char ans[220][220];
int main()
{
    int n;
    char s[220];
    while(~scanf("%d",&n)&&n){
            scanf("%s",s);
            int j=1;
            int k=0;
        for(int i=1;i<=(int )strlen(s)/n;i++)
        {
			//分奇偶列存储,怕出错的话,可以输出中间量的变化即可
            //printf("%d+++++++++++\n",(int )strlen(s)/n);
            while(i%2!=0 &&j<=n)
            {
                //printf("%d %d**\n",j,k);
                ans[i][j++] = s[k++];

                if(j==n+1){
                    j--;
                    //k++;
                    break;
                }
            }
            while(i%2==0&&j>=1)
            {
                //printf("%d %d***********\n",j,k);
                ans[i][j--] = s[k++];

                 if(j==0)
                 {
                    j++;
                    //k++;
                    break;
                 }
            }
            //k--;

        }
        /*for(int i=1;i<=(int )strlen(s)/n+1;i++)
        {
            for(int j=1;j<=n;j++)
            {
                printf("%c ",ans[i][j]);
            }
            printf("\n");
        }*/
        for(int j=1;j<=n;j++)
        {
            for(int i=1;i<=(int )strlen(s)/n;i++)
            {
                printf("%c",ans[i][j]);
            }
            //
        }
        printf("\n");
    }
    return 0;
}

也有另外种代码,大家可以瞧瞧:

#include<stdio.h>
#include<string.h>
char a[220][220];
char b[220];
int main()
{
	int n,i,j;
	while(~scanf("%d",&n)&&n)
	{
		scanf("%s",b);
		int len=strlen(b);
		int j=1,k=0,flag=1;
		while(k<len)
		{
			for(i=1;i<=n;i++,k++)
			{
				if(k==len)
				{
					flag=0;
					break;
				}
				a[j][i]=b[k];
			}
			if(flag==0)
				break;
			j++;
			for(i=n;i>=1;i--,k++)
			{
				if(k==len)
				{
					flag=0;
					break;
				}
				a[j][i]=b[k];
			}
			if(flag==0)
				break;
			j++;
			
		}
		int t=j;
		for(j=1;j<=n;j++)
		{
			for(i=1;i<t;i++)
				printf("%c",a[i][j]);
		}
		printf("\n");
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值