Codeforces Round #265 (Div. 2) 465A. inc ARG(数学题)

题目链接:http://codeforces.com/problemset/problem/465/A

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?

Input

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.

Output

Print a single integer — the number of bits in the cell which change their state after we add 1 to the cell.

Sample test(s)

input

4
1100

output

3

input

4
1111

output

4

Note

In the first sample the cell ends up with value 0010, in the second sample — with 0000.

代码2如下:

#include <cstdio>
int main()
{
    int n;
    char s[117];
    while(~scanf("%d",&n))
    {
        getchar();
        gets(s);
        int ans = 0;
        for(int i = 0; i < n; i++)
        {
            ans++;
            if(s[i] == '0')
                break;
        }
        printf("%d\n",ans);
    }
    return 0;
}

代码2如下:

#include <cstdio>
#include <cstring>
int main()
{
    int n;
    char s[100];
    int a[117],b[117];
    while(~scanf("%d",&n))
    {
        getchar();
        gets(s);
        for(int i = 1; i <= n; i++)
        {
            b[i] = a[i] = s[i-1]-'0';
        }
        int tt = 1;
        for(int i = 1; i <= n; i++)
        {
            a[i]+=tt;
            if(a[i] >= 2)
            {
                a[i] = 0;
                tt = 1;
            }
            else
                tt = 0;
        }
        int k = 0;
        for(int i = 1; i <= n; i++)
        {
            if(a[i] != b[i])
                k++;
        }
        printf("%d\n",k);
    }
    return 0;
}
时间: 2024-10-10 07:40:22

Codeforces Round #265 (Div. 2) 465A. inc ARG(数学题)的相关文章

Codeforces Round #265 (Div. 2)

Codeforces Round #265 (Div. 2) 题目链接 A:把数字变换后比较一下几个不一样即可 B:连续2个以上0当作一次操作,开头的0和结尾的0可以忽略 C:贪心从末尾去构造,由于保证一开始是回文,所以保证修改后出现回文只可能为长度2或3的,这样的话判断复杂度就很小了 D:暴力枚举情况,然后判断 E:把操作逆过来处理出每个数字对应的位数和相应数字,然后在for一遍计算答案即可 代码: A: #include <cstdio> #include <cstring>

Codeforces Round #265 (Div. 1)

A No to Palindromes! 题意:给一个长度为n的用前m个字符构成的字符串,定义一个字符串是好的当且仅当他的每个子串都不是回文的,现在给出一个好的字符串,求比他字典序大的第一个好的字符串. 题解:从后往前每一位枚举,若把当前枚举的位改成ch后为好的串,只需要判断他和他前面的一个字符是否相同构成长度为2的回文串,或者和他前面的前面的两个字符构成长度为3的回文串. 于是找到第一个可以换的位置,后面每个位置从'a'开始枚举可以取得的第一个字符即可. 1 #include <cstdio>

Codeforces Round #265 (Div. 2) 题解

A:给你一个二进制数,问你加一以后改变多少位 解题思路:乱搞 解题代码: 1 // File Name: a.cpp 2 // Author: darkdream 3 // Created Time: 2014年09月07日 星期日 23时27分31秒 4 5 #include<vector> 6 #include<list> 7 #include<map> 8 #include<set> 9 #include<deque> 10 #includ

Codeforces Round #265 (Div. 2) B. Inbox (100500)

Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread. Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the cont

Codeforces Round #265 (Div. 2) C. No to Palindromes!

Paul hates palindromes. He assumes that string s is tolerable if each its character is one of the first p letters of the English alphabet and s doesn't contain any palindrome contiguous substring of length 2 or more. Paul has found a tolerable string

Codeforces Round #265 (Div. 2) D. Restore Cube

Peter had a cube with non-zero length of a side. He put the cube into three-dimensional space in such a way that its vertices lay at integer points (it is possible that the cube's sides are not parallel to the coordinate axes). Then he took a piece o

Codeforces Round #279 (Div. 2) ABCD

Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name     A Team Olympiad standard input/output 1 s, 256 MB  x2377 B Queue standard input/output 2 s, 256 MB  x1250 C Hacking Cypher standard input/output 1 s, 256 MB  x740 D Chocolate standard input/

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i

Codeforces Round #424 (Div. 2) D. Office Keys(dp)

题目链接:Codeforces Round #424 (Div. 2) D. Office Keys 题意: 在一条轴上有n个人,和m个钥匙,门在s位置. 现在每个人走单位距离需要单位时间. 每个钥匙只能被一个人拿. 求全部的人拿到钥匙并且走到门的最短时间. 题解: 显然没有交叉的情况,因为如果交叉的话可能不是最优解. 然后考虑dp[i][j]表示第i个人拿了第j把钥匙,然后 dp[i][j]=max(val(i,j),min(dp[i-1][i-1~j]))   val(i,j)表示第i个人拿