Input an integer containing only 0s and 1s (i.e., a “binary” integer) and print its decimal equivalent.

[Hint: Use the remainder and division operators to pick off the “binary” number’s digits one at a time from right to left. Just as in the decimal number system, in which the rightmost digit has a positional value of 1, and the next digit left has a positional value of 10, then 100, then 1000, and so on, in the binary number system the rightmost digit has a positional value of 1, the next digit left has a positional value of 2, then 4, then 8, and so on. Thus the decimal number 234 can be interpreted as4 * 1 + 3 * 10 + 2 * 100 . The decimal equivalent of binary 1101= 1 * 1 + 0 * 2 + 1 * 4 + 1 * 8 ]

Input Specification:

Input an integer containing only 0s and 1s.

Output Specification:

Print its decimal equivalent of binary (1101 = 1 * 1 + 0 * 2 + 1 * 4 + 1 * 8 ). After showing the number, seperate each number and symbol by ong space.

Sample Input:

1001101

结尾无空行

Sample Output:

1001101 = 1 * 1 + 0 * 2 + 1 * 4 + 1 * 8 + 0 * 16 + 0 * 32 + 1 * 64

结尾无空行

作者sunjun单位武汉理工大学代码长度限制16 KB时间限制400 ms内存限制64 MB

答案:

#include <iostream>
#include <string>
#include <math.h>
#include <cstring>
using namespace std;

int main(){
    char str[10];
    cin >> str;
    char cha[10] = {0};
    strcpy(cha,str);
    string str2 = " * ";
    string str3 = " + ";
    string str1[10];
    for(int i=strlen(str)-1,j=0; i>=0,j<=strlen(str)-1; i--,j++){      
        if(j != strlen(str)-1){
            str1[j] += cha[i];
            str1[j] += str2;
            str1[j] += std::to_string((int)pow(2,j));
            str1[j] += str3;
        }else{
            str1[j] += cha[i];
            str1[j] += str2;
            str1[j] += std::to_string((int)pow(2,j));
        }
    }
    cout << str << " = ";
    for(int i=0; i<=strlen(str); i++)
        cout << str1[i];
    return 0;
}

7-18 Decimal Equivalent of a Binary Number (10 分)相关推荐

  1. 10位以内二进制数字转十进制计算器(Printing the Decimal Equivalent of a Binary Number)

    挺有成就感的一个小程序,二进制转十进制. 代码如下: //JHTP Exercise 4.31: Printing the Decimal Equivalent of a Binary Number ...

  2. 2019长安大学ACM校赛网络同步赛 J Binary Number(组合数学+贪心)

    链接:https://ac.nowcoder.com/acm/contest/897/J 来源:牛客网 Binary Number 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32 ...

  3. 693. Binary Number with Alternating Bits -LeetCode

    为什么80%的码农都做不了架构师?>>>    Question 693. Binary Number with Alternating Bits Solution 思路:输入一个整 ...

  4. Leetcode PHP题解--D58 693. Binary Number with Alternating Bits

    2019独角兽企业重金招聘Python工程师标准>>> D58 693. Binary Number with Alternating Bits 题目链接 693. Binary N ...

  5. 1102 Invert a Binary Tree (25 分)

    1102 Invert a Binary Tree (25 分) The following is from Max Howell @twitter: Google: 90% of our engin ...

  6. PAT甲级1038 Recover the Smallest Number (30 分):[C++题解]贪心、排列成最小的数、字符串

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: 贪心: 对于字符串a和b,如果 a+b < b+a (这里+代表字符串中的连接)代表字典序更小.举例 a = 321 , b ...

  7. PAT甲级1151 LCA in a Binary Tree (30 分):[C++题解]LCA、最低公共祖先、哈希表映射

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: 和下面这道题几乎是同一题:PAT甲级1143 Lowest Common Ancestor (30 分):[C++题解]LCA.最低 ...

  8. VS Code 1.18版本更新内容整理(2017年10月 October 2017)

    VS Code 1.18版本更新内容整理(2017年10月 October 2017) 久前开始使用的VS Code,使用一段时间以后确实感觉比之前在用的Sublime Text好很多,可能是汉化及插 ...

  9. PAT甲级 1151 LCA in a Binary Tree (30分) LCA算法/C++

    1151 LCA in a Binary Tree (30分) 题目大意:给出一棵树的中序和先序遍历,找到这棵树中U和V最小的共同祖先. Tips: 使用一个unordered_map记录中序遍历的值 ...

最新文章

  1. Linux镜像USB转网口驱动模块配置
  2. 生成从A到Z这个一个字符序列
  3. python stm32-尝试一下使用Python来开发STM32
  4. EnterpriseLibrary2.0系列文章及下载
  5. 两种计算和输出n内5要么9除尽互惠
  6. JS-继承(es5,es6)
  7. Linux下c开发 之 线程通信
  8. Linux文件系统与日志
  9. 并查集——食物链(poj1182)
  10. 二.激光SLAM框架学习之A-LOAM框架---介绍及其演示
  11. 撬动百亿VRAR产业,让VR们“造”起来
  12. POJ 1789(最小生成树)
  13. ta leader是什么岗位_阿里专家:如何成为一名“值得跟”的Leader?
  14. 写给想要做自动化测试的人
  15. 解决问题--java.lang.ClassNotFoundException -- redis抛出
  16. python html5 便利店收银系统_便利店收银系统使用教程
  17. sai厚涂上色教程,sai厚涂绘画人物上色教程!
  18. PyAudio模块的基本使用,阻塞式/非阻塞式地录制/播放音频
  19. 【HUSTOJ】1053: 字符图形9-数字正三角
  20. [shell编程] 入门基础总结 (一) (一种豁然开朗的感觉)

热门文章

  1. 红黑树--(高清无码图+代码)演示
  2. 密码学_SM4国密算法
  3. 360手机java手机管家软件_360手机管家最新版下载_360手机管家官方下载-太平洋下载中心...
  4. 候选码、主码、外码、主属性、主键、主关键字、非主属性
  5. 指针类型和指向的数据类型
  6. 数字化门店| 旧衣回收店系统 | 智慧门店小程序开发教程
  7. oracle数据库的blob和clob类型,是什么意思和区别
  8. 使用 break 语句直接强行退出循环
  9. android修改输出分辨率,编译Android8.1修改默认分辨率和屏幕密度
  10. Robin一个专注开发者的组织