You are not logged in. Please login at www.codechef.com to post your questions!

×

INTEST : Wrong Answer

0
1
#include <cstdio>
using namespace std;
int main(){
long long int n,k,ctr=0,*arr;
scanf("%I64d %I64d",&n,&k);
arr = new long long int[n];

for(long long int i=0;i<n;i++){
   scanf("%I64d",&arr[i]);
}

for(long long int j=0;j<n;j++){
   if(arr[j]%k==0){
      ++ctr; 
    }
}

printf("%I64d",ctr);
return 0;
}

I can't seem to figure out why this code is giving me a wrong answer on CodeChef ?

asked 28 Sep '14, 20:28

cpp11's gravatar image

0★cpp11
64
accept rate: 0%

edited 01 Oct '14, 00:03


Two wrong things at the same time!

1.) scanf("%d%d", &n, &k); There should be two "%d", u have to take values of both n and k, and ryt now you are taking only n.

2.) for(int i=0; i<=n :- Let me tell you, starting from i=0 to i=n, there are n+1 numbers, however you have to take only n numbers, So, "i<n" only.

So you are getting wrong answer because, k is taking value from the first element of the "so-typed-array" by you. Make the necessary changes.

link

answered 29 Sep '14, 21:39

pranjalranjan's gravatar image

4★pranjalranjan
2.0k21432
accept rate: 20%

Thanks ! I made the necessary corrections, but it still gives me a "Wrong Answer" on CodeChef. I also ran this code on Orwell Dev-C++ 5.7.1 - and it seems to be running fine on the test cases I tried it out on. Any suggestions ?

(30 Sep '14, 23:53) cpp110★
1

Woah! Hey i took ur code and made a small change. Now i am printing the elements as soon as they are being stored. U see this.

include <cstdio>

using namespace std;
int main(){
long long int n,k,ctr=0,*arr;
scanf("%I64d %I64d",&n,&k);
arr = new long long int[n];

for(long long int i=0;i<n;i++){
  scanf("%I64d",&arr[i]);
  printf("%I64d\n",arr[i]);
}

for(long long int j=0;j<n;j++){
   if(arr[j]%k==0){
     ++ctr;
   }
}

printf("%I64d",ctr);
delete[] arr;
return 0;
}

Just for testing.

(01 Oct '14, 12:21) pranjalranjan4★
1

Now what happened is:- U chck it urself. Type

7 3 1 51 966369 7 9 999996 11

which is the sample case given in problem and check the result! :-D u will be shocked. Now let me tell you the reason. The problem lies with "%I64d". Chnage it to "%lld" which is the standard specifier for long long integers. Ur output will be fine then.

(01 Oct '14, 12:32) pranjalranjan4★

That works ! Thanks a lot ! By the way, Can you explain why %lld works, but not %I64d ?

(01 Oct '14, 14:16) cpp110★
toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×2,476
×1,919
×856
×112
×49
×40
×32
×24

question asked: 28 Sep '14, 20:28

question was seen: 1,863 times

last updated: 01 Oct '14, 14:16