Codeforces Round #265 (div2) A. inc ARG
来源:互联网 发布:软件开发团队协作 编辑:程序博客网 时间:2024/04/30 09:28
Sergey is testing a next-generation processor. Instead of bytes the processor works with memory cells consisting of n 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 — with 0000.
水题,二进制的一个数加一,判断有多少位发生变化,从低位到高位输入。从低位到高位判断到第一个0结束就行了。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;char s[200];int a[200];int main(){ int n; scanf("%d",&n); scanf("%s",s); for(int i=0;i<n;i++) a[i]=s[i]-'0'; int ans=0; for(int i=0;i<n;i++) { if(a[i]==1) { ans++; } else { ans++; break; } } printf("%d\n",ans); return 0;}
- Codeforces Round #265 (div2) A. inc ARG
- Codeforces Round #265 (Div. 2) 465A. inc ARG(数学题)
- Codeforces Round #265 (Div. 2) 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 div2 round#231 A
- codeforces div2 round#230 A
- codeforces Round 411 div2 A
- Codeforces Round #376A (div2)
- codeforces Round #377 div2 A B C(数学推导)
- Codeforces Round #265(div2)题解
- codeforces Round #237(div2) A解题报告
- codeforces Round #238(div2) A解题报告
- codeforces Round #241(div2) A解题报告
- patch修改
- android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context r
- 【移动开发】在Canvas上显示动画
- 线性回归 Linear Regression, 逻辑回归 Logistic Regression 小结
- Problem I: 俊爷的局域网
- Codeforces Round #265 (div2) A. inc ARG
- 【OC学习-26】对象的浅拷贝和深拷贝——关键在于属性是否可被拷贝
- Runtime Error的几种情况
- stack与queue用法及它们的几层含义
- vshield manager的一些笔记
- jQuery经典面试题及答案精选
- What's wrong with 8051
- Ubuntu 14.04 开启启动器图标最小化功能
- bluedroid移植(串口流控)