山寨版磁力蜈蚣
這題不難 模擬即可 但是並不是 真的要交換
只要正著印 跟 反著印就好
不過此題輸出有點大 導致輸出過慢
在此研究出了3種加快法
第一種 : 173 ms
也是最基本輸出
/*****************************************************/
#include<stdlib.h>
#include<stdio.h>
main()
{
int N,a,b;
while(scanf("%d",&N)==1)
{
int top=N-1,down=0,A[101];
for(a=0;a<N;a++)
scanf("%d",&A[a]);
for(a=0;a<N;a++,puts(""))
if(a%2==0)
{
for(b=down;b<=top;b++)
printf("%d ",A[b]);
down++;
}
else
{
for(b=top;b>=down;b--)
printf("%d ",A[b]);
top--;
}
}
return 0;
}
/***********************************************************/
第二種 : 154 ms
改用字串讀入 與 字串輸出
速度較快
/***********************************************************/
#include<stdlib.h>
#include<stdio.h>
main()
{
int N,a,b;
while(scanf("%d",&N)==1)
{
int top=N-1,down=0;
char A[101][3];
for(a=0;a<N;a++)
scanf("%s",&A[a]);
while(top>=down)
{
for(b=down;b<=top;b++)
printf("%s ",A[b]);
down++;
puts("");
for(b=top;b>=down;b--)
printf("%s ",A[b]);
top--;
puts("");
}
}
return 0;
}
/**********************************************************/
以下的內容 如果你要學 就必須遵守一個原則
"我分享的程式碼 不是給你抄來刷掉我的"
第三種 : 14ms
在以上的做法中 都要用for跑printf 去輸出
當然你想必也知道 printf也是內建 跑的也是很慢
所以在此改用一個字串紀錄要輸出的東西
之後只用"一次"的printf將所有資料輸出 (代價是記憶體去紀錄)
/**********************************************************/
#include<stdlib.h>
#include<stdio.h>
main()
{
int N,a,b;
while(scanf("%d",&N)==1)
{
int top=N-1,down=0,Atop=0;
char A[101][4],S[20000];
for(a=0;a<N;a++)
scanf("%s",A[a]);
while(top>=down)
{
for(b=down;b<=top;b++,S[Atop++]=' ')
{
S[Atop++]=A[b][0];
if(A[b][1]!='\0')
S[Atop++]=A[b][1];
}
down++;
S[Atop++]='\n';
for(b=top;b>=down;b--,S[Atop++]=' ')
{
S[Atop++]=A[b][0];
if(A[b][1]!='\0')
S[Atop++]=A[b][1];
}
top--;
S[Atop++]='\n';
}
S[Atop]='\0';
printf("%s",S);
}
return 0;
}
上一篇:矩形中的幾何