400 - Unix ls
Moderator: Board moderators
here's my code .... maybe there is some error in it
<font size=-1>[ This Message was edited by: FlyDeath on 2002-02-01 13:51 ]</font>
Code: Select all
#include <stdio.h>
#include <string.h>
#include <math.h>
char name[100][100];
int in;
int len[100];
void sort()
{
int small;
char smallest[100];
int mark[100];
char temp[100][100];
int i,j,l;
int result=0;
for(i=0;i<100;i++)
{
mark[i]=0;
for(j=0;j<100;j++)
temp[i][j]=0;
}
for(i=0;i<in;i++)
{
for(j=0;j<100;j++)
smallest[j]='{';
for(j=0;j<in;j++)
{
if(mark[j]==1)
continue;
result=strcmp(smallest,name[j]);
if(result>0)
{
for(l=0;l<100;l++)
smallest[l]=name[j][l];
small=j;
}
}
for(j=0;j<100;j++)
temp[i][j]=name[small][j];
mark[small]=1;
}
for(i=0;i<100;i++)
for(j=0;j<100;j++)
name[i][j]=temp[i][j];
}
void main()
{
int i,j,k;
int maxlen=0;
int rows=0;
int left;
int line[30];
int now=0;
int cols=0;
int temp=0;
while(scanf("%d",&in)!=EOF)
{
if(in==0)
break;
maxlen=0;
gets(name[0]);
for(i=0;i<in;i++)
gets(name[i]);
for(i=0;i<in;i++)
len[i]=strlen(name[i]);
sort();
for(i=0;i<in;i++)
len[i]=strlen(name[i]);
for(i=0;i<in;i++)
if(maxlen<len[i])
maxlen=len[i];
rows=floor((double)(60-maxlen)/(maxlen+2))+1;
left=in-rows*floor((double)in/rows);
cols=floor((double)in/rows);
for(i=0;i<rows;i++)
{
line[i]=cols;
if(left>i)
line[i]++;
}
for(i=0;i<60;i++)
printf("-");
printf("n");
for(i=0;i<cols;i++)
{
for(j=0;j<rows;j++)
{
temp=0;
for(k=0;k<j;k++)
temp=temp+line[k];
if(j!=0)
printf(" ");
printf("%s",name[temp+i]);
if(j==rows-1)
for(k=len[temp+i];k<maxlen;k++)
printf(" ");
else
for(k=len[temp+i];k<maxlen;k++)
printf(" ");
}
printf("n");
}
for(i=0;i<left;i++)
{
temp=0;
for(k=0;k<i;k++)
temp=temp+line[k];
if(i!=0)
printf(" ");
printf("%s",name[temp+cols]);
if(i==rows-1)
for(k=len[temp+cols];k<maxlen;k++)
printf(" ");
else
for(k=len[temp+cols];k<maxlen;k++)
printf(" ");
}
if(left!=0)
printf("n");
}
}
You know what? I think you really could use some reading, starting from the page below:
http://acm.uva.es/board/faq.php#bbcode
Because, if you really want other people to revise your code, you could at least make your code look good and easy to read.
Ivor
(sorry, no offence meant)
http://acm.uva.es/board/faq.php#bbcode
Because, if you really want other people to revise your code, you could at least make your code look good and easy to read.
Ivor
(sorry, no offence meant)
400 WA
The problem seems to be very easy, but I get WA May be there is some tricks for the problem?
[pascal]Program p400;
Const MaxN = 200;
Var List : Array[1..MaxN]of String;
S : String;
i,j,L,p : Integer;
N,R,C : Integer;
begin
While Not Eof(InPut) Do begin
Readln(N);
for i:=1 to 60 do Write('-');
Writeln;
L:=1;
for i:=1 to N do begin
Readln(List);
if length(List)>L then L:=length(List);
end;
for i:=1 to N-1 do
for j:=i+1 to N do
if List>List[j] then begin
S:=List;
List:=List[j];
List[j]:=S;
end;
C:=1;
While True Do begin
C:=C+1;
if (C-1)*(L+2)+L>60 then Break;
end;
C:=C-1;
R:=N div C;
While R*C<N do R:=R+1;
for i:=1 to N do While length(List)<L do List:=List+' ';
for i:=1 to R do begin
p:=0;
for j:=1 to C do begin
While True Do begin
p:=p+1;
if (p-i) mod R=0 then Break;
end;
Write(List[p]);
if j<C then Write(' ') else Writeln;
end;
end;
end;
end.[/pascal]
[pascal]Program p400;
Const MaxN = 200;
Var List : Array[1..MaxN]of String;
S : String;
i,j,L,p : Integer;
N,R,C : Integer;
begin
While Not Eof(InPut) Do begin
Readln(N);
for i:=1 to 60 do Write('-');
Writeln;
L:=1;
for i:=1 to N do begin
Readln(List);
if length(List)>L then L:=length(List);
end;
for i:=1 to N-1 do
for j:=i+1 to N do
if List>List[j] then begin
S:=List;
List:=List[j];
List[j]:=S;
end;
C:=1;
While True Do begin
C:=C+1;
if (C-1)*(L+2)+L>60 then Break;
end;
C:=C-1;
R:=N div C;
While R*C<N do R:=R+1;
for i:=1 to N do While length(List)<L do List:=List+' ';
for i:=1 to R do begin
p:=0;
for j:=1 to C do begin
While True Do begin
p:=p+1;
if (p-i) mod R=0 then Break;
end;
Write(List[p]);
if j<C then Write(' ') else Writeln;
end;
end;
end;
end.[/pascal]
ACM 400
It alway gets Runtime Error. I don't know where the bugs are.
[c]
#include<stdio.h>
#include<string.h>
void main(void)
{
int n,x,y,row,col,maxlen,z,a;
char file[100][61],temp[61];
while(scanf("%d",&n)!=EOF)
{
for(x=0;x<n;x++)
scanf("%s",file[x]);
for(x=0;x<n-1;x++)
for(y=x+1;y<n;y++)
if(strcmp(file[x],file[y])>0)
{
strcpy(temp,file[x]);
strcpy(file[x],file[y]);
strcpy(file[y],temp);
}
for(x=0,maxlen=0;x<n;x++)
if(strlen(file[x])>maxlen)
maxlen=strlen(file[x]);
col=60/(maxlen+2);
row=n/col;
if(n%col!=0)
row++;
for(x=0;x<60;x++)
printf("-");
printf("\n");
y=0;
for(x=0;x<row;x++)
{
for(z=0;z<col;y+=row,z++)
if(y<n)
{
printf("%s",file[y]);
for(a=0;a<maxlen+2-strlen(file[y]);a++)
printf(" ");
}
printf("\n");
y=x+1;
}
}
}
[/c]
[c]
#include<stdio.h>
#include<string.h>
void main(void)
{
int n,x,y,row,col,maxlen,z,a;
char file[100][61],temp[61];
while(scanf("%d",&n)!=EOF)
{
for(x=0;x<n;x++)
scanf("%s",file[x]);
for(x=0;x<n-1;x++)
for(y=x+1;y<n;y++)
if(strcmp(file[x],file[y])>0)
{
strcpy(temp,file[x]);
strcpy(file[x],file[y]);
strcpy(file[y],temp);
}
for(x=0,maxlen=0;x<n;x++)
if(strlen(file[x])>maxlen)
maxlen=strlen(file[x]);
col=60/(maxlen+2);
row=n/col;
if(n%col!=0)
row++;
for(x=0;x<60;x++)
printf("-");
printf("\n");
y=0;
for(x=0;x<row;x++)
{
for(z=0;z<col;y+=row,z++)
if(y<n)
{
printf("%s",file[y]);
for(a=0;a<maxlen+2-strlen(file[y]);a++)
printf(" ");
}
printf("\n");
y=x+1;
}
}
}
[/c]
-
- New poster
- Posts: 12
- Joined: Mon Jul 29, 2002 3:04 pm
- Contact:
the following code gets WA. any help would be appreciated
[c]#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char input[100][100];
int sort_function( const void *a, const void *b);
int main()
{
int a,b,c,d,e,f,g,h,i,n;
while (scanf("%d%*c", &n) == 1)
{
for (i=0; i< n; i++)
{
gets(input);
}
b = 0;
for ( i = 0; i < n; i++) if (strlen(input) > b) b = strlen(input);
qsort ((void *)input, n, sizeof(input[0]), sort_function);
for (i =0; i<60; i++) printf("-");
printf("\n");
d = (60-b) / (b+2) + 1;
e = n / d + (!!( n % d));
for (f = 0; f< e; f ++)
for (c = 0; c < d; c++)
{
g = c * e + f;
if ( g < n)
{
printf("%s", input[g]);
if ( c < d-1)
{
h = (b + 2) - strlen(input[g]);
for (i = 0; i < h; i++)
printf(" ");
}
else printf("\n");
}
}
}
return 0;
}
int sort_function( const void *a, const void *b)
{
return ( strcmp ((char *) a, (char *) b));
}
[/c]
[c]#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char input[100][100];
int sort_function( const void *a, const void *b);
int main()
{
int a,b,c,d,e,f,g,h,i,n;
while (scanf("%d%*c", &n) == 1)
{
for (i=0; i< n; i++)
{
gets(input);
}
b = 0;
for ( i = 0; i < n; i++) if (strlen(input) > b) b = strlen(input);
qsort ((void *)input, n, sizeof(input[0]), sort_function);
for (i =0; i<60; i++) printf("-");
printf("\n");
d = (60-b) / (b+2) + 1;
e = n / d + (!!( n % d));
for (f = 0; f< e; f ++)
for (c = 0; c < d; c++)
{
g = c * e + f;
if ( g < n)
{
printf("%s", input[g]);
if ( c < d-1)
{
h = (b + 2) - strlen(input[g]);
for (i = 0; i < h; i++)
printf(" ");
}
else printf("\n");
}
}
}
return 0;
}
int sort_function( const void *a, const void *b)
{
return ( strcmp ((char *) a, (char *) b));
}
[/c]
-
- New poster
- Posts: 8
- Joined: Tue Jul 23, 2002 2:38 am
- Location: High Point, North Carolina / Goodlettsville, Tennesee
- Contact:
I too am having problems with this problem, namely that the online judge has told me a) compile error or b) runtime error (2 different submissions of the same exact source code). It runs perfectly for me, and I've been looking over it for a day or two and nothing stands out to me as wrong...
[cpp]
// http://acm.uva.es/p/v4/400.html
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
inline void pad(int x) {
for ( ; x > 0; --x)
cout << " ";
}
int main(void) {
int n;
register int i, x, longest, columns, rows, cur_cols;
string buf;
vector<string> filenames;
for (cin >> n; cin; cin >> n) {
// collect input and find largest filename
longest = 0;
for (i = n; i > 0; --i) {
cin >> buf;
if (buf.size() > longest)
longest = buf.size();
filenames.push_back(buf);
}
// data prep and needed info gathering
sort(filenames.begin(), filenames.end());
columns = (60 / (longest + 2));
rows = filenames.size() / columns;
if (filenames.size() % columns)
++rows;
// spit out mandatory seperator
for (i = 0; i < 60; ++i)
cout << "-";
cout << endl;
for (i = 0; i < rows; ++i) {
// spit out the first item in the row
cout << filenames;
pad(longest+2-filenames.length());
// spit out any other items in the row
//
// item finding technique:
// use number of rows needed total as an offset and keep it in
// check my making sure we're not using an out of range index and
// making sure we haven't outputed the number of columns needed
for (x = i + rows, cur_cols = 1;
x < filenames.size() && cur_cols < columns;
x += rows, ++cur_cols) {
cout << filenames[x];
pad(longest+2-filenames[x].length());
}
// time for the next row...
cout << endl;
}
filenames.clear();
}
return 0;
}
[/cpp]
any pointers would be appreciated
[cpp]
// http://acm.uva.es/p/v4/400.html
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
inline void pad(int x) {
for ( ; x > 0; --x)
cout << " ";
}
int main(void) {
int n;
register int i, x, longest, columns, rows, cur_cols;
string buf;
vector<string> filenames;
for (cin >> n; cin; cin >> n) {
// collect input and find largest filename
longest = 0;
for (i = n; i > 0; --i) {
cin >> buf;
if (buf.size() > longest)
longest = buf.size();
filenames.push_back(buf);
}
// data prep and needed info gathering
sort(filenames.begin(), filenames.end());
columns = (60 / (longest + 2));
rows = filenames.size() / columns;
if (filenames.size() % columns)
++rows;
// spit out mandatory seperator
for (i = 0; i < 60; ++i)
cout << "-";
cout << endl;
for (i = 0; i < rows; ++i) {
// spit out the first item in the row
cout << filenames;
pad(longest+2-filenames.length());
// spit out any other items in the row
//
// item finding technique:
// use number of rows needed total as an offset and keep it in
// check my making sure we're not using an out of range index and
// making sure we haven't outputed the number of columns needed
for (x = i + rows, cur_cols = 1;
x < filenames.size() && cur_cols < columns;
x += rows, ++cur_cols) {
cout << filenames[x];
pad(longest+2-filenames[x].length());
}
// time for the next row...
cout << endl;
}
filenames.clear();
}
return 0;
}
[/cpp]
any pointers would be appreciated