Note that there are some explanatory texts on larger screens.

plurals
  1. POCan I make this C++ code faster without making it much more complex?
    primarykey
    data
    text
    <p>here's a problem I've solved from a programming problem website(codechef.com in case anyone doesn't want to see this solution before trying themselves). This solved the problem in about 5.43 seconds with the test data, others have solved this same problem with the same test data in 0.14 seconds but with much more complex code. Can anyone point out specific areas of my code where I am losing performance? I'm still learning C++ so I know there are a million ways I could solve this problem, but I'd like to know if I can improve my own solution with some subtle changes rather than rewrite the whole thing. Or if there are any relatively simple solutions which are comparable in length but would perform better than mine I'd be interested to see them also.</p> <p>Please keep in mind I'm learning C++ so my goal here is to improve the code I understand, not just to be given a perfect solution.</p> <p>Thanks</p> <h1>Problem:</h1> <p>The purpose of this problem is to verify whether the method you are using to read input data is sufficiently fast to handle problems branded with the enormous Input/Output warning. You are expected to be able to process at least 2.5MB of input data per second at runtime. Time limit to process the test data is 8 seconds.</p> <p>The input begins with two positive integers n k (n, k&lt;=10^7). The next n lines of input contain one positive integer ti, not greater than 10^9, each. Output</p> <p>Write a single integer to output, denoting how many integers ti are divisible by k. Example</p> <h1>Input:</h1> <p>7 3<br> 1<br> 51<br> 966369<br> 7<br> 9<br> 999996<br> 11 </p> <h1>Output:</h1> <p>4 </p> <h1>Solution:</h1> <pre><code>#include &lt;iostream&gt; #include &lt;stdio.h&gt; using namespace std; int main(){ //n is number of integers to perform calculation on //k is the divisor //inputnum is the number to be divided by k //total is the total number of inputnums divisible by k int n,k,inputnum,total; //initialize total to zero total=0; //read in n and k from stdin scanf("%i%i",&amp;n,&amp;k); //loop n times and if k divides into n, increment total for (n; n&gt;0; n--) { scanf("%i",&amp;inputnum); if(inputnum % k==0) total += 1; } //output value of total printf("%i",total); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload