Algorithm
problem Link : https://onlinejudge.org/index.php?option=onlinejudge&Itemid=8&page=show_problem&problem=1621
All of you know about LCM (Least Common Multiple). For example LCM of 4 and 6 is 12. LCM can also be defined for more than 2 integers. LCM of 2, 3, 5 is 30. In the same way we can define LCM of first N integers. The LCM of first 6 numbers is 60. As you will see LCM will increase rapidly with N. So we are not interested in the exact value of the LCM but we want to know the last nonzero digit of that. And you have to find that effeciently. Input Each line contains one nonzero positive integer which is not greater than 1000000. Last line will contain zero indicating the end of input. This line should not be processed. You will need to process maximum 1000 lines of input. Output For each line of input, print in a line the last nonzero digit of LCM of first 1 to N integers.
Sample Input 3 5 10 0
Sample Output 6 6 2
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,v;
while(scanf("%d",&n),n){
int cur = 0,best = 0;
for(int i=0;i < n;i++){
cin >> v;
cur = max(0,cur+v);
best = max(best,cur);
}
if(best) printf("The maximum winning streak is %d.\n",best);
else printf("Losing streak.\n");
}
}
Copy The Code &
Try With Live Editor
Input
5
10
0
Output
6
2
Demonstration
UVA Online Judge solution - 10684-The jackpot - UVA Online Judge solution in C,C++,java