基数(数字系统) [英] radix (numeral systems)

查看:62
本文介绍了基数(数字系统)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题:
找出所有不超过给定m的自然数,即表示对称序列的二进制自然数和1(从1开始).显示这些数字的十进制和二进制记录.

我是C ++编程的新手.我一直在尝试解决这个问题,但没有成功.

This is my question:
Find all natural numbers not exceeding a given m, the binary notation which is a symmetrical sequence nudey and ones (starting from one). Show decimal and binary records of these numbers.

I am new to programming in C++. I''ve been trying to solve this question but haven''t been successful.

int m;
cout<<"Enter the starting number: ";
cin>>m;

while(m>0)
{
  cout<<m<<"   ";
  --m;
}


这给了我我所需要的.

现在,我需要获取这些数字的二进制形式,并找出对称的数字.
例如1001.
之后,属于该类别的内容将更改回十进制并显示在监视器上.

如果有人可以帮助我,我将非常感激.


This gave me what i needed.

Now I need to get the binary form of these numbers and show out the ones that are symmetrical.
i.e for example 1001.
After which the ones that fall into the category will be changed back decimal and shown on the monitor.

I will be very grateful if some could help me out.
Thank you in anticipation for your response.

推荐答案

查看以下api以便轻松转换为二进制和字符串:

http://www.cplusplus.com/reference/clibrary/cstdlib/itoa/ [ ^ ]

http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/ [ ^ ]

现在该怎么办:
-用户输入一个数字,该数字是最大数字,您只希望该数字以下是二进制对称的数字.
-现在您需要一个循环,以"i"开头并将其初始化为1
-使用上述api
将我转换为二进制字符串 -将结果二进制字符串复制到另一个字符串中,该字符串必须很大
-继续从第一个二进制字符串追加到该字符串,但从字符串的末尾到开头.您现在有了一个对称字符串
-将其转换为数字.
-如果数字大于用户输入的数字,则循环应停止,否则打印数字和二进制表示形式.

这应该可以让您开始获得足够的信息来完成我认为的工作.
Check out the following api''s for easy converting to binary and to strings :

http://www.cplusplus.com/reference/clibrary/cstdlib/itoa/[^]

http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/[^]

Now how go about this :
- user enters a number, this number is the maximum number, you only want numbers that are binary symetrical below that number.
- now you need a loop where you will start with say i and initialise it to 1
- convert i to a binary string using above api
- copy the result binary string in another string, this string must be big
- keep appending to this string from the first binary string, but from the back of the string to the beginning. You now have a symetrical string
- convert it to a number.
- if the number is bigger than what the user enters, loop should stop, otherwise print the number and binary representation.

This should get you started with enough information to get the job done I think.


从二进制数的字符串(您说过的)开始,创建两个指向(1的指针)二进制字符串的开头,以及(2)同一字符串的最后一个字符.进行一个循环,然后在该循​​环内比较两个指针所指向的字符,然后将第一个指针移至下一个,将结束指针移至前一个字符.重复直到发现不匹配或第一个指针移到结束指针之外.

注意,当我说指针时,没有什么可以阻止您使用std :: string和迭代器.但是,在那种情况下,比较迭代器可能比较棘手(请再次询问您是否无法解决问题)
Starting from the string of the binary number (which you said you have), create two pointers pointing to (1) the start of the binary string and (2) the last character of that same string. Make a loop, and inside that loop compare the characters the two pointers are pointing to, then move the first pointer to the next, and the end pointer to the previous character. Repeat until either you find a mismatch, or the first pointer moves past the end pointer.

Note, when I say pointer, nothing stops you from using a std::string and iterators instead. In that case however comparing the iterators may be tricky (feel free to ask again if you can''t solve it)


这篇关于基数(数字系统)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆