Two Buttons

点击打开链接

题目的意思就是要n变为m可以通过减一或者成二变为m,第一种方法用广搜(bfs):

#include<stdio.h>

#include<string>
#include<queue>
#include<string.h>
using namespace std;
struct node
{
    int num;//数
    int step;//记录次数
}st,ed,p;
int a[20005];
void bfs(int n,int m)
{
    memset(a,0,sizeof(a));
    queue<node>q;
    st.num=n;
    st.step=0;
    a[st.num]=1;//一定要标记,不然会wa…………
    q.push(st);
    while(!q.empty())
    {
        ed=q.front();
        if(ed.num==m)
        {
            printf("%d\n",ed.step);
        }
        q.pop();
        p.step=ed.step+1;


        if(2*ed.num>0&&2*ed.num<2*m&&a[2*ed.num]==0)
        {
            p.num=2*ed.num;
            a[p.num]=1;
            q.push(p);
        }
        if(ed.num-1>0&&ed.num-1<2*m&&a[ed.num-1]==0)
        {
            p.num=ed.num-1;
            a[ed.num-1]=1;
            q.push(p);
        }
    }


}
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    if(n>=m)
    {
        printf("%d\n",n-m);
    }
    else
    {
        bfs(n,m);
    }

}

当m>n,要让n变到m有两种方法(1):2*n-m+1;(2):n-m/2+1;用二式减一式得到n-m/2;由一式可以得出2*n>m(-->)n>m/2;所以n-m/2>0,所以当m%2==0&&m>n时让m/2是最好的方法,让n先变得小于等于m:

#include <iostream>
#include<stdio.h>
#include<string>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
using namespace std;
int main()
{
  int n,m;
  int num=0;记录次数
  scanf("%d%d",&n,&m);
      while(m>n)
      {
          if(m%2==0)
          {
              m/=2;
          }
          else
          {
              m++;
          }
          num++;
      }
      printf("%d\n",num+n-m);


}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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、付费专栏及课程。

余额充值