Help Please-I don't know what's wrong
Ali93 (12)
Dec 29, 2012 at 1:20pm UTC
Hi guys
I'm trying to solve a problem from website"UVa Online Judge"
I sent my answer 4 times and it's still wrong
Can any one help my please ?
This is the question:-
http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=3999
And this is my code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
#include<iostream>
#include<string>
using namespace std;
int main()
{
int count,temp,i=0,j=0,k=0,z=0,a=0;
string *list1,list2[5];
list2[0]="Happy" ;
list2[1]="birthday" ;
list2[2]="to" ;
list2[3]="you" ;
list2[4]="Rujia" ;
cin>>count;
temp=count;
list1=new string[count];
while (temp!=0)
{
cin>>list1[i];
temp--;
i++;
}
cout<<endl;
while (true )
{
cout<<list1[z]<<": " <<list2[a]<<endl;
z++;
a++;
if (a==3)
j++;
if (a==4)
{
k++;
a=0;
}
if (z==count)
z=0;
if (j==count)
{
cout<<list1[z]<<": " <<list2[4]<<endl;
z++;a++;j=0;
if (z==count)
z=0;
if (a==4)
{
k++;
a=0;
}
}
if (k==count+1)break ;
}
return 0;
}
Thanks in advance
ajh32 (163)
Dec 29, 2012 at 2:34pm UTC
This should solve the problem for you:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
#include <iostream>
#include <string>
#include <vector>
using namespace std;
const char * song[] = {"Happy" , "birthday" , "to" , "you" , "Rujia" };
struct member
{
string name;
bool has_sung;
member()
: name("" )
, has_sung(false )
{
}
member(string _name)
: name(_name)
, has_sung(false )
{
}
};
typedef vector<member> MEMBER_TYPE;
MEMBER_TYPE members;
void sing_song(MEMBER_TYPE::iterator p)
{
cout << endl;
for (int n = 0; n < 4; ++n)
{
for (int m = 0; m < (sizeof (song)/sizeof (song[0]))-1; ++m)
{
if (n == 2 && m == 3)
++m;
cout << p->name << ": " << song[m] << endl;
p->has_sung = true ;
++p;
if (p == members.end())
p = members.begin();
}
cout << endl;
}
}
int main()
{
int member_count;
cin >> member_count;
for (int n = 0; n < member_count; ++n)
{
member m;
cin >> m.name;
members.push_back(m);
}
for (MEMBER_TYPE::iterator p = members.begin(); p != members.end(); ++p)
{
if (!p->has_sung)
{
sing_song(p);
}
}
return 0;
}
Hope that help, let me know.
Andrew
Ali93 (12)
Dec 29, 2012 at 2:41pm UTC
Thanks Andrew for help ,,,
But Your code is false
ajh32 (163)
Dec 29, 2012 at 2:43pm UTC
What do you mean by my code is false? Works for me, as I tested it.
Ali93 (12)
Dec 29, 2012 at 2:57pm UTC
Yes it's work - but in the "UVa Online Judge" website your code is false.
If you didn't check my code - it's work and give the right answer but i don't know why it's false .
ajh32 (163)
Dec 29, 2012 at 3:15pm UTC
I see.
Maybe they're checking that you can't enter >100 characters for each name and that you ignore whitespace? Plus each name must start with an upper case char and all the rest must be lower case. Other than that I can't see what else it could be, unless their test case data is wrong!
Last edited on Dec 29, 2012 at 3:17pm UTC
ajh32 (163)
Dec 29, 2012 at 3:24pm UTC
BTW - you have a memory leak. You allocated memory on the heap for list1 and don't delete it.
cire (2347)
Dec 29, 2012 at 7:27pm UTC
On the left, output for the test case expected. On the right, your output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Mom: Happy
Dad: birthday
Girlfriend: to
Mom: you
Dad: Happy
Girlfriend: birthday
Mom: to
Dad: you
Girlfriend: Happy
Mom: birthday
Dad: to
Girlfriend: Rujia
Mom: Happy
Dad: birthday
Girlfriend: to
Mom: you
Mom: Happy
Dad: birthday
Girlfriend: to
Mom: you
Dad: Happy
Girlfriend: birthday
Mom: to
Dad: you
Girlfriend: Happy
Mom: birthday
Dad: to
Girlfriend: Rujia
Mom: Happy
Dad: birthday
Girlfriend: to
Mom: you
ajh32 (163)
Dec 29, 2012 at 8:20pm UTC
Well, if I edit my code to remove the endl both before and after the song output, according to the comment from cire above this code should give the same result:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
#include <iostream>
#include <string>
#include <vector>
using namespace std;
const char * song[] = {"Happy" , "birthday" , "to" , "you" , "Rujia" };
struct member
{
string name;
bool has_sung;
member()
: name("" )
, has_sung(false )
{
}
member(string _name)
: name(_name)
, has_sung(false )
{
}
};
typedef vector<member> MEMBER_TYPE;
MEMBER_TYPE members;
void sing_song(MEMBER_TYPE::iterator p)
{
for (int n = 0; n < 4; ++n)
{
for (int m = 0; m < (sizeof (song)/sizeof (song[0]))-1; ++m)
{
if (n == 2 && m == 3)
++m;
cout << p->name << ": " << song[m] << endl;
p->has_sung = true ;
++p;
if (p == members.end())
p = members.begin();
}
}
}
int main()
{
int member_count;
cin >> member_count;
for (int n = 0; n < member_count; ++n)
{
member m;
cin >> m.name;
members.push_back(m);
}
for (MEMBER_TYPE::iterator p = members.begin(); p != members.end(); ++p)
{
if (!p->has_sung)
{
sing_song(p);
}
}
return 0;
}
Topic archived. No new replies allowed.