I am referring to this https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=208 problem. The problem is easy and self-explanatory. I made 2 attempts out of which one was Accepted and other was given Wrong Answer verdict.
The code which got selected is below where I did char by char input and output,
#include <bits/stdc++.h>
using namespace std;
int main() {
char c;
int flag = 0;
while((c = getchar())!=EOF) {
if(c == '\"') {
if(flag) {
cout<<'\''<<'\'';
flag = 0;
}
else {
cout<<'`'<<'`';
flag = 1;
}
}
else {
cout<<c;
}
}
return 0;
}
Whereas as following is the code which got wrong answer, here I took a line as input at a time. To me it does the same thing, but wrong answer.
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
int flag = 0, c = 0;
while(getline(cin, s)) {
if(c) {cout<<'\n';} //don't print newline first time
c=1;
for(auto& x : s) {
if(x == '\"') {
if(flag) {
cout<<'\''<<'\'';
flag = 0;
}
else {
cout<<'`'<<'`';
flag = 1;
}
}
else {
cout<<x;
}
}
}
return 0;
}
I am not able to figure out what's the issue ? It will be helpful if someone can point out the problem in the second version.
Aucun commentaire:
Enregistrer un commentaire