Back to General discussions forum
The system rejects my answer for Problem 37, although it is definitely correct, I can prove it.
input data: 3000000 7 96
My program returned the answer 40901, while the system insists on the answer 40902. This code is my apology:
public class MyClass {
public static void main(String[] args) {
int m = 0;
long p = 3000000L;
System.out.print("| Month | Balance |\n|-------|---------|");
do {
++m;
p *= 1207L;
p /= 1200L;
p -= 40901L;
System.out.print(String.format("\n| %5d | %7d |", m, p));
} while (p > 0);
}
}
By the way, if you are converting from integers to real numbers and vice versa, then you are doing it wrong. You can get the idea from my code. If you want, I can send my program by e-mail or publish it here.
With deepest respect and hope for fruitful cooperation, Kulbas Oglydov.
"By the way, if you are converting from integers to real numbers and vice versa, then you are doing it wrong."
Respectfully, I disagree. Your line p /= 1200L
is overly generous to the borrower as it always rounds down.
Of course. Does the condition of the problem say something else?
In your code, the function f
returns f(50, 12, 50) = 1
but I would argue that it takes more than a month to repay
the money as you still have 0.5
left to pay back after the first month.
Thank you, I understand. However, this is not clearly mentioned in the problem statement. In any case, this does not mean that real numbers cannot be dispensed with. I solved the problem without them. A similar situation arose in the "Duel Chances" problem. I still try not to use real numbers in integer calculations unless absolutely necessary.
public class MyClass {
public static void main(String[] args) {
final Scanner s = new Scanner(System.in);
int m = 0;
long p = 3000000L;
System.out.print("| Month | Balance |\n|-------|---------|");
do {
++m;
p *= 1207L;
p += 600L;
p /= 1200L;
p -= 40901L;
System.out.print(String.format("\n| %5d | %7d |", m, p));
} while (p > 0);
}
}
Kulbas, Hi!
Let's see.
However, this is not clearly mentioned in the problem statement
what is not mentioned, or is not clear enough? feel free to sugest what exactly should be rephrased, as I'm slowpoke and not always easily follow other's thoughts :)
By the way, if you are converting from integers to real numbers and vice versa, then you are doing it wrong
This is interesting but completely vague. What is done wrong and why? Feel free to suggest any corrections but please express them in some more clear way :)
As to your code, by the way, it looks strange to instantiate scanner variable but never read from it.
And how you choose, whether to calculate in integer dollars or integer cents?
I still try not to use real numbers in integer calculations unless absolutely necessary.
While for this problem there could be some motivation to regard it as "integer calculation", for the "Duel chances"
it is queer. Money are counted in integers often enough, that's true. However probabilities are not integers,
as a matter of fact they are all in range 0..1
. The matter that specific task asks to convert them to percents
and round to integers doesn't mean the task itself is about "integer calculations".
If you are interested in tasks where integers are unavoidable, look at brezenham line and circle algorithms somewhere below. They arose from using in low-end hardware and so motivation for integers there is justified.
Real numbers in a computer are not as precise as they are in pure mathematics. The computer may have rounding errors. In fact, I strive to solve problems in rational numbers, not integers. I could even make an appropriate class, but for such simple tasks it is usually redundant.
The fact that the probability of an event is between 0 and 1 does not matter in this case. I just express these numbers in a different way. For example, in hundredths, as in that case.
Such practices are not so rare. For example, in computer games, you may not want to calculate trigonometric functions using mathematic library. It is better to store a table of 360 values (or 256, or 65536, it's implementation details, of course). And these values will be integer. For example, one thousandth of a unit (or 1/255, or 1/65535), if such accuracy is sufficient. In other words, sin(0) = 0, sin(30) = 500 instead of 0.5, sin(60) = 866 instead of 0.866, sin(90) = 1000 instead of 1.0, and so on. I heard that similar techniques are used in professional neural networks implementations. However, in this matter I am not competent enough.
It may seem strange to the younger generation, but I started out programming 8-bit processors that couldn't even multiply. Therefore, when I wanted to multiply a number by 5, I doubled it twice, then added the original one. Of course, now I don’t do that, but such a search for the way with minimal effort (for processor, not brain) is already working on a subconscious level.
For example, when I calculate the roots of a quadratic equation, I do not divide them by (a * 2). I calculate (0.5 / a) and then multiply both roots by that number. This is mathematically equivalent, but I am replacing one multiplication and two divisions with one division and two multiplications. Computers multiply much faster and more accurately than they divide.
You can see how I solved the "Whisky Blending" if you're curious. It looks crazy, but works.
I love strange techniques from an era when people could think, and not just include libraries. Recently I saw a headline on YouTube: How to make Tetris on Unity. This was the last straw after which I stopped going to YouTube. Other social networks have never been used by me at all.
By the way, I just had an idea! Why not make a Tetris-themed problem? For example, removing filled lines. Although this is probably too easy, but that's the first thing that came to mind.
It may seem strange to the younger generation, but I started out programming 8-bit processors that couldn't even multiply
Then definitely check our problems on Intel-4004 assembly - hopefully you'll find them curious :)
from an era when people could think, and not just include libraries
To that I heartily agree. I guess you remember ZX Spectrum era with its games - often impressing "how this could be put into such small amount of memory".
However I prefer that general problems don't have such limitations. Also practice with real values is important for newcomers (not for you or other gurus) - you may notice how often people fail to submit "Rounding" problem for example.
Still modern "libraries" and "library methods" are spoiling education. For example "reverse/rotate string" problems nowadays are done not in-place so they lose any sense. We even have dedicated version of problem to implement it in BASIC :)
As about Tetris-like games - also thought of this but had no clear idea on exact problem. Though it may be straightforward - to implement Tetris in Lua and make challenge which solution score more points. Just not sure how popular lua-based problems are yet among our colleagues :)