题目1208:10进制 VS 2进制(进制转换以及大数保存问题)

题目链接:http://ac.jobdu.com/problem.php?pid=1208

详细链接:https://github.com/zpfbuaa/JobduInCPlusPlus

参考代码:

//
// 1208 10进制 VS 2进制.cpp
// Jobdu
//
// Created by PengFei_Zheng on 17/04/2017.
// Copyright © 2017 PengFei_Zheng. All rights reserved.
// #include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <cmath>
#define MAX_SIZE 10010
#define FROM 10
#define TO 2 using namespace std; int main(){
char from[MAX_SIZE];
while(scanf("%s",from)!=EOF){
int len = (int)strlen(from);
int size = ; char to[MAX_SIZE];//逆序保存十进制数对应的二进制,正好是题目需要的逆置二进制
int ans[MAX_SIZE];//保存最终计算结果 while(true){//FROM 进制转为 TO 进制
int i = ;
while(from[i]=='' && i < len) i++;
if(i == len) break;
int remain = ;
for(; i < len ; i++){
int tmp = from[i] - '' + remain * FROM;
from[i] = tmp/TO+'';
remain = tmp%TO;
}
to[size++] = remain+'';
}
// cout<<to<<endl;
int length = ;
ans[] = ;
for(int i = ; i < size ; i++){//1011 --> (((0*2+1)*2+0)*2+1)*2+1
int carry = to[i] - '';
for(int j = ; j < length ; j++){
if(j==)
ans[j] = ans[j] * TO + carry;
else
ans[j] = ans[j] * TO;
}
for(int j = ; j < length ; j++){
if(j == length - && ans[j] >=){
ans[j] = ans[j]%;
ans[++j] = ;
length++;
}
else if (ans[j]>=){
ans[j] = ans[j]%;
ans[j+]++;
}
}
}
for(int i = length - ; i >= ; i--){
printf("%d",ans[i]);
}
printf("\n");
}
return ;
} /**************************************************************
Problem: 1208
User: zpfbuaa
Language: C++
Result: Accepted
Time:40 ms
Memory:1520 kb
****************************************************************/
上一篇:Tomcat配置HTTPS方式生成安全证书


下一篇:Cannot resolve MVC View