Lucky Number
Posted NWU_ACM
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Lucky Number相关的知识,希望对你有一定的参考价值。
C++版本:
1 #include <bits/stdc++.h> 2 #define _xx ios_base::sync_with_stdio(0);cin.tie(0); 3 using namespace std; 4 typedef long long ll; 5 ll a, b; 6 bool cmp(const string& s1, const string& s2) //s1 <= s2 ? true : false 7 { 8 if(s1.size() == s2.size()) 9 { 10 for(int i = 0; i < s1.size(); i++) 11 if(s1[i] != s2[i]) return s1[i] < s2[i]; 12 return true; 13 } 14 else s1.size() < s2.size(); 15 } 16 bool isok(const string& s) 17 { 18 ll x = 0, y = 0; 19 for(int i = 0; i < s.size(); i++) 20 { 21 x = (x*10 + s[i] - \'0\')%a; 22 y = (y*10 + s[i] - \'0\')%b; 23 } 24 return x != 0 && y != 0; 25 } 26 void addone(string& s) 27 { 28 int i; 29 for(i = s.size() - 1; i >= 0; i--) 30 if(s[i] != \'9\') break; 31 else s[i] = \'0\'; 32 s[i]++; 33 } 34 int main() 35 {_xx 36 // freopen("in.txt", "r", stdin); 37 // freopen("out.txt", "w", stdout); 38 string s1, s2, ans; 39 int t = 0; 40 while(cin >> a >> b >> s1 >> s2) 41 { 42 if(a == 1 || b == 1) 43 { 44 cout << -1 << endl; 45 continue; 46 } 47 s2.insert(0, "0"); 48 while(s1.size() < s2.size()) s1.insert(0, "0"); 49 ans = "-1"; 50 while(cmp(s1, s2)) 51 { 52 if(isok(s1)) 53 { 54 ans = s1; 55 break; 56 } 57 else addone(s1); 58 } 59 ans.erase(0, ans.find_first_not_of("0")); 60 cout << ans << endl; 61 } 62 return 0; 63 }
JAVA版本:
1 import java.math.BigInteger; 2 import java.util.Scanner; 3 4 public class Main { 5 6 public static void main(String[] args) { 7 BigInteger a, b, l, r; 8 9 Scanner input = new Scanner(System.in); 10 11 while(input.hasNext()) { 12 a = input.nextBigInteger(); 13 b = input.nextBigInteger(); 14 l = input.nextBigInteger(); 15 r = input.nextBigInteger(); 16 if (a.equals(BigInteger.ONE) || b.equals(BigInteger.ONE)) { 17 System.out.println(-1); 18 } 19 else { 20 while(l.compareTo(r) <= 0) { 21 if(l.mod(a) != BigInteger.ZERO && l.mod(b) != BigInteger.ZERO) { 22 break; 23 } 24 l = l.add(BigInteger.ONE); 25 } 26 if(l.compareTo(r) <= 0) { 27 System.out.println(l); 28 } 29 else { 30 System.out.println(-1); 31 } 32 } 33 } 34 35 input.close(); 36 } 37 38 }
以上是关于Lucky Number的主要内容,如果未能解决你的问题,请参考以下文章
Symfony2:找不到“GET /lucky/number”的路由