You are using a version without Ads of this website. Please, consider donating:

### Help Please-I don't know what's wrong

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

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667`` `````` #include #include 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<

This should solve the problem for you:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071`` ``````#include #include #include 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_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
Thanks Andrew for help ,,,

But Your code is false
What do you mean by my code is false? Works for me, as I tested it.
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 .
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
BTW - you have a memory leak. You allocated memory on the heap for list1 and don't delete it.
On the left, output for the test case expected. On the right, your output:

 ``1234567891011121314151617`` ``````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```
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:

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667`` ``````#include #include #include 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_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.

You are using a version without Ads of this website. Please, consider donating: