POJ 1589

题意:给出n个字符串,排成阵列,要求每行长度不超过60,且每列宽度为n个字符串中最长的那个的长度加2,最后一列就是最长的长度,要使行数尽可能少。

题解:直接推导出最后需要多少行多少列,细心一点就没问题了。

View Code
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 char file[102][102][65];
 6 struct data
 7 {
 8     char s[70];
 9     bool operator<(const data &ne)const
10     {
11         return strcmp(s,ne.s)<0;
12     }
13 }po[102];
14 int main()
15 {
16     int n,r,c,mlen;
17     while(scanf("%d",&n)!=EOF)
18     {
19         memset(file,'\0',sizeof(file));
20         mlen=0;
21         for(int i=0;i<n;i++)
22         {
23             scanf("%s",po[i].s);
24             mlen=max(mlen,(int)strlen(po[i].s));
25         }
26         sort(po,po+n);
27         c=(60-mlen)/(mlen+2)+1;
28         r=n/c+(n%c!=0);
29         int nu=0;
30         for(int i=0;i<c&&nu<n;i++)
31             for(int j=0;j<r&&nu<n;j++)
32                 strcpy(file[j][i],po[nu++].s);
33         puts("------------------------------------------------------------");
34         for(int i=0;i<r;i++)
35         {
36             for(int j=0;j<c;j++)
37             {
38                 if(file[i][j][0]!='\0')
39                     printf("%s",file[i][j]);
40                 int k=strlen(file[i][j]);
41                 while(k<mlen)
42                     putchar(' '),k++;
43                 if(j==c-1)
44                     printf("\n");
45                 else
46                     printf("  ");
47             }
48         }
49     }
50     return 0;
51 }

转载于:https://www.cnblogs.com/tmeteorj/archive/2012/10/18/2729370.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值