Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
How can I count the even and the odd integers in a given number in C++?
For example: The user inputs: 32478
output: 3 even numbers and 2 odd numbers.
The basic algorithm is:
Take the number modulo 2 (num % 2). If the result is 1 then the number is odd; increment the odd counter. If not then it's even; increment the even counter.
Divide the number by 10, dropping the remainder. (num /= 10)
Go back to step 1 if the number isn't zero.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
For a study I am doing everyone gets assigned a unique number.
All unique numbers are divisble by 11 (this is done because it makes sequential numbers quite different from each other).
I would ideally like a regex which I can use to check that the number entered in the study_id field is an acceptable value - e.g divisible by 11.
I will have leading zeroes to a maximum of 5 digits
So:
00011 - Acceptable
00012 - Not Acceptable
13211 - Acceptable
13221 - Not Acceptable
Any suggestions gratefully received
This isn't possible because there are no textual similarities between numbers that are divisible by 11. Regex is used for text matching.
For example 000165 is divisible by 11 as is 00011.
The best way to validate the number is to divide it by 11 and see if there is any remainder. So in Excel you'd do this:
=IF(MOD(165, 11) = 0, "VALID", "INVALID")
Or C# you'd do something like this
bool isValid = 165 % 11 == 0;
(Disclaimer I'm not familiar with ODK so I can't provide a suitable sample; I've just guessed on the best language to write in)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
I need a really short way to extract the last 4 digits of a hex number. So an input of 0x2479c should throw an output of 0x479c. I want to avoid converting and reconverting to binary.
Modulo division, which would generally work for decimal numbers, does not work in this case.
0x2479c modulo 0xffff = 0x479e
which isn't correct. I'm trying to achieve this is c/c++.
You should use a mask and then a byte wise 'and' with an other value. In your case :
0x2479c & 0x0ffff
Either use a mask
0x2479c & 0x0ffff
or the modulo operator
0x2479c % (0x10000);
You were off by one in the operand of modulo.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
As a part of a recent topcoder SRM problem we had to compute number of buses "B" required to carry "N" people given that each bus has "S" seats.
What is the smartest way to compute this in C++?
The obvious way is to do:
if(N%S==0){B=N/S;}
else{ B=N/S + 1;}
^ ALL VARIABLES ARE INTEGERS, N AND S ASSIGNED APPROPRIATE VALUES
However I cant understand the logic behind the following code which is one particular topcoder user's solution which I was checking out;
B = (N + (S-1))/S;
How does this work?
The code
B = (N + (S-1))/S;
is a common rounding trick. We know that in integer division, the remainder is cut-off, essentially what floor does. In this case, we enforce a ceil operation by adding S-1 first.
This is similiar to the common way of rounding floating point numbers:
n = floor(n + 0.5);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
let's go straight to the facts.
I am studying for my own interest an eventual prediction algorithm for some unexistent lottery.
Let's say they roll out 3 numbers every day.
And those numbers are in range from 0 to 50.
I am asking, what would be the best approach to try to predict next 3 numbers knowing
all previous historic ones?
1. What i have
I have a list of 3 numbers from a range of {0,50} (integers)
<x0,y0,z0>
<x1,y1,z1>
<x2,y2,z2>
<x3,y3,z3>
Those numbers represent winning values of lottery.
2. What i need
I need to predict next 3 lottery numbers(possible WINNERS) by taking previous numbers into consideration
The order of the predicted numbers doesn't mater. It might be 1,2,3 or 3,2,1.
3. Question
Which approach / algorithm should i choose and why?
Super thanks for any help!
If the numbers you roll out are random, there is no way to make a prediction, as the next numbers are not linked in any way to the previous one. The most you can do is a guessing algorithm.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I want to write a program that calculates if a number is a multiple of some other numbers.
Lets say that i want to check if 25 can be given from a function like 2a+3b+4c=25.Is there any algorithm that can find a b c in order to check if abc are integers?
For generating all possibilities, a Brute force algorithm will suffice:
Loop over all possible values for a and subtract the sum 2a from 25. Nest similar loops for b using the remainder. If the remainder after subtracting 3b is a multiple of 4, then loop over all possible values of c and output a, b, and c as a combination.