• Divisibility by Eight (数学)


    Divisibility by Eight
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn't contain leading zeroes.

    Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes and is divisible by 8. After the removing, it is forbidden to rearrange the digits.

    If a solution exists, you should print it.

    Input

    The single line of the input contains a non-negative integer n. The representation of number n doesn't contain any leading zeroes and its length doesn't exceed 100 digits.

    Output

    Print "NO" (without quotes), if there is no such way to remove some digits from number n.

    Otherwise, print "YES" in the first line and the resulting number after removing digits from number n in the second line. The printed number must be divisible by 8.

    If there are multiple possible answers, you may print any of them.

    Sample test(s)
    input
    3454
    output
    YES
    344
    input
    10
    output
    YES
    0
    input
    111111
    output
    NO

    能被8整除的特性:最后三位可以被8整除。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <string>
     4 #include <queue>
     5 #include <vector>
     6 #include <map>
     7 #include <algorithm>
     8 #include <cstring>
     9 #include <cctype>
    10 #include <cstdlib>
    11 #include <cmath>
    12 #include <ctime>
    13 #include <climits>
    14 using    namespace    std;
    15 
    16 const    int    SIZE = 105;
    17 char        S[SIZE];
    18 bool        VIS[SIZE],FLAG;
    19 vector<char>    ANS;
    20 
    21 void    dfs(int start,int sum,int count);
    22 int    main(void)
    23 {
    24     cin >> S;
    25     dfs(0,0,0);
    26     if(!FLAG)
    27         puts("NO");
    28 
    29     return    0;
    30 }
    31 
    32 void    dfs(int start,int sum,int count)
    33 {
    34     if(FLAG || count > 3)
    35         return    ;
    36     for(int i = start;S[i];i ++)
    37     {
    38         if(FLAG)
    39             return    ;
    40         if(!VIS[i])
    41         {
    42             VIS[i] = true;
    43             int    back = sum;
    44             ANS.push_back(S[i]);
    45             sum = ANS[ANS.size() - 1] - '0';
    46             if(ANS.size() >= 2)
    47                 sum += (ANS[ANS.size() - 2] - '0') * 10;
    48             if(ANS.size() >= 3)
    49                 sum += (ANS[ANS.size() - 3] - '0') * 100;
    50             if(sum % 8 == 0)
    51             {
    52                 cout << "YES" << endl;
    53                 for(int i = 0;i < ANS.size();i ++)
    54                     cout << ANS[i];
    55                 cout << endl;
    56                 FLAG = true;
    57                 return    ;
    58             }
    59 
    60             dfs(i + 1,sum,count + 1);
    61             VIS[i] = false;
    62             sum = back;
    63             ANS.pop_back();
    64         }
    65     }
    66 }
  • 相关阅读:
    Selenium2Library+ride学习笔记
    windbg 调试技巧
    LINUX常用命令--重定向、管道篇(四)
    Linux文件系统与结构
    windbg命令学习4
    windbg命令学习3
    windbg命令学习2
    MySQL常用操作命令
    Httpwatch 工具介绍
    windows平台上用python 远程线程注入,执行shellcode
  • 原文地址:https://www.cnblogs.com/xz816111/p/4572785.html
Copyright © 2020-2023  润新知