uva 400 - Unix ls
2013-08-12 12:50
363 查看
这道题很水 但是我做了一上午 改了很多次 2次RE 9次WA 最后发现原来是判断输出的边界的时候少减了个1 就这样错了一上午 不知道你遇到这种事的时候是怎么样的 反正我是想找块豆腐撞死算了 下面看代码吧
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
struct STR
{
char s[65];
int len;
};
int cmp(struct STR a, struct STR b)
{
int i;
for(i = 0; a.s[i]!='\0'&&b.s[i]!='\0'; i++)
if(a.s[i] < b.s[i])return 1;
else if(a.s[i] > b.s[i]) return 0;
if(a.s[i] == '\0')return 1;
if(b.s[i] == '\0')return 0;
}
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif // LOCAL
int N;
while(scanf("%d",&N)!=EOF)
{
struct STR str
;
getchar();
int maxn = -1;
for(int i = 0; i < N; i++)
{
gets(str[i].s);
str[i].len = strlen(str[i].s);
maxn = max(maxn,str[i].len);
}
int column = 60/(maxn+2);//这没判断是否为零就直接除RE了
if(column == 0)column++;
int row = N / column;
if(row*column < N)row++;
sort(str,str+N,cmp);
for(int i = 0; i < 60; i++)printf("-");
printf("\n");
for(int i = 0 ; i < row; i++)
{
for(int j = 0; j < column; j++)
{
if((i+j*row) > N-1)continue;//9次WA在这 就是N少减了个1啊
printf("%s",str[i+j*row].s);
for(int k =str[i+j*row].len; k < maxn;k++)
printf(" ");
if(j != column-1&&(i+j*row) != N)
printf(" ");
}
printf("\n");
}
}
return 0;
}
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
struct STR
{
char s[65];
int len;
};
int cmp(struct STR a, struct STR b)
{
int i;
for(i = 0; a.s[i]!='\0'&&b.s[i]!='\0'; i++)
if(a.s[i] < b.s[i])return 1;
else if(a.s[i] > b.s[i]) return 0;
if(a.s[i] == '\0')return 1;
if(b.s[i] == '\0')return 0;
}
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif // LOCAL
int N;
while(scanf("%d",&N)!=EOF)
{
struct STR str
;
getchar();
int maxn = -1;
for(int i = 0; i < N; i++)
{
gets(str[i].s);
str[i].len = strlen(str[i].s);
maxn = max(maxn,str[i].len);
}
int column = 60/(maxn+2);//这没判断是否为零就直接除RE了
if(column == 0)column++;
int row = N / column;
if(row*column < N)row++;
sort(str,str+N,cmp);
for(int i = 0; i < 60; i++)printf("-");
printf("\n");
for(int i = 0 ; i < row; i++)
{
for(int j = 0; j < column; j++)
{
if((i+j*row) > N-1)continue;//9次WA在这 就是N少减了个1啊
printf("%s",str[i+j*row].s);
for(int k =str[i+j*row].len; k < maxn;k++)
printf(" ");
if(j != column-1&&(i+j*row) != N)
printf(" ");
}
printf("\n");
}
}
return 0;
}
相关文章推荐
- 算法竞赛入门经典第五章例题5-8 Unix ls UVA - 400
- UVa——400Unix ls(字典序文本处理输出iomanip)
- UVa 400 Unix ls 命令
- UVA 400 Unix ls by sixleaves
- UVa 400 - Unix ls
- UVa 400 - Unix ls
- UVA 400 Unix ls
- Unix ls, UVa 400
- uva400 unix ls (字符串 排序)
- Uva - 400 - Unix ls
- Uva - 400 - Unix ls
- Unix ls UVA400
- UVA400-Unix的ls命令
- uva 400 Unix ls
- UVa--400 Unix ls(格式输出)
- 例题5-8 Unix ls 命令 UVa400
- uva 400 Unix ls 文件输出排版 排序题
- UVa 400 - Unix ls
- UVA 400-Unix ls【字符串】
- Uva400 Unix ls 【排序+技巧】【例5-8】