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

×

Enormous input test

#include<iostream>
using namespace std;
int main()
{unsigned long n,k,t;
 scanf("%lu,%lu",&n,&k);
 int i = 0;
 int count = 0;
 while(i < n)
 {scanf("%lu",&t);
  if (t%k==0)
  count++;
  i++;
 }
 printf("%lu",count);
}

Why is this giving wrong answer?

asked 18 Feb '15, 23:15

xenocide15's gravatar image

0★xenocide15
1
accept rate: 0%

edited 18 Feb '15, 23:22

kunal361's gravatar image

4★kunal361
6.0k133272


The problem is in this line

scanf("%lu,%lu",&n,&k);

It should be

scanf("%lu %lu",&n,&k);

That is you should separate the %lu with a space and not a comma. It's also best if you change i and count to unsigned long, just to be safe. Once you change that, your program gives the right output ( well it gave the right output for the conditions that I tried ).

Hope this solves your problem

link

answered 19 Feb '15, 11:26

arun_as's gravatar image

1★arun_as
7413618
accept rate: 10%

edited 19 Feb '15, 11:32

One of the reasons could be that you are incrementing the variable "i" inside the if block....I think it should be outside...hope this helps...:)

link

answered 18 Feb '15, 23:25

kunal361's gravatar image

4★kunal361
6.0k133272
accept rate: 21%

i++ is not inside the if block, it's outside of it

(19 Feb '15, 11:28) arun_as1★

oh yaa...sorry...my bad!!!

(19 Feb '15, 21:01) kunal3614★

Other two reasons are:
1) Not printing new line
2) Additional comma in the scanf's format specifier

link

answered 18 Feb '15, 23:36

rahul_nexus's gravatar image

2★rahul_nexus
7741923
accept rate: 13%

no new line required!!!

(19 Feb '15, 11:10) kunal3614★

maybe the data type n is of unsigned long you are using and in the loop you are incrementing i and checking it with n which might be upto 10^7 . so try changing the data type of i and count from int to unsigned long and

link

answered 19 Feb '15, 11:23

suraj_ch77's gravatar image

5★suraj_ch77
513
accept rate: 33%

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:

×3,828
×1,919

question asked: 18 Feb '15, 23:15

question was seen: 719 times

last updated: 19 Feb '15, 21:01