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 }
  • 相关阅读:
    mysql关联查询
    文本框,下拉框,单选框只读状态属性
    sql索引实例
    sql视图实例
    SQL触发器实例
    存储过程实例
    sql 、linq、lambda 查询语句的区别
    LINQ中的一些查询语句格式
    面试宝典
    SQL常用语句
  • 原文地址:https://www.cnblogs.com/Kiven5197/p/6033821.html
Copyright © 2011-2022 走看看