c++ - How to optimize this code to remove time exceeding error on hacker Earth? -
input - 4 integers- t=test cases, , b= positive numbers, n output - print nth number divisible or b. example- = 2, b= 3, n=10 numbers divisble 2 or 3= 2,3,4,6,8,9,10,12,14,15 10th number divisible 2 or 3 = 15. output = 15
#include<iostream> #include<stack> #include<stdio.h> using namespace std; int main() { int t; stack<int> s; scanf("%d",&t); for(int t=1;t<=t;t++) { int a; scanf("%d",&a); int b; scanf("%d",&b); int n; scanf("%d",&n); for(int i=0;s.size()<=n;i++) { if(i%a == 0 || i%b == 0) s.push(i); } cout<<s.top(); } return 0; }
Comments
Post a Comment