Sicily 6084. Times17

Description

After realizing that there is much money to be made in software development, Farmer John has launched a small side business writing short programs for clients in the local farming industry. Farmer John's first programming task seems quite simple to him -- almost too simple: his client wants him to write a program that takes a number N as input, and prints 17 times N as output. Farmer John has just finished writing this simple program when the client calls him up in a panic and informs him that the input and output both must be expressed as binary numbers, and that these might be quite large. Please help Farmer John complete his programming task. Given an input number N, written in binary with at most 1000 digits, please write out the binary representation of 17 times N.

Input

* Line 1: The binary representation of N (at most 1000 digits).

Output

* Line 1: The binary representation of N times 17.

Sample Input
 Copy sample input to clipboard
10110111
Sample Output
110000100111
// Problem#: 6084
// Submission#: 3667523
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<cctype>
#include<algorithm>
#include<cmath>
#include<climits>
#include<stack>
#include<vector>
using namespace std;
int main(){
    string n_binary;
    cin>>n_binary;
    string result_binary=n_binary+"0000";
    n_binary="0000"+n_binary;
    int len=result_binary.length();
    int temp=0;
    for(int i=len-1;i>0;i--){
        if((n_binary[i]-'0')+(result_binary[i]-'0')+temp<2){
            result_binary[i]=(n_binary[i]-'0')+(result_binary[i]-'0')+temp+'0';
            temp=0;
        }
        else{
            result_binary[i]=(n_binary[i]-'0')+(result_binary[i]-'0')+temp+'0'-2;
            temp=1;
        }
    }
    if(temp==1){
        result_binary[0]='0';
        result_binary="1"+result_binary;
    }
    cout<<result_binary<<endl;
    return 0;
}                                 

#include<iostream>
using namespace std;


int main(){
    string n_binary;
    cin>>n_binary;
    string result_binary=n_binary+"0000";//乘以17可以先乘上16在加上本身
    n_binary="0000"+n_binary;//补足位数进行模拟手算
    int len=result_binary.length();
    int temp=0;//记录进位
    for(int i=len-1;i>0;i--){//模拟手算加法
        if((n_binary[i]-'0')+(result_binary[i]-'0')+temp<2){//不够进位
            result_binary[i]=(n_binary[i]-'0')+(result_binary[i]-'0')+temp+'0';
            temp=0;
        }
        else{
            result_binary[i]=(n_binary[i]-'0')+(result_binary[i]-'0')+temp+'0'-2;
            temp=1;
        }
    }
    if(temp==1){//如果最高位有进位需要在前面补'1',而进位之后第二位变为'0'
        result_binary[0]='0';
        result_binary="1"+result_binary;
    }
    cout<<result_binary<<endl;
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值