Two Buttons

Two Buttons
Time Limit:2000MS     Memory Limit:262144KB    64bit IO Format:%I64d & %I64u

DescriptionVasya hasfound a strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button, device multiplies the displayed number by two. After clicking the blue button, device subtracts one from the number on the display. If at some point the number stops being positive, the device breaks down. The display can show arbitrarily large numbers. Initially, the display shows number n.
Bob wants to get number m on the display. What minimum number of clicks he has to make in order to achieve this result?

Input
The first and the only line of the input contains two distinct integers n and m (1 ≤ n, m ≤ 104), separated by a space .

Output
Print a single number — the minimum number of times one needs to push the button required to get the number m out of number n.

Sample Input
Input
4 6
Output
2
Input
10 1
Output
9

题目简化大意:给你两个数N,M,对于N有两种操作--减一和乘二
求从N变换到M的最少操作次数(每次只能进行一种操作)
例如N=4,M=6时,最少操作次数为2,即(4-1)*2=6;
N=10,M=1时,N只能进行减的操作,需要减9次


解题思路:开始的时候没有往搜索方向想,想了好久也没有
推出什么公式来,后来在稿纸左画画右画画突然灵机一动,不是
直接可以暴搜一顿吗?这不直接就出来了?然后键盘啪啪啪,代码
出来了,结果就悲剧了~~超内存有木有!没有剪枝啊!直接搜不加优化
的话会浪费系统运行时间和内存空间,所以啊,苦逼的剪枝来了~~~


///下面这段代码是没有经过剪枝的,直接把不等于M的数都放入队列中,结果嘛,可以粘到
///编译器上试试~~(可以试试1234 4321这组数据)

#include
    
    
     
     
#include
     
     
      
      
#include
      
      
       
       
using namespace std;
struct num
{
    int nn;
    int times;
};
int main()
{
    int n,m,s;
    while(~scanf("%d%d",&n,&m))
    {
        int flag=1;
        if(n>m)
            printf("%d\n",n-m);
        else
        {
            queue
       
       
        
         xt;
            num a;
            a.nn=n;
            a.times=0;
            xt.push(a);
            while(!xt.empty())
            {
                num b=xt.front();
                xt.pop();
                if(b.nn*2==m)
                {
                    flag=0;
                    s=b.times+1;
                    break;
                }
                else
                {
                    num c;
                    c.nn=b.nn*2;
                    c.times=b.times+1;
                    xt.push(c);
                }
                if(b.nn-1==m)
                {
                    flag=0;
                    s=b.times+1;
                    break;
                }
                else
                {
                    num c;
                    c.nn=b.nn-1;
                    c.times=b.times+1;
                    xt.push(c);
                }

            }
            if(!flag)
                printf("%d\n",s);
        }

    }
    return 0;
}

       
       
      
      
     
     
    
    

 

///后来剪啊剪,把等于0,大于M的,和重复的数都剪掉,才AC掉

/*#include
    
    
     
     
#include
     
     
      
      
#include
      
      
       
       
using namespace std;
struct num
{
    int nn;
    int times; //记录操作的次数
};
int ar[100000]; //记录数组,搜索过的数状态为1,未搜索的数状态为0
int main()
{
    int n,m,s;
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=0; i<100000; i++)
            ar[i]=0;    //初始化状态0
        int flag=1;
        if(n>m)
            printf("%d\n",n-m);
        else
        {
            queue
       
       
        
         xt;
            num a;
            a.nn=n;
            a.times=0;
            xt.push(a);  //把初始的n放入队列中,操作的次数为0
            ar[a.nn]=1;
            while(!xt.empty())//放到队列中的数满足n!=m
            {
                num b=xt.front();
                xt.pop();
                if(b.nn>m)  //n大于m的情况
                {
                    if((b.nn-1)==m)
                    {
                        flag=0;
                        s=b.times+1;
                        break;
                    }
                    if(b.nn-1>m&&!ar[b.nn-1])   //不需要把2*n放入队列中,即把这部分剪掉,把未搜索过的点放入队列中
                    {
                        num c;
                        c.nn=b.nn-1;
                        ar[c.nn]=1;     //把队列中的数的状态标记为1
                        c.times=b.times+1;
                        xt.push(c);
                    }
                }
                if(b.nn==0)      //把n减到0的情况剪掉
                 continue;
                if(b.nn
        
        
       
       
      
      
     
     
    
    

 

As an AI language model, I cannot provide images or submit a detailed report with visual representations of the forms mentioned in the question. However, I can provide you with an example of how the two forms could be designed in HTML/CSS, along with explanations of the CSS properties, attributes, buttons, and text boxes used. a) Registration form for new user: HTML Code: ``` <form> <label for="name">Name:</label> <input type="text" id="name" name="name" required> <label for="email">Email:</label> <input type="email" id="email" name="email" required> <label for="password">Password:</label> <input type="password" id="password" name="password" required> <button type="submit">Submit</button> </form> ``` CSS Code: ``` form { display: flex; flex-direction: column; align-items: center; justify-content: center; width: 50%; margin: 0 auto; } label { font-weight: bold; margin-top: 20px; } input[type="text"], input[type="email"], input[type="password"] { padding: 10px; border: none; border-radius: 5px; margin-bottom: 20px; width: 100%; } button[type="submit"] { background-color: #4CAF50; color: white; padding: 10px 20px; border: none; border-radius: 5px; cursor: pointer; } button[type="submit"]:hover { background-color: #3e8e41; } ``` Explanation: - The form is structured with labels and input fields for name, email, and password. - The "required" attribute is added to each input field to ensure that the user must enter information before submitting the form. - The CSS code defines the layout of the form, including centering it on the page and styling the input fields and submit button. - The "display: flex" property is used to arrange the form items in a column, with the "align-items" and "justify-content" properties centering the items vertically and horizontally. - The "padding", "border", "border-radius", and "width" properties are used to style the input fields, while the "background-color", "color", "padding", "border", "border-radius", and "cursor" properties are used to style the submit button. Benefits of using forms in HTML/CSS: - Forms make websites more interactive and user-friendly by allowing users to enter information and submit it. - Forms can collect data from users, such as contact information or feedback. - Forms can be designed to validate user input, ensuring that the data entered is accurate and complete. - Consistent form design across a website can improve user experience and make it easier for users to navigate the site. b) Student Registration form: HTML Code: ``` <form> <label for="name">Name:</label> <input type="text" id="name" name="name" required> <label for="email">Email:</label> <input type="email" id="email" name="email" required> <label for="phone">Phone:</label> <input type="tel" id="phone" name="phone" required> <label for="gender">Gender:</label> <select id="gender" name="gender"> <option value="male">Male</option> <option value="female">Female</option> <option value="other">Other</option> </select> <label for="dob">Date of Birth:</label> <input type="date" id="dob" name="dob" required> <label for="address">Address:</label> <textarea id="address" name="address"></textarea> <button type="submit">Submit</button> </form> ``` CSS Code: ``` form { display: flex; flex-direction: column; align-items: center; justify-content: center; width: 50%; margin: 0 auto; } label { font-weight: bold; margin-top: 20px; } input[type="text"], input[type="email"], input[type="tel"], input[type="date"], textarea { padding: 10px; border: none; border-radius: 5px; margin-bottom: 20px; width: 100%; } select { padding: 10px; border: none; border-radius: 5px; margin-bottom: 20px; width: 100%; } button[type="submit"] { background-color: #4CAF50; color: white; padding: 10px 20px; border: none; border-radius: 5px; cursor: pointer; } button[type="submit"]:hover { background-color: #3e8e41; } ``` Explanation: - The form is structured with labels and input fields for name, email, phone number, gender, date of birth, and address. - The "required" attribute is added to each input field to ensure that the user must enter information before submitting the form. - The CSS code defines the layout of the form, including centering it on the page and styling the input fields and submit button. - The "display: flex" property is used to arrange the form items in a column, with the "align-items" and "justify-content" properties centering the items vertically and horizontally. - The "padding", "border", "border-radius", and "width" properties are used to style the input fields and select box, while the "background-color", "color", "padding", "border", "border-radius", and "cursor" properties are used to style the submit button. Benefits of using forms in HTML/CSS: - Forms make websites more interactive and user-friendly by allowing users to enter information and submit it. - Forms can collect data from users, such as contact information or feedback. - Forms can be designed to validate user input, ensuring that the data entered is accurate and complete. - Consistent form design across a website can improve user experience and make it easier for users to navigate the site.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值