Back to Problem Solutions forum
So I'm not sure what I'm doing wrong... When I printf I seem to be doing my additions and multiplications correctly. I also retrieve the number with which I do the modulo successfully.
Here's my code :
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int c; double a; char b='0';
scanf("%lf\n",&a);
do{
if(b=='+') a=a+c;
else if(b=='*') a=a*c;
scanf("%c %d\n",&b,&c);
}while(b!='%');
a=fmod(a,c);
k=floor(a+0.5);
printf("%d",k);
return 0;
}
I had to use a double type because other types do not support such big numbers, I got negative numbers with long.
There have been several posts in the forum about this very problem, if you search back a bit. However, the point of this problem is to understand how to use modulo to keep a number within certain "bounds", called Z/NZ. When you take a number modulo n, the number you get back is always guaranteed to be less than n. That said, you can use modulo any time throughout your algorithm to keep the number within bounds. You can set up a type of checking system to decide when the number is large enough to perform modulo, or you could just perform the modulo at each step without checking. The result will be the same. You will probably find that the best way to do this is to read all the lines in before starting your algorithm.
I hope I have been of some help. You will see these types of situations a lot as a programmer, so it is a must that you learn to understand it. You can read about modular arithmetic and Z/NZ on Code Abbey and Wikipedia.
-- Christopher P. Matthews
One more thing: I've looked at some of your solutions, and I would suggest writing small functions to accomplish specific tasks and building your program that way, rather than doing everything in main. For one, it is very hard for another person reading your code to understand what is going on. For two, when you use functions, it adds a logical flow to the program that makes it much easier to deduce what is going on. Also, use comments!
-- Christopher P. Matthews
Thank you very much for your reply. I thought about using modulo for every operation, the problem is that I would have to browse through all values first, but then I have no idea how to "reset" my scanf to the beginning. Well, as I you would have guessed (even more so by checking my 'solutions', haha...) I'm quite the beginner at programming. Anyhow, I'll look into it.
Thanks again for your time.
You're quite welcome. See, the problem is not "reseting" scanf; rather, you want to read in all lines of stdin, and store then in an array, rather than read through all of them every iteration. It will save you a lot of time and computing power.
I'm glad I could be of help.
-- Christopher P. Matthews
EN: I wonder, who implemented an algorithm, that solves this problem with these initial data.
RU: Мне интересно у кого реализован алгоритм, который решит Эту задачу с такими исходными данными :)
(((176896932454305448757139039745644361794723334152282364694666
* 23)
+ 8)
% 1054)
666?
One more:
1792475871468546560184376981937546814325108469057381567414320846758435690512476571046589784158076465784378765714058178432758168457873779143
%1054
No problem, it's 499.
I posted my updated solution which deals with an arbitrarily large starting value without resorting to a BigInteger library.
MontBlanc, googling "singly linked list in C language".
Guy Gervais, you are Right :) 666 and 499
Sad thing - but people coding in Python often even do not realize it is a hard problem for they have long arithmetics just out of the box. So OldSchool you should not take them into account :)
If I'm not mistaken, OldSchool wrote the beginning of a "BigInteger" class using strings in VB6. It certainly isn't the easiest way to solve this problem, but it's pretty impressive in it's own way.
>but it's pretty impressive in it's own way.
Oh yes - I've done this myself few times in my life and I remember that especially implementing division operation is quite a boring thing - so I also was impressed by OldSchool's solution :)
:)