zoukankan      html  css  js  c++  java
  • 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 }
    View Code

    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 }
    View Code
  • 相关阅读:
    在Android中,使用Kotlin的 API请求简易方法
    Android开发者的Kotlin:书
    用Kotlin开发Android应用(IV):定制视图和Android扩展
    用Kotlin开发Android应用(III):扩展函数和默认值
    zookeeper应用
    BigDecimal的setScale()方法无效(坑)
    Linux命令详解之—less命令
    jdk10 var定义变量的由来
    Mysql DataPacketTooBigException异常处理
    JDK自带的监控工具方法
  • 原文地址:https://www.cnblogs.com/NWUACM/p/6445271.html
Copyright © 2011-2022 走看看