简体   繁体   English

打印给定字符串中以 1 开头和结尾的子字符串的数量

[英]printing the number of substrings that start and end with 1 in a given string

For example, if the input string is “00100101”, then the output substrings should be “1001”, “100101” and “101”.例如,如果输入字符串是“00100101”,那么 output 子字符串应该是“1001”、“100101”和“101”。

i think there is a mistake with function but not sure.我认为 function 有一个错误,但不确定。 I got 2 errors Line 24, col 6 [Error] conflicting types for 'binarysubstring' and Line 4, col 6 [Note] previous declaration of 'binarysubstring' was here我有 2 个错误第 24 行,第 6 列 [错误] 'binarysubstring' 和第 4 行,第 6 列 [注意] 先前声明的 'binarysubstring' 的类型冲突在这里

#include<stdio.h>
#include<string.h>

long binarysubstring(int, char);

int main()
{
    int t,n,count;
    long r;
    char a[n+1];
    t=0;
    scanf("%d",&n);
    scanf("%s",&a);
    r=binarysubstring(n, a[n]);
    printf("%ld",r);
}

long binarysubstring(int n,char a[n])
{
    int i;
    long r;
    for(i=0;i<n;i++)
    {
        if(a[i]==1)
        {
            r+=1;
        }
    }
    r=((r*(r-1))/2);
    return r;
}

As there are multiple mistakes to process here, I'll try to explain and fix them starting from the errors you see on compilation.由于这里有多个错误需要处理,我将尝试从您在编译时看到的错误开始解释和修复它们。

First, your compilation error is because your function declaration takes a char as the second argument instead of a character array , line 4 should instead be首先,您的编译错误是因为您的 function 声明将char作为第二个参数而不是字符数组,第 4 行应该改为

long binarysubstring(int, char[]);

You also should not be declaring the array size inside the function signature on line 18.您也不应该在第 18 行的 function 签名中声明数组大小。

Your other immediate issue is that you initialize the buffer char a[n+1];您的另一个直接问题是您初始化缓冲区char a[n+1]; on line 10 before the input to define n on line 12, and since you did not define a value when n was declared on line 8, line 10 will actually declare a character array of size n + 1 = 0 + 1 = 1, which cannot store any input.在第 10 行,在第 12 行定义n之前的输入,并且由于在第 8 行声明n时您没有定义值,因此第 10 行实际上将声明一个大小为 n + 1 = 0 + 1 = 1 的字符数组,这无法存储任何输入。

With these three things fixed, the following code compiled for me with Clang-7:修复了这三件事后,使用 Clang-7 为我编译了以下代码:

#include<stdio.h>
#include<string.h>

long binarysubstring(int, char[]); // Added the brackets here

int main()
{
    int t,n,count;
    long r;
    t=0;
    scanf("%d",&n); // Moved this...
    char a[n+1]; // ...to be before this
    scanf("%s", a);
    r=binarysubstring(n, a);
    printf("%ld",r);
}

long binarysubstring(int n, char a[/*Removed n, do not pass anything here*/]) 
{
    int i;
    long r;
    for(i=0;i<n;i++)
    {
        if(a[i]==1)
        {
            r+=1;
        }
    }
    r=((r*(r-1))/2);
    return r;
}

However, this code always returns 0 because on line 24, your if statement compares each char to an integer , which is parsed to its ASCII code that is not the actual character of 1. As a result, r is never incremented because the condition is never true with the binary strings you are inputting, and it remains 0 up to your final sum formula, which then evaluates to 0 as well.但是,此代码始终返回 0,因为在第 24 行,您的 if 语句将每个charinteger进行比较,后者被解析为其 ASCII 代码,该代码不是 1 的实际字符。因此, r永远不会增加,因为条件是您输入的二进制字符串永远不会正确,并且在您的最终总和公式之前它仍然为 0,然后计算结果也为 0。

At this point, I'll give my attempt to implement what your question asks:在这一点上,我将尝试实现您的问题:

#include <stdio.h>

long binarysubstring(unsigned short, char[]);

int main() {
    unsigned short n;
    printf("Input the length of your string: ");
    scanf("%hu", &n);
    char input[n + 1];
    printf("Input the binary string: ");
    scanf("%s", input);
    long answer = binarysubstring(n, input);
    printf("Answer: %ld\n", answer);
}

long binarysubstring(unsigned short n, char a[]) {
    long r = 0;
    for (unsigned short i = 0; i < n; ++i) {
      if (a[i] == '1') {
        ++r;
      }
    }
    return r*(r-1)/2;
}

This assumes the input is a binary string.这假设输入是二进制字符串。 I use the same algorithm you appear to, counting the number of times 1 appears in the string with r and then returning the sum of the integers in the sequence from 1 to r , inclusive.我使用与您看起来相同的算法,计算 1 在r字符串中出现的次数,然后返回从 1 到r序列中的整数之和,包括在内。 It also happens to return 0 for binary strings of length 1.对于长度为 1 的二进制字符串,它也恰好返回 0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM