+ 11

Help Me! Why is this stopping ?

I recently was trying to create a solver for Agus Mei's Quiz No. 3, 100. So, I created this: #include<bits/stdc++.h> using namespace std; bool nozero(int no) { int d; while(no!=0) { d=no%10; if(d==0) return false; no/=10; } return true; } void f() { int x,y,z,a; for(int i=0;i<1000;i++) { x=1+(rand()%98); if(nozero(x)) break; else continue; } a=100-x; for(int i=0;i<1000;i++) { for(int j=0;j<100;j++) { y=rand(); if(nozero(y)) break; else continue; } for(int k=0;k<100;k++) { z=rand(); if(nozero(z)) break; else continue; } if((y/z==a&&y%z==0)) break; else continue; } cout<<x<<"+"<<y<<"/"<<z<<endl; } int main(void) { srand(time(0)); for(int i=0;i<5;i++) f(); } But on running, it on my PC, I encounter the following problem, that it sometimes stop working, and prints only few of the correct answers. (Sometimes all, sometimes none.) So, how to repair the program? I guess I should make the program save the results in a file, so that whenever it works, the correct ones are saved. This will help me get many results // Atleast after 10-15 runs... Edit : Now, even the answers are coming wrong. Why?

26th Apr 2017, 12:16 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
6 ответов
+ 4
1. Your code crashes because you attempt to divide by 0. z = rand() can return 0, then your nozero function simply returns true so further down the line y / z will cause a divide by 0. 2. You're not checking if the digits in the equation are unique, unless you don't want to of course :P If checking for uniqueness then using rand will take forever to get even 1 answer, unless you're extremely lucky. 3. After 1000 attempts your for loop quits and outputs its last data, thats why they are all wrong Here is my implementation in C++, this does not work on the playground because std::to_string is bugged. (works on code blocks at least) #include <iostream> #include <cmath> #include <vector> bool areUnique(std::string n) { int arr[] = {0,1,1,1,1,1,1,1,1,1}; for(const auto &i: n) { if(isdigit(i)) { if(arr[i - '0']) --arr[i - '0']; else return false; } } //Also check if all digits are used, except 0 for(int i = 1; i < 10; ++i) if(arr[i]) return false; return true; } std::vector<int> getDivisors(int n) { std::vector<int> divisors; int sq = sqrt(n); for(int i = 1; i <= sq; ++i) { if(n % i == 0) { divisors.push_back(i); divisors.push_back(n / i); } } return divisors; } int main() { const int TARGET = 100; for(int y = 1; y < 100000; ++y) { for(const auto& z: getDivisors(y)) { int x = TARGET - y / z; if(x > 0) { if(areUnique(std::to_string(x) + std::to_string(y) + std::to_string(z))) { std::cout << x << " + " << y << " / " << z << std::endl; } } } } }
26th Apr 2017, 5:56 PM
Dennis
Dennis - avatar
+ 11
After some observation, I was able to see that y/z is never returning to be == a, perhaps because the loop ends by that time. I'll now try using an infinite loop, which will be slower, but may return an answer... Edit : That didn't work. Not at all. No answer at all. ;..(
26th Apr 2017, 12:33 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 10
@Dennis 1) I was Stupid to not make an if for the no==0... 2) I assumed rand() which would be run 1000 times may return me unique data, but I forgot that it is a pseudo-random generator. - I'll redesign it with a randon_distribution_engine... Thanks a lot! You're Great!...
29th Apr 2017, 4:47 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 9
Trying it, but unable to find any problem...
26th Apr 2017, 12:18 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 9
These are some run results... 6+13673/29839 21+23174/5618 76+23666/7759 51+26978/32728 47+15818/19776 74+21382/3245 47+21439/558 32+6359/13973 3+28541/1724 57+3669/25733 18+5681/6152 59+28338/9311 81+21829/1858 25+26926/23925 61+31658/4694 94+17418/17871 87+31395/22289 64+17493/25448 Why are 'all' of them wrong?
26th Apr 2017, 12:20 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 4
have you tried debugging it?
26th Apr 2017, 12:13 PM
jay
jay - avatar