CodeForces 465A inc ARG

本文详细介绍了如何在给定的n位二进制数中执行加一操作,并通过实例展示了该操作导致的不同位数变化情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A. inc ARG
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.


输入一个二进制数,从左往右加1,判断两数不同位数

#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;
}


资源下载链接为: https://pan.quark.cn/s/67c535f75d4c C语言作为一门基础且强大的编程语言,在底层系统编程和算法实现方面表现卓越,其效率与灵活性备受推崇。其中,“用指针实现的C语言排序算法”这一主题,融合了C语言的核心概念——指针,以及数据结构和算法的基础知识。指针是C语言的一大特色,它能够直接操作内存地址,从而为高效的数据操作提供了有力支持。在排序算法中,指针通常被用作迭代工具,用于遍历数组或链表,进而改变元素的顺序。 常见的排序算法,如冒泡排序、选择排序、插入排序、快速排序和归并排序等,都可以借助指针来实现。具体而言: 冒泡排序:通过交换相邻元素来实现排序。在C语言中,可以定义一个指向数组的指针,通过指针的递增或递减操作来遍历数组,比较相邻元素并在必要时进行交换。 选择排序:每次从剩余部分中找到最小(或最大)元素,然后将其与第一个未排序的元素进行交换。指针可用于标记已排序和未排序部分的边界。 插入排序:将元素插入到已排序的部分,以保持有序性。可以使用指针跟踪已排序部分的末尾,并在找到合适位置后进行插入操作。 快速排序:采用分治策略,选择一个“基准”元素,将数组分为两部分,一部分的所有元素都小于基准,另一部分的所有元素都大于基准。这一过程通常通过递归来实现,而基准元素的选择和划分过程往往涉及指针操作。 归并排序:将数组分为两半,分别对它们进行排序,然后再进行合并。在C语言中,这通常需要借助动态内存分配和指针操作来处理临时数组。 在实现这些排序算法时,理解指针的用法极为关键。指针不仅可以作为函数参数传递,从而使排序算法能够作用于任何可寻址的数据结构(如数组或链表),而且熟练掌握指针的解引用、算术运算和比较操作,对于编写高效的排序代码至关重要。然而,需要注意的是,尽管指针提供了直接操作内存的便利,但不当使用可能会引发错误,例如内存泄漏、空指针
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值