Getting Zero

Eureka.. 2022-04-16 17:35:54
加精

 

#include <iostream>

using namespace std;
/** Getting Zero
 * either set v=(v+1) mod 32768
 * or set v=(2*v) mod 32768
 * 输入:第一行输入n - 表示有n个整数;
 * 第二行 - 输入n个整数
 * 使ai等价于0的最少步骤
 */
int n;                              //输入n个整数
int a[32778];                       //储存整数的数组

/**< 考虑使用bfs或dfs 秉承能用b不用d的原则先试试bfs */
struct node{                        //新建队列
    int data;
};


int bfs(int ai)
{
    struct node que[32778];
    int book[32778] = {};
    int head = 1,tail = 1;          //首、尾指针
    que[tail++].data = ai;            //将需要处理的原数字入队
    book[ai] = 1;
    while(head < tail)
    {
        int x = que[head].data;

        int new_x1 = (x+1) % 32768; //第一种每次加1
        int new_x2 = (2*x) % 32768; //第二种每次乘2

        if(!x)
            break;
        if(!book[new_x1])
        {
            que[tail++].data = new_x1;
            book[new_x1] = book[x] + 1;
        }
        if(!book[new_x2])
        {
            que[tail++].data = new_x2;
            book[new_x2] = book[x] + 1;
        }
        head++;
    }
    return book[0] - 1;
}
int main()
{
    cin >> n;
    for(int i = 1;i <= n;i++)
    {
        cin >> a[i];
        cout << bfs(a[i]) << " ";
    }
    return 0;
}

 

...全文
110 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复
Key Features Get started with Raspberry Pi Zero and put all of its exciting features to use Create fun games and programs with little or no programming experience Learn to use this super-tiny PC to control hardware and software for work, play, and everything else Book Description Raspberry Pi Zero is half the size of Raspberry Pi A, only with twice the utility. At just three centimeters wide, it packs in every utility required for full-fledged computing tasks. This practical tutorial will help you quickly get up and running with Raspberry Pi Zero to control hardware and software and write simple programs and games. You will learn to build creative programs and exciting games with little or no programming experience. We cover all the features of Raspberry Pi Zero as you discover how to configure software and hardware, and control external devices. You will find out how to navigate your way in Raspbian, write simple Python scripts, and create simple DIY programs. What you will learn Understand how to initially download the operating system and set up Raspberry Pi Zero Find out how to control the GPIO pins of Raspberry Pi Zero to control LED circuits Get to grips with adding hardware to the GPIO to control more complex hardware such as motors Add USB control hardware to control a complex robot with 12 servos Include speech recognition so that projects can receive commands Enable the robot to communicate with the world around it by adding speech output Control the robot from a distance and see what the robot is seeing by adding wireless communication Discover how to build a Robotic hand and a Quadcopter About the Author Richard Grimmett has always been fascinated by computers and electronics since his very first programming project that used Fortran on punch cards. He has a bachelor's and master's degree in electrical engineering and a PhD in leadership studies. He also has 26 years of experience in the radar and telecommunications industries, and even has one of the original brick phones. He now teaches computer science and electrical engineering at the Brigham Young University, Idaho, where his office is filled with his numerous robotics projects. Table of Contents Getting Started with Raspberry Pi Zero Programming Raspberry Pi Zero Accessing the GPIO Pins on Raspberry Pi Zero Building and Controlling a Simple Wheeled Robot Building a Robot That Can Walk Adding Voice Recognition and Speech – A Voice Activated Robot Adding Raspberry Pi Zero to an RC Vehicle Playing Rock, Paper, or Scissors with Raspberry Pi Zero Adding Raspberry Pi Zero to a Quadcopter

51,747

社区成员

发帖
与我相关
我的任务
社区描述
和众多高校算法内卷分子,一起学习和交流算法那。浓郁的算法交流氛围,拒绝躺平,有效内卷。加入我们,私信我拉你入核心内卷群。
算法数据结构leetcode 个人社区
社区管理员
  • 执 梗
  • Dream-Y.ocean
  • ღCauchyོꦿ࿐
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

 刷题!

试试用AI创作助手写篇文章吧