Sicily 6084. Times17

来源:互联网 发布:阿里云总部 良渚 编辑:程序博客网 时间:2024/06/06 08:59
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
原创粉丝点击