///////////////////////////////////////////////////////////////////////////////////////////////////////
作者:tt267
聲明:本文遵循以下協議自由轉載-非商用-非衍生-保持署名|Creative Commons BY-NC-ND 3.0
轉載請註明:http://blog.csdn.net/tt2767/article/details/45967567
///////////////////////////////////////////////////////////////////////////////////////////////////////
題目鏈接:http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=584&pid=1002
這題好麻煩啊,wa了好幾次,開始的時候以爲思路錯了,換用一維數組存儲矩陣去做,結果超時了,後來一想是忽略了正常的沒有空的情況 ⊙▂⊙ ,加上之後就AC了。。。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#define INF 999999999
using namespace std;
char cip[100009];
int main()
{
int Case,key,id=1;
int j,i,k,space,l,col,row,fly,Count;
scanf("%d",&Case);
while(Case--)
{
getchar();
gets(cip);
scanf("%d",&key);
printf("Case #%d:\n",id++);
l = strlen(cip);
space = l % key; //最後一行的元素個數
col = l / key + (space?1:0);//原矩陣列數
//當密匙等於密文長度或等於1時明明什麼都沒做麼。。。。直接輸出就好
if(key == l || key ==1)
puts(cip);
else if(space) //如果存在空位要修改fly值進行輸出
{
for(i = 0 ; i < col -1 ; i ++)
{
fly= col;
Count = 0;
for(j = i ; Count != key ; j += fly)
{
printf("%c",cip[j]);
if(Count >=space)
fly = col-1;
Count++;
}
}
for(j = i ; space-- ; j += col)
{
printf("%c",cip[j]);
}
puts("");
}
else//不存在就正常輸出唄
{
for(i = 0 ; i < col ;i++)
{
for(j = 0 ; j < key ; j++)
{
printf("%c",cip[j*col+i]);
}
}
puts("");
}
}
return 0;
}