算法训练 进制转换

    xiaoxiao2021-03-25  153

    问题描述   编写一个程序,输入一个二进制的字符串(长度不超过32),然后计算出相应的十进制整数,并把它打印出来。   输入格式:输入为一个字符串,每个字符都是’0’或’1’,字符串的长度不超过32。   输出格式:输出一个整数。   输入输出样例 样例输入 1101 样例输出 13   #include<stdio.h> #include<math.h> #include<string.h> int main(){ char s[32]; scanf("%s",s); int i,ans=0;//初始化 for(i=0;i<strlen(s);i++){ ans+=(s[i]-48)*pow(2,strlen(s)-1-i); } printf("%d",ans); return 0; }
    转载请注明原文地址: https://ju.6miu.com/read-10033.html

    最新回复(0)