![:D](./images/smilies/icon_biggrin.gif)
400 - Unix ls
Moderator: Board moderators
400 WA... @@a
please help me....
I got WA so many times ...
the following is my code:
[c]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int num, maxlen, cols, rows;
int size[100];
char fname[100][100];
void clean()
{
int i, j;
num = maxlen = cols = rows = 0;
for(i = 0 ; i < 100 ; i++)
{
for(j = 0 ; j < 100 ; j++)
fname[j] = '\0';
size = 0;
}
}
void print_blank(char str[])
{
int len = strlen(str);
int b_len = maxlen + 2 - len;
int i;
for(i = 0 ; i < b_len ; i++)
printf(" ");
}
void print()
{
int i, j;
for(i = 0 ; i < 60 ; i++)
printf("-");
printf("\n");
if(maxlen + 2 > 60)
cols = 1;
else
cols = 60 / (maxlen + 2);
rows = num / cols;
if(num % cols != 0)
rows += 1;
for(i = 0 ; i < rows ; i++)
{
for(j = 0 ; j < cols ; j++)
{
printf("%s", fname[i + rows * j]);
if(i == rows - 1 && j == (num % cols) - 1)
break;
if(j != cols - 1)
print_blank(fname[i + rows * j]);
}
printf("\n");
}
}
void fsize()
{
int i, tmp;
maxlen = 0;
for(i = 0 ; i < num ; i++)
{
tmp = strlen(fname);
size = tmp;
if(tmp > maxlen)
maxlen = tmp;
}
}
void sort()
{
int i, j;
char tmp[100];
for(i = 0 ; i < num - 1 ; i++)
{
for(j = i + 1 ; j < num ; j++)
{
if(strcmp(fname, fname[j]) > 0)
{
strcpy(tmp, fname[j]);
strcpy(fname[j], fname);
strcpy(fname, tmp);
}
}
}
}
int main()
{
int i;
char tmp[100];
while(fgets(tmp, sizeof(tmp), stdin) != NULL)
{
num = atoi(tmp);
for(i = 0 ; i < num ; i++)
{
fgets(tmp, sizeof(tmp), stdin);
strncpy(fname, tmp, strlen(tmp) - 1);
}
sort();
fsize();
print();
clean();
}
return 0;
}
[/c]
I got WA so many times ...
![:cry:](./images/smilies/icon_cry.gif)
the following is my code:
[c]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int num, maxlen, cols, rows;
int size[100];
char fname[100][100];
void clean()
{
int i, j;
num = maxlen = cols = rows = 0;
for(i = 0 ; i < 100 ; i++)
{
for(j = 0 ; j < 100 ; j++)
fname[j] = '\0';
size = 0;
}
}
void print_blank(char str[])
{
int len = strlen(str);
int b_len = maxlen + 2 - len;
int i;
for(i = 0 ; i < b_len ; i++)
printf(" ");
}
void print()
{
int i, j;
for(i = 0 ; i < 60 ; i++)
printf("-");
printf("\n");
if(maxlen + 2 > 60)
cols = 1;
else
cols = 60 / (maxlen + 2);
rows = num / cols;
if(num % cols != 0)
rows += 1;
for(i = 0 ; i < rows ; i++)
{
for(j = 0 ; j < cols ; j++)
{
printf("%s", fname[i + rows * j]);
if(i == rows - 1 && j == (num % cols) - 1)
break;
if(j != cols - 1)
print_blank(fname[i + rows * j]);
}
printf("\n");
}
}
void fsize()
{
int i, tmp;
maxlen = 0;
for(i = 0 ; i < num ; i++)
{
tmp = strlen(fname);
size = tmp;
if(tmp > maxlen)
maxlen = tmp;
}
}
void sort()
{
int i, j;
char tmp[100];
for(i = 0 ; i < num - 1 ; i++)
{
for(j = i + 1 ; j < num ; j++)
{
if(strcmp(fname, fname[j]) > 0)
{
strcpy(tmp, fname[j]);
strcpy(fname[j], fname);
strcpy(fname, tmp);
}
}
}
}
int main()
{
int i;
char tmp[100];
while(fgets(tmp, sizeof(tmp), stdin) != NULL)
{
num = atoi(tmp);
for(i = 0 ; i < num ; i++)
{
fgets(tmp, sizeof(tmp), stdin);
strncpy(fname, tmp, strlen(tmp) - 1);
}
sort();
fsize();
print();
clean();
}
return 0;
}
[/c]
-
- Experienced poster
- Posts: 106
- Joined: Thu Jan 29, 2004 12:07 pm
- Location: Bangladesh
- Contact:
Re: 400 WA... @@a
I think you should revise the print() function you've made. In the part where you are calculating the number of collumns and rows seems little strange to me because I did not do it in this way. May this is the problem, may be not. Would you please explain why you are doing this ?
By the way, you can use memset() to clear the array instead of the clean() function you used
[c]
if(maxlen + 2 > 60)
cols = 1;
else
cols = 60 / (maxlen + 2);
rows = num / cols;
[/c]
By the way, you can use memset() to clear the array instead of the clean() function you used
400 is unclear
Problem 400 is unclear:
http://acm.uva.es/p/v4/400.html
The last sentence of the second paragraph says "Your program should use as few rows (R) as possible with rows being filled to capacity from left to right."
But in the output section it says "The sorted filenames 1 to R will be listed down column 1; filenames R+1 to 2R listed down column 2; etc." which suggests that columns should be filled to capacity and not rows.
I am aware of a solution posted at:
http://online-judge.uva.es/board/viewto ... hlight=400
but even it is only accepted with presentation error.
So which is it? Fill to capacity the rows or the columns?
Lets create a test case where columns > rows:
size=10, max_length=13 which implies 4 columns
Filling rows to capacity we have:
Filling columns to capacity we have:
Now a test case where columns < rows:
size=10, max_length=14 which implies 3 columns
Filling rowss to capacity we have:
Filling columns to capacity we have:
Perfect square test case (columns > rows or columns == rows):
size=16, max_length=10 which implies 5 columns
Filling rows to capacity we have:
Filling columns to capacity we have:
In the perfect square test case the fifth row could be eliminated if filling columns to capacity but that should be counted wrong according the to problem statement: "There will be as many columns as will fit in 60 characters." And since the last column is to be max_length and the others max_length+2 we have 1+(60-max_length)/(max_length+2) many columns; in this case 5. I suppose a good question then would be "does an empty column still count as a column?"
It is clear that "filling rows to capacity" and "filling columns to capacity" are two different things. It seems like the correct answer is to fill rows to capacity. Maybe the problem description could be corrected?
http://acm.uva.es/p/v4/400.html
The last sentence of the second paragraph says "Your program should use as few rows (R) as possible with rows being filled to capacity from left to right."
But in the output section it says "The sorted filenames 1 to R will be listed down column 1; filenames R+1 to 2R listed down column 2; etc." which suggests that columns should be filled to capacity and not rows.
I am aware of a solution posted at:
http://online-judge.uva.es/board/viewto ... hlight=400
but even it is only accepted with presentation error.
So which is it? Fill to capacity the rows or the columns?
Lets create a test case where columns > rows:
size=10, max_length=13 which implies 4 columns
Filling rows to capacity we have:
Code: Select all
1aaaaaaaaaaaa 4 7 9
2 5 8 10
3 6
Code: Select all
1aaaaaaaaaaaa 4 7 10
2 5 8
3 6 9
Now a test case where columns < rows:
size=10, max_length=14 which implies 3 columns
Filling rowss to capacity we have:
Code: Select all
1aaaaaaaaaaaaa 5 8
2 6 9
3 7 10
4
Code: Select all
1aaaaaaaaaaaaa 5 9
2 6 10
3 7
4 8
Perfect square test case (columns > rows or columns == rows):
size=16, max_length=10 which implies 5 columns
Filling rows to capacity we have:
Code: Select all
1aaaaaaaaa 5 8 11 14
2 6 9 12 15
3 7 10 13 16
4
Code: Select all
1aaaaaaaaa 5 9 13 ??????????
2 6 10 14 ??????????
3 7 11 15 ??????????
4 8 12 16 ??????????
^
what happened to the 5th row? ------------------|
It is clear that "filling rows to capacity" and "filling columns to capacity" are two different things. It seems like the correct answer is to fill rows to capacity. Maybe the problem description could be corrected?
have problems with 400 the UNIX ls one..
i tried all the input cases.. had gone thru the previous messages. but still get WA. i dont know why? could anyone please figure out the problem. the code is as follows:
[cpp]
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
int num; /* num indicates the number of names */
while(cin>>num)
{
char str[num][61];
unsigned int max_len=0;
for(int k=0;k<60;k++)
cout<<"-";
if(num==0)
continue;
for(int i=0;i<num;i++)
{
cin>>str;
// cout<<"\n"<<str;
int dum=strlen(str);
str[dum+1]='\0';
if(max_len<strlen(str))
max_len=strlen(str);
}
int num_r;
int num_c=(60-max_len)/(max_len+2)+1;
if(num_c!=0)
num_r=(num%num_c==0)? (num/num_c):(num/num_c+1);
else
num_c=1;
int count=0;
/* sorting */
for(int i=0;i<num;i++)
for(int j=i+1;j<num;j++)
if(strcmp(str[j],str)<0)
{
char k[60];
strcpy(k,str);
strcpy(str,str[j]);
strcpy(str[j],k);
}
/* printing in the order */
for(int i=0;i<num_r && count<num;i++)
{
// printf("\n%-*s",max_len,str);
// count++;
printf("\n");
for(int j=0;j<num_c&&count<num;j++)
{
if(j==num_c-1)
printf("%-*s",max_len,str[i+j*(num_r)]);
else
printf("%-*s",max_len+2,str[i+j*(num_r)]);
count++;
}
}
cout<<"\n";
}
}
[/cpp]
I also have another doubt. do we need to print a line for a case when input is 0 number of filenames? i tried both the cases .. one in which the output line was printed and the other in which it was not.. but there was problem with both.i got WA about 5 times. please help me.
.
[cpp]
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
int num; /* num indicates the number of names */
while(cin>>num)
{
char str[num][61];
unsigned int max_len=0;
for(int k=0;k<60;k++)
cout<<"-";
if(num==0)
continue;
for(int i=0;i<num;i++)
{
cin>>str;
// cout<<"\n"<<str;
int dum=strlen(str);
str[dum+1]='\0';
if(max_len<strlen(str))
max_len=strlen(str);
}
int num_r;
int num_c=(60-max_len)/(max_len+2)+1;
if(num_c!=0)
num_r=(num%num_c==0)? (num/num_c):(num/num_c+1);
else
num_c=1;
int count=0;
/* sorting */
for(int i=0;i<num;i++)
for(int j=i+1;j<num;j++)
if(strcmp(str[j],str)<0)
{
char k[60];
strcpy(k,str);
strcpy(str,str[j]);
strcpy(str[j],k);
}
/* printing in the order */
for(int i=0;i<num_r && count<num;i++)
{
// printf("\n%-*s",max_len,str);
// count++;
printf("\n");
for(int j=0;j<num_c&&count<num;j++)
{
if(j==num_c-1)
printf("%-*s",max_len,str[i+j*(num_r)]);
else
printf("%-*s",max_len+2,str[i+j*(num_r)]);
count++;
}
}
cout<<"\n";
}
}
[/cpp]
I also have another doubt. do we need to print a line for a case when input is 0 number of filenames? i tried both the cases .. one in which the output line was printed and the other in which it was not.. but there was problem with both.i got WA about 5 times. please help me.
![:oops:](./images/smilies/icon_redface.gif)
Life is like that
400 - Unix ls - PE
Hi!
I got PE in this problem and can't fix it!
I would be very appreciate if someone can find the bug in my code
Thanks in advance!
Regards
Javi
[cpp][/cpp]
I got PE in this problem and can't fix it!
I would be very appreciate if someone can find the bug in my code
Thanks in advance!
Regards
Javi
[cpp]
Code: Select all
Code removed
Last edited by JaviGS on Sat Aug 14, 2004 2:19 pm, edited 1 time in total.
Re: 400 - Unix ls - PE
There are many other posts about this issue. Please check them before posting a new topic.
7th Contest of Newbies
Date: December 31st, 2011 (Saturday)
Time: 12:00 - 16:00 (UTC)
URL: http://uva.onlinejudge.org
Date: December 31st, 2011 (Saturday)
Time: 12:00 - 16:00 (UTC)
URL: http://uva.onlinejudge.org
400 unix ls P.E!!!
400. unix ls
i think my answer is right.
but, always P.E
do you know any tip for 400?
how do i make a correct answer?
i think my answer is right.
but, always P.E
do you know any tip for 400?
how do i make a correct answer?
In 24 hr judge, PE is as good as AC, but incase you are wondering why they occur, here is the reason:
If you have extra spaces and newlines in your code, you will get PE. Since formatting of Output for this problem is more difficult than others, this problem is more prone to get PE, as extra spaces go unnoticed.
If you have extra spaces and newlines in your code, you will get PE. Since formatting of Output for this problem is more difficult than others, this problem is more prone to get PE, as extra spaces go unnoticed.
then, ?
then how can i fix my program?
i'll show you input & output
input
output
i really really want to get accept without P.E
i'll show you input & output
input
Code: Select all
10
tiny
2short4me
very_long_file_name
shorter
size-1
size2
size3
much_longer_name
12345678.123
mid_size_name
12
Weaser
Alfalfa
Stimey
Buckwheat
Porky
Joe
Darla
Cotton
Butch
Froggy
Mrs_Crabapple
P.D.
19
Mr._French
Jody
Buffy
Sissy
Keith
Danny
Lori
Chris
Shirley
Marsha
Jan
Cindy
Carol
Mike
Greg
Peter
Bobby
Alice
Ruben
5
abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij
abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghik
abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghid
abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghix
abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghib
Code: Select all
------------------------------------------------------------
12345678.123 size-1
2short4me size2
mid_size_name size3
much_longer_name tiny
shorter very_long_file_name
------------------------------------------------------------
Alfalfa Cotton Joe Porky
Buckwheat Darla Mrs_Crabapple Stimey
Butch Froggy P.D. Weaser
------------------------------------------------------------
Alice Chris Jan Marsha Ruben
Bobby Cindy Jody Mike Shirley
Buffy Danny Keith Mr._French Sissy
Carol Greg Lori Peter
------------------------------------------------------------
abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghib
abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghid
abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij
abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghik
abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghix
i really really want to get accept without P.E
It is difficult to tell, but perhpas you have extra spaces at the end of each line of output.
e.g
[c]
12345678.123 size-1
2short4me size2
mid_size_name size3
much_longer_name tiny
shorter very_long_file_name
[/c]
there could be some extra spaces in your output after size-1, Which should not be there.
Well this is just a suggestion.![:roll:](./images/smilies/icon_rolleyes.gif)
e.g
[c]
12345678.123 size-1
2short4me size2
mid_size_name size3
much_longer_name tiny
shorter very_long_file_name
[/c]
there could be some extra spaces in your output after size-1, Which should not be there.
Well this is just a suggestion.
![:roll:](./images/smilies/icon_rolleyes.gif)
Runtime Error (SIGFPE) in 400??????
i just want to know what is Runtime Error (SIGFPE)??
/* Sorry For Nothing */
You are doing something like this:
a = b / 0;
or
a = b % 0;
where b is an integer.
Read this:
http://online-judge.uva.es/board/viewforum.php?f=31
a = b / 0;
or
a = b % 0;
where b is an integer.
Read this:
http://online-judge.uva.es/board/viewforum.php?f=31
My signature:
- Please make discussion about the algorithm BRFORE posting source code.
We can learn much more in discussion than reading source code. - I HATE testing account.
- Don't send me source code for debug.