I have to discuss the Problem whose CodeID is “JOHNY”
I didn’t got an AC from this problem until I gave up using std:find in my answer;
I replaced
cout<< ((std::find(A,A+K,Uncle_Johny[0])-A)+1) <<endl;
with
for (int i=0; i<N; i++) {
if (A[i]==Uncle_Johny[0]) {
//cout<<“ANS=”;
cout<<(i+1)<<endl;break;
}
}
and then got an AC.
But I’m still wondering why using std::find got me a Wrong Answer…
Should I post all my code on this place?
#include <iostream>
#include <algorithm>
using namespace std;
void CASE() {
int N;
int A[100];
int Uncle_Johny[1];
int K;
cin>>N;
for (int i=0; i<N; i++) {
//A[i]=9999999;
cin>> A[i];
}
cin>>K;
Uncle_Johny[0]= A[K-1];
sort(A,A+N);
//cout<<"F=";
//cout<< ((std::search(A,A+K,Uncle_Johny,Uncle_Johny+1)-A)+1) <<endl;
cout<< ((std::find(A,A+K,Uncle_Johny[0])-A)+1) <<endl;
for (int i=0; i<N; i++) {
if (A[i]==Uncle_Johny[0]) {
//cout<<"ANS=";
cout<<(i+1)<<endl;break;
}
}
}
int main()
{
int T=(-1);
cin>>T;
for (int zz=0; zz<T; zz++) {
CASE();
}
return 0;
}
1 Like
This code prints out two answers per testcase, so no wonder it’s getting WA
Please post the code that only used std::find
but got WA.
Edit:
One thing that leaps out is with:
cout<< ((std::find(A,A+K,Uncle_Johny[0])-A)+1) <<endl;
you are only searching through the first K elements of A, instead of all of A.
2 Likes
Thanks - see my Edit, above
2 Likes
#include <iostream>
#include <algorithm>
using namespace std;
void CASE() {
int N;
int A[100];
int Uncle_Johny[1];
int K;
cin>>N;
for (int i=0; i<N; i++) {
//A[i]=9999999;
cin>> A[i];
}
cin>>K;
Uncle_Johny[0]= A[K-1];
sort(A,A+N);
//for (int i=0; i<N; i++) { cout<<"A: "<<A[i]<<","; }
//cout<<endl;
//cout<<"F=";
//cout<< ((std::search(A,A+K,Uncle_Johny,Uncle_Johny+1)-A)+1) <<endl; //為什麼這個輸出是錯的??
cout<< ((std::find(A,A+K,Uncle_Johny[0])-A)+1) <<endl;
/*
for (int i=0; i<N; i++) {
if (A[i]==Uncle_Johny[0]) {
//cout<<"ANS=";
cout<<(i+1)<<endl;break;
}
}
*/
}
int main()
{
int T=(-1);
cin>>T;
for (int zz=0; zz<T; zz++) {
CASE();
}
return 0;
}
I have got an AC by the following code
cout<< ((std::find(A,A+100,Uncle_Johny[0])-A)+1) <<endl;
But I am still wondering why…
In this problem, I put my data between A[0] and A[K-1].
There are only K elements mattering in this problem.
Why shall I search through all elements of this array?
Consider the testcase:
1
5
10 1 2 3 4
1
2 Likes