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 }

你可能感兴趣的:(poj)