zoukankan      html  css  js  c++  java
  • A. Transformation: from A to B

    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output
     

    Vasily has a number a, which he wants to turn into a number b. For this purpose, he can do two types of operations:

    • multiply the current number by 2 (that is, replace the number x by x);
    • append the digit 1 to the right of current number (that is, replace the number x by 10·x + 1).

    You need to help Vasily to transform the number a into the number b using only the operations described above, or find that it is impossible.

    Note that in this task you are not required to minimize the number of operations. It suffices to find any way to transform a into b.

    Input

    The first line contains two positive integers a and b (1 ≤ a < b ≤ 109) — the number which Vasily has and the number he wants to have.

    Output

    If there is no way to get b from a, print "NO" (without quotes).

    Otherwise print three lines. On the first line print "YES" (without quotes). The second line should contain single integer k — the length of the transformation sequence. On the third line print the sequence of transformations x1, x2, ..., xk, where:

    • x1 should be equal to a,
    • xk should be equal to b,
    • xi should be obtained from xi - 1 using any of two described operations (1 < i ≤ k).

    If there are multiple answers, print any of them.

    Examples
    input
    2 162
    output
    YES
    5
    2 4 8 81 162
    input
    4 42
    output
    NO
    input
    100 40021
    output
    YES
    5
    100 200 2001 4002 40021

    题意:

    有两种变换方式:

    (1)x*10+1

    (2)x*2

    问a能否经过两种变换转换成b

    如果b是奇数,则必是由(1)方式转换而来,若为偶数则必是(2)转换而来。

    附AC代码:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 
     4 int c[100010];
     5 
     6 int main(){
     7     int a,b;
     8     cin>>a>>b;
     9     int ans=1;
    10     c[0]=b;
    11     int flag=1;
    12     while(a<b){
    13         if((b-1)%10==0){
    14             b=(b-1)/10;
    15             c[ans++]=b;
    16         }
    17         else if(b%2==0){
    18             b/=2;
    19             c[ans++]=b;
    20         }
    21         else{
    22             flag=0;
    23             break;
    24         }
    25     }
    26     if(flag==0){
    27         cout<<"NO"<<endl;
    28     }
    29     else if(a==b){
    30         cout<<"YES"<<endl;
    31         cout<<ans<<endl;
    32         for(int i=ans-1;i>=0;i--){
    33             cout<<c[i]<<" ";
    34         }
    35     }
    36     else{
    37         cout<<"NO"<<endl;
    38     }
    39     return 0;
    40 }
  • 相关阅读:
    JSP的动态Include的静态Include
    JAVA观察者模式
    JAVA单例模式
    【转】 linux 安装nginx及编译参数详解
    【转】Linux下nginx配置https协议访问的方法
    【转】./configure && make && make install详解
    【转】linux下如何查看某个软件 是否安装?安装路径在哪
    【转发】查看Linux版本系统信息方法汇总
    【转发】CentOS 7 巨大变动之 systemd 取代 SysV的Init
    【转发】centos 7安装完后出现please make your choice from '1' ......
  • 原文地址:https://www.cnblogs.com/Kiven5197/p/6033821.html
Copyright © 2011-2022 走看看