I use c++,my code is behind. But there is a weird problem . When i define n as int and use cin>>n,the program can't receive the name have more than two word or have symbols like '-'. So i have to define n as a string and transfer the string into a int,why this strange thing happen? #include<iostream> #include<string.h> #include<cstring> using namespace std; int main() { char room[4][1000][210]; memset(room,0,sizeof(room)); int k=0,nth[4]={0}; char n[10]; cin.getline(n,10,'\n'); int len=10; while(n[len]=='\0'){len--;} int temp=1; for(int i=len;i>=0;i--) { k+=(n[i]-48)*temp; temp*=10; } for(int i=0;i<k;i++) { char temp[210],temp1[20]; cin.getline(temp,210,'\n'); cin.getline(temp1,20,'\n'); if(temp1[0]=='S') { strcpy(room[0][nth[0]],temp); nth[0]++; } else if(temp1[0]=='H') { strcpy(room[1][nth[1]],temp); nth[1]++; } else if(temp1[0]=='G') { strcpy(room[2][nth[2]],temp); nth[2]++; } else { strcpy(room[3][nth[3]],temp); nth[3]++; } } /* omit so this is not a correct solution*/ } #include <iostream> #include <string.h> int main() { char grif[1001][202],sliz[1001][202],huff[1001][202],rave[1001][202],name[202],group[20]; int n,i,gcur=0,scur=0,hcur=0,rcur=0; std:: cin >> n; while( n>=0 ) { std:: cin.getline(name,202); std:: cin.getline(group,20);
if(!strcmp(group,"Slytherin")) { strcpy(sliz[scur],name); scur++; } if(!strcmp(group,"Hufflepuff")) { strcpy(huff[hcur],name); hcur++; } if(!strcmp(group,"Gryffindor")) { strcpy(grif[gcur],name); gcur++; } if(!strcmp(group,"Ravenclaw")) { strcpy(rave[rcur],name); rcur++; }
--n; }
std::cout<<"Slytherin:\n"; for( i=0; i<scur; ++i ) std:: cout << sliz[i] << '\n'; std::cout<<'\n';
std::cout<<"Hufflepuff:\n"; for( i=0; i<hcur; ++i ) std:: cout << huff[i] << '\n'; std::cout<<'\n';
std::cout<<"Gryffindor:\n"; for( i=0; i<gcur; ++i ) std:: cout << grif[i] << '\n'; std::cout<<'\n';
std::cout<<"Ravenclaw:\n"; for( i=0; i<rcur; ++i ) std:: cout << rave[i] << '\n'; std::cout<<'\n';
return 0; } This is madness! Don't write such code =) int main() { int students; cin >> students; map< string, vector<string> > house_to_students; for (int student = 0; student < students; student++) { cin.ignore();
string student_name; getline(cin, student_name); string house; cin >> house; house_to_students[house].push_back(student_name); } for (auto house : { "Slytherin", "Hufflepuff", "Gryffindor", "Ravenclaw" }) { cout << house << ":" << endl;
for (auto student : house_to_students[house]) cout << student << endl; cout << endl; } } It happened bcs of cin function In the input they give n and endl after n; so, somehow getline reads this endl as a string, so you can do this insted of "cin>>n" use "scanf("%d\n", &n); I use fgets to read from stdin, when I try to store name in an array with length 201, i got WA 3,but when I changed the length to 210, I got AC, who can tell me why? i got it: if the name has exactly 200 symbols, the name and the newline will be read into two strings. This is one of the easiest problem in timus and I can't believe i'm getting WA1 for this problem. Im solving this problem using C++ and I'm using getline method from sstream to get the input. I'm storing the names in vectors and printing them with empty lines between the houses. I know Test 1 is usually the given data, but im getting WA1 even though I'm getting the same answer in my computer as given in the problem description. Does anyone know how to overcome this WA1 problem? Edited by author 03.01.2012 15:53 Ok so I found the problem, I know timus is piping the input and output via text files and the problem in that was that repeated getlines will fail. So I used the piping method as mentioned in timus and the problem was visible as it printed empty output to the output file. If you want to know how to pipe inputs and outputs follow the below link(look under Other notes section)- http://acm.timus.ru/help.aspx?topic=cppFor example : cin>>number; while(true){ getline(cin,input1); getline(cin,input2); if(somecondition) break; } However while(getline(cin,input)) {do} will work. But since we need 2 inputs the above code will fail as getline will return empty strings due to the reasons mentioned here- http://www.cplusplus.com/forum/articles/6046/ I overcame the problem using cin.ignore() just before entering the loop as to flush the input stream correctly and the corrected code of above may look like the code below- cin>>number; cin.ignore();//flushes input stream while(true){ getline(cin,input1); getline(cin,input2); if(somecondition) break; } If this didn't work then I suggest you to program in other languages. Edited by author 04.01.2012 10:41Thanks... great help indeed ... :) Give me SOLUTION ,please. Hint for C: Just Store and print. It might get a Memory Limit Exceeded. Then take some special care to avoid MLE. Edited by author 11.04.2006 23:52 it must work, but WA test 1? type elem=record name:string;fuck:string;end; var a:array[1..1000]of elem; n,i:word; begin readln(n); for i:=1 to n do begin readln(a[i].name,a[i].fuck); end; writeln('Slytherin:'); for i:=1 to n do if a[i].fuck='Slytherin' then writeln(a[i].name); writeln(''); writeln('Hufflepuff:'); for i:=1 to n do if a[i].fuck='Hufflepuff' then writeln(a[i].name); writeln(''); writeln('Gryffindor:'); for i:=1 to n do if a[i].fuck='Gryffindor' then writeln(a[i].name); writeln(''); writeln('Ravenclaw:'); for i:=1 to n do if a[i].fuck='Ravenclaw' then writeln(a[i].name); end. 8th line is wrong. (readln(a[i].name,a[i].fuck);) need two line readln(a[i].name); readln(a[i].fuck); Please delete your code. Edited by author 29.12.2006 10:44 1 aaaaaa bbbbb Gryffindor Edited by author 21.10.2013 15:02 give me some tests!!! 4 Ivan Ivanov Gryffindor Mac Go Nagolo Hufflepuff Um Bridge Slytherin Tatiana Henrihovna Grotter Ravenclaw If your code on pascal, don't use "CASE x oF" it's got CE(Compilation error) var n,i,j,k:integer; s,h,g,r:string; a:array[1..200] of string; begin s:='Slytherin'; h:='Hufflepuff'; g:='Gryffindor'; r:='Ravenclaw'; readln(n); for i:=1 to 2*n do readln(a[i]); writeln(s,':'); for i:=2 to 2*n do if a[i]=s then writeln(a[i-1]); writeln; writeln(h,':'); for i:=2 to 2*n do if a[i]=h then writeln(a[i-1]); writeln; writeln(g,':'); for i:=2 to 2*n do if a[i]=g then writeln(a[i-1]); writeln; writeln(r,':'); for i:=2 to 2*n do if a[i]=r then writeln(a[i-1]); end. a:array[1..2000] of string; #include <iostream> #include <string> using namespace std; int main () { string s[2002]; unsigned int n; bool t=false; bool t1=false; bool t2=false; bool t3=false; cin>>n; for( int i=1;i<=2*n;i++) { cin>>s[i]; if("Slytherin"==s[i]) t=true; if("Hufflepuff"==s[i]) t2=true; if("Gryffindor"==s[i]) t1=true; if("Ravenclaw"==s[i]) t1=true; } if(t) { cout<<"Slytherin:"<<endl; for(int i=1;i<=n;i++) if("Slytherin"==s[2*i]) cout<<s[2*i-1]<<endl; cout<<endl; } if(t2) { cout<<"Hufflepuff:"<<endl; for(int i=1;i<=n;i++) { if("Hufflepuff"==s[2*i]) cout<<s[2*i-1]<<endl; } cout<<endl; } if(t1) { cout<<"Gryffindor:"<<endl; for(int i=1;i<=n;i++) if("Gryffindor"==s[2*i]) cout<<s[2*i-1]<<endl; } if(t) { cout<<"Ravenclaw:"<<endl; for(int i=1;i<=n;i++) if("Ravenclaw"==s[2*i]) cout<<s[2*i-1]<<endl; } return 0; } Xatolaringiz juda ko'p, asosiy o'rindagi xato kiritish jarayonidagi xato! ya'ni "cin" operatori probel tashlanganda ham kiritish tugaganini bildiradi, ism esa 1 talik, 2-talik,3-talik... bo'lishi mumkin cin.getline(s[i]) yana qanday xato "cin" tashqari yana nima xato Решение на С дает OLE on test 3. Аналогичное решение на Pascal Accepted. !? Что это значит? строка, хранящая имя, должна содержать не 200 символов, а 201: один дополнительный символ для нуль-символа. у меня была такая же ошибка, и с таким исправлением я получил AC Thanks for your hint! строка, хранящая имя, должна содержать не 200 символов, а 201: один дополнительный символ для нуль-символа. у меня была такая же ошибка, и с таким исправлением я получил AC use English respect others! what can be the reason of Crash (access violation) on test 3.? Is it mistake in example or bug?(I write on pascal) Edited by author 13.12.2011 23:41 Your output is the same as in the sample test. :) usually i don't code in C# but I just found that C# is very good at this kind of problems... string manipulation is great in C#... you have many handy things such as List, Dictionary etc.. If you have WA1 on C++, it means that you are doing input not right. In that case I recommend you to decide this problem on Pascal - I haven't problems with input such I have in C++. The wrong line was: cout << endl << "Griffyndor:" << endl; Edited by author 26.10.2011 03:43 Edited by author 26.10.2011 19:58 Edited by author 26.10.2011 19:58 Solved! While solving this problem, pay attention to spelling of faculties ;-) Why I have WA? On my computer everything is ok... Please Help. Code: #include <cstdlib> #include <iostream> #include <string> using namespace std; int main(int argc, char *argv[]) { int n;
scanf("%d", &n);
string *tab = new string [n*2];
//-------------------INPUT------------------------
for(int i=0; i<n*2; i++){ getline(cin, tab[i]); }
string *list = new string [n+7];
//------------------Slytherin----------------------- int pos=0; //pos - position list[pos]="Slytherin:";
for(int i=0; i<n*2; i+=2){ if(tab[i+1][0]=='S'){ pos++; list[pos]=tab[i]; } } pos++; //------------------Hufflepuff----------------------- pos++; list[pos]="Hufflepuff:";
for(int i=0; i<n*2; i+=2){ if(tab[i+1][0]=='H'){ pos++; list[pos]=tab[i]; } } pos++; //------------------Gryffindor----------------------- pos++; list[pos]="Gryffindor:";
for(int i=0; i<n*2; i+=2){ if(tab[i+1][0]=='G'){ pos++; list[pos]=tab[i]; } } pos++; //------------------Ravenclaw----------------------- pos++; list[pos]="Ravenclaw:";
for(int i=0; i<n*2; i+=2){ if(tab[i+1][0]=='R'){ pos++; list[pos]=tab[i]; } }
//-------------------OUTPUT---------------------------
for(int i=0; i<n+7; i++){ cout << list[i] << endl; }
delete [] tab; delete [] list;
system("pause"); return EXIT_SUCCESS; } who knew, what is test #3? Oh,i find my mistake! now AC(#2716357 0.031 377 Kb). the last symbol must be '\n'!!! and you must use DP and you haven't MLE! (sorry for my english) |
|