• C语言 · 进制转换


    算法训练 进制转换  
    时间限制:1.0s   内存限制:512.0MB
        
    问题描述
      编写一个程序,输入一个二进制的字符串(长度不超过32),然后计算出相应的十进制整数,并把它打印出来。
      输入格式:输入为一个字符串,每个字符都是’0’或’1’,字符串的长度不超过32。
      输出格式:输出一个整数。
      输入输出样例
    样例输入
    1101
    样例输出
    13
     
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<math.h>
     4 int main(){
     5     char a[32];
     6     int sum=0,k=1;
     7     gets(a);
     8     int len = strlen(a);
     9     for(int i=len-1;i>=0;i--){
    10         if(i==len-1)//个位 
    11             k=1;
    12         else//其他位 
    13             k*=2;
    14         sum=sum+(a[i]-'0')*k;
    15     }
    16     printf("%d",sum);
    17 }
  • 相关阅读:
    python 线程同步
    python 线程模块
    Python线程
    Python 多线程
    Python SMTP发送邮件
    Python Internet 模块
    简单实例
    Socket 对象(内建)方法
    Python 网络编程
    python 数据库错误处理
  • 原文地址:https://www.cnblogs.com/panweiwei/p/6575186.html
Copyright © 2020-2023  润新知