Codeforces Round #265 (Div. 2) A. inc ARG
来源:互联网 发布:知乎日报 for mac dmg 编辑:程序博客网 时间:2024/05/17 22:28
Sergey is testing a next-generation processor. Instead of bytes the processor works with memory cells consisting ofn bits. These bits are numbered from 1 to n. An integer is stored in the cell in the following way: the least significant bit is stored in the first bit of the cell, the next significant bit is stored in the second bit, and so on; the most significant bit is stored in the n-th bit.
Now Sergey wants to test the following instruction: "add 1 to the value of the cell". As a result of the instruction, the integer that is written in the cell must be increased by one; if some of the most significant bits of the resulting number do not fit into the cell, they must be discarded.
Sergey wrote certain values of the bits in the cell and is going to add one to its value. How many bits of the cell will change after the operation?
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of bits in the cell.
The second line contains a string consisting of n characters — the initial state of the cell. The first character denotes the state of the first bit of the cell. The second character denotes the second least significant bit and so on. The last character denotes the state of the most significant bit.
Print a single integer — the number of bits in the cell which change their state after we add 1 to the cell.
41100
3
41111
4
In the first sample the cell ends up with value 0010, in the second sample — with0000.
题意:一个二进制+1后变化的位数,是从左到右的
思路:找到第一个0的位置就对了
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int main() {int n, num[110];char str[110];scanf("%d", &n);scanf("%s", str);for (int i = 0; i < n; i++)num[i] = str[i] - '0';int ans = 0;for (int i = 0; i < n; i++) {if (num[i] == 1)ans++;else {ans++;break;}}printf("%d\n", ans);return 0;}
- Codeforces Round #265 (Div. 2) 465A. inc ARG(数学题)
- Codeforces Round #265 (Div. 2) A. inc ARG
- Codeforces Round #265 (div2) A. inc ARG
- CodeForces 465A inc ARG
- Codeforces 465A inc ARG
- Codeforces-465A-inc ARG
- codeforces-#465A. inc ARG(模拟)
- Codeforces 465A inc ARG(水题)
- A. inc ARG
- Codeforces Beta Round #86 (Div. 2 Only) B PFAST Inc.
- Codeforces Round #265 (Div. 1) A
- Codeforces Round #131 (Div. 2) A B
- Codeforces Round #174 (Div. 2) Problem A
- Codeforces Round #173 (Div. 2) A.Bit++
- Codeforces Round #181 (Div. 2) A题
- Codeforces Round #184 (Div. 2) A题
- Codeforces Round #185 (Div. 2)--A,B
- Codeforces Round #172 (Div. 2) A题
- 阿里校招附加题2014-8-29
- ubuntu 12.04 选择GRUB启动项的问题
- GC垃圾收集器 C#
- 前端发展所悟(一)
- cocos重力感应
- Codeforces Round #265 (Div. 2) A. inc ARG
- QML类型说明-JumpListCategory
- 隐马尔科夫模型之前向算法和维特比算法
- STM32 I2C从机发送数据
- 交换排序之快速排序
- 两个驱动程序安装错误
- Android自定义控件——仿ios开关按钮
- Web.xml配置详解之context-param
- 通过SSIS实现Oracle与Sqlserver数据库间的数据同步,并定时执行同步任务