天天看點

鄭輕OJ zzulioj 1068: 二進制數

http://acm.zzuli.edu.cn/zzuliacm/problem.php?id=1068

第一次嘗試把n進制化為十進制,竟然成功了

1068: 二進制數

Time Limit: 1 Sec   Memory Limit: 128 MB

Submit: 2330   Solved: 1404

Submit Status Web Board

Description

将一個二進制數,轉換為對應的十進制數。

Input

輸入一個二進制數,以回車結束。該二進制數為正數,長度不超過31。

Output

輸出一個整數,為該二進制數對應的十進制數。

Sample Input

100000000001
      

Sample Output

2049
      
#include<stdio.h>
#include<string.h>
#define N 35

int er(int x) {
	int s=1;
	for(int j=1; j<=x; j++)
		s=s*2;
	return s;
}

int main() {
	char wqs[N];
	int n,ans;
	while(scanf("%s",wqs)!=EOF) {
		ans=0;
		n=strlen(wqs);
		int i=0;
		for(int j=n-1; j>=0; j--) {
			int shu=wqs[j]-'0';
			ans=ans+shu*er(i++);
		}
		printf("%d\n",ans);
	}
	return 0;
}