Please read the new rule regarding the restriction on the use of AI tools. ×

Wrong answer on test 6 in problem 158A

Revision en1, by Akuver, 2019-12-30 09:20:43

I can't understand what is wrong with this piece of code of mine as a solution of the problem 158A:

include

using namespace std; int main() { int n,k,a,cnt=0,b; scanf("%d %d",&n,&k); for(int i=0;i<n;i++) { scanf("%d",&a); if(i==k) b==a; if(a>0 && cnt<k) cnt++; else { if(b==a & a>0) cnt++; } } printf("%d\n",cnt); return 0; } Then I submitted this solution and it worked:

include

using namespace std; int main() { int n,k,a,b; scanf("%d %d",&n,&k); for (int i = 0; i < k; ++i) { scanf("%d",&a); if (!a) { printf("%d\n",i); return 0; } } for (int i = k; i < n; ++i) { scanf("%d",&b); if (b != a) { printf("%d\n",i); return 0; } } printf("%d\n",n);
return 0; } Please help me understand why the code that I submitted earlier fails.

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en3 English Akuver 2019-12-30 20:33:35 54
en2 English Akuver 2019-12-30 20:32:52 835
en1 English Akuver 2019-12-30 09:20:43 1083 Initial revision (published)