In: Computer Science
There are two lights that blink at regular intervals. When each one blinks, it turns on and then immediately back off; they don’t toggle. They are both off at time t=0t=0. The first one blinks at t=p,2p,3p,…t=p,2p,3p,… seconds; the second one blinks at t=q,2q,3q,…t=q,2q,3q,… seconds. Once they start, they both keep blinking forever. It is very exciting to see them blink at the same time (on the same second). But your patience is going to run out eventually, in ss seconds. Will they blink at same time between t=1t=1 and t=st=s (inclusive)? Write a program that can answer this question, quick, before they start blinking again!
Output:
Writable Program:
#include<iostream>
#include<vector>
using namespace std;
bool checkLight(int p,int q,float s){
vector<int> A;
vector<int> B;
int store;
int n;
int i=1;
do{
n = i*p;
A.push_back(n);
i++;
}while(n<=s);
A.pop_back();
int j=1;
do{
n = j*q;
B.push_back(n);
j++;
}while(n<=s);
B.pop_back();
for (int i=0; i<A.size(); i++)
{
// Check any element is equal
int j;
for (j=0; j<B.size(); j++) {
if (A[i] == B[j]) {
cout<<"Yes";
return 1;
}
}
}
return 0;
}
int main(){
int p,q,s;
cin>>p>>q>>s;
if(checkLight(p,q,s)){
return 0;
}
else{
cout<<"no"<<endl;
}
}
Hope I answered the question.
If you have any doubts, or queries, feel free to ask
I'll respond to you as soon as I can.
Have a nice day