C++: Decimal to Roman Numeral conversion [closed] - c++

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I know how to do this and have made a program that does so in the past, however it never incorporated the special cases of roman numerals (e.g. IV = 4). Integrating that into a functional program in a minimalistic way is my main problem. Is there a good way to do this?

A very simple solution, in pseudo-code, could look something like this:
value = get_the_value_to_convert();
divider = 1000; // Start at 1000 (M)
while (value > 0)
{
count = value / divider;
value = value % divider;
for (i = 0; i < count; ++i)
print(roman_numeral_from_number(divider));
divider /= 10;
}
The roman_numeral_from_numbe function does a one-to-one mapping between a number and a roman numeral.
For e.g. 5432 as input it would print MMMMMCCCCXXXII.
For better results, divide the divider variable with 2 and 5 every second time.
For even better results, add check for certain numbers, like 90 being equal to "XC".
Note that even with the modifications, the above algorithm can't handle very large numbers, but on the other hand Wikipedia states that historically it was only used for small numbers anyway.

Related

What does cost mean in context of time complexity? [closed]

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 2 years ago.
Improve this question
Consider the following pseudo code:
Pseudocode:
list_Sum(A,n){//A->array and n->number of elements in the array
sum =0 // cost=1 no of times=1
for i=0 to n-1 // cost=2 no of times=n+1 (+1 for the end false condition)
sum = sum + A[i] // cost=2 no of times=n
return sum // cost=1 no of times=1
}
What does cost mean in context of time complexity?
Nothing, actually. Theoretically, this would be the number of instructions required on a fictional architecture made up by the author. Practically, it doesn't map to any existing, relevant architecture. No real architecture has exactly these costs.
The only relevant part is the times 1 or times n part, as that is applicable regardless of architecture.
There is also vectorization and super-scalarity, which easily slice the cost of e.g. the loop by factor 15-50 (so imagine that as a "real" cost of 0.02 for the body of the loop), but even then it stays times n.

Confusion over how to code a for loop including a for all addition in c++ [closed]

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 3 years ago.
Improve this question
I am developing the optimization of a bus schedule problem in c++ and need to code each of the constraints, however I am struggling to know how to code a particular kind of constraint.
The sets i,j,k and h all range from 1 to I,J,K and H respectively. My confusion lies with how to code the left hand side of the constraint. I am trying to use for loops however I am getting confused as to how to implement the for all i,h part.
My question is how to code the left hand side of the equation, given that X is a four dimensional array. I so far have two for loops, looping from j,k = 0 to j,k < J,K. How would I include for all i,h
Any help would be greatly appreciated =)
In pseudo code that would be
for all i
for all h
sum = 0
for all j
for all k
sum += X[i,h,j,k]
if (sum != 1) -> condition not satisfied
You basically have for each combination of i and h a seperate condition that has to be satisfied. For each of those conditions you have to sum over j and k.

Write a Regular Expression which identifies numbers divisible by 11 [closed]

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)

Most frequent value in dataset (with variation) [closed]

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 7 years ago.
Improve this question
This is a part programming, part statistical math question.
I have a dataset where I want to get the most frequent number (mode), the problem is that I am dealing with values with slight variation.
So normally {1,2,50,50,90} the most frequent number would be 50
But in my case the numbers look like this:
{1,2,49,50,51,90} but the result is still 50
So my question is how can I efficiently calculate this number and is there a statistical term for this number?
Some pseudo code:
Float items.val[] = {1,2,49,50,51,90};
Float threshold = 4;
For (item in items) {
For (subitem in items){
Float dist=Distance(time,subitem)
If (dist < threshold){
item.dist += dist
}
}
}
Output=Sort(item.dist)[0]
There are various ways to go about this.
(1) the most careful, exact way is to assume a probabilistic model for the observed values, and look for the mode (as the expected value or most probable or some other criterion) of the inferred values. I am going to guess this is far too much work in this case, although given unlimited time I would certainly want to approach it that way.
(2) construct a histogram, and look for the bin which has the greatest density (with density = (#items in bin)/(width of bin)). This doesn't necessarily yield a single value.
(3) fit a parametric distribution to the observed values, and report the mode of the fitted distribution.
You might get more traction for this question at stats.stackexchange.com. Good luck and have fun.
EDIT: After looking at your example code, I see it is not too different from (2) above. It seems like a reasonable and workable approach.

A computing trick to calculate for e.g number of boxes required to place N objects given each box can hold M objects? [closed]

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);