18.12.21 【USACO】Times17

题目描述

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.

输入

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

输出

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

样例输入

10110111

样例输出

110000100111

来源

USACO 2012 March Bronze

 1 #include <iostream>
 2 #include <string.h>
 3 #include <algorithm>
 4 #include <stack>
 5 #include <string>
 6 #include <math.h>
 7 #include <queue>
 8 #include <stdio.h>
 9 #include <string.h>
10 #include <set>
11 #include <vector>
12 #include <fstream>
13 #define maxn 1005
14 #define inf 999999
15 #define cha 127
16 #define eps 1e-6 
17 #define oo 1503
18 using namespace std;
19 
20 string a, b;
21 int n1[maxn], n2[maxn];
22 
23 void init() {
24     cin >> a;
25     b = a + "0000";
26     int sizea = a.length(), sizeb = b.length();
27     for (int i = 4; i < sizeb; i++)
28         n1[i] = a[i-4] - '0';
29     for (int i = 0; i < sizeb; i++)
30         n2[i] = b[i] - '0';
31     for (int i = sizeb - 1; i >= 1; i--) {
32         n2[i] = n2[i] + n1[i];
33         n2[i - 1] += n2[i] / 2;
34         n2[i] %= 2;
35     }
36     string tmp;
37     while (n2[0] != 0) {
38         char ch = n2[0] % 2 + '0';
39         tmp = ch + tmp;
40         n2[0] /= 2;
41     }
42     cout << tmp;
43     for (int i = 1; i < sizeb; i++)
44         printf("%d", n2[i]);
45     printf("\n");
46 }
47 
48 int main() {
49     init();
50     return 0;
51 }
View Code

 

转载于:https://www.cnblogs.com/yalphait/p/10155117.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值