简体   繁体   English

为什么我的空字符串分配没有清除我的字符串

[英]Why my empty string assignment doesn't clear my string

I have an exercise which looks like that:我有一个看起来像这样的练习:

Problem statement is simple and straight forward.问题陈述简单直接。 You will be given a non-negative integer P of length N and you need to check whether it's divisible by Q?您将得到一个长度为 N 的非负 integer P,您需要检查它是否可以被 Q 整除?

Integer P will be given in its decimal representation with P0 as leftmost digit and P1 as second digit from left ! Integer P 将以十进制表示形式给出,P0 为最左边的数字,P1 为左起第二个数字!

Rest of the digit can be generated from the formula: Rest 的位数可以由公式生成:

Pi = ( 4*Pi-1 + Pi-2 ) modulo Q for 2 <= i <= N-1 Pi = ( 4*Pi-1 + Pi-2 ) 模 Q 为 2 <= i <= N-1

Input The first line contains one integer T - denoting the number of test cases.输入 第一行包含一个 integer T - 表示测试用例的数量。

T lines follow each containing four integers P0, P1, Q and N ! T 行之后,每行包含四个整数 P0、P1、Q 和 N!

Output For each testcase output YES if the corresponding integer is divisible by Q and NO otherwise. Output 对于每个测试用例 output 如果相应的 integer 可被 Q 整除,则为 YES,否则为 NO。

Constraints T <= 100000 0 < P0, P1, Q < 10 0 < N <= 1018 Example Input:约束 T <= 100000 0 < P0, P1, Q < 10 0 < N <= 1018 示例输入:

4 4

1 4 2 2 1 4 2 2

1 4 2 1 1 4 2 1

4 2 3 2 4 2 3 2

3 4 7 3 3 4 7 3

Output: YES NO YES NO Explanation Value of P is 14, 1, 42, 345 in respective cases ! Output: YES NO YES NO 说明 P 值分别为 14、1、42、345 !

and that's what I came up with这就是我想出的

    #include <iostream>
    #include <vector>
    #include <string>
    
    using namespace std;

    int main()
    {
        int t, q, n, p_0, p_1, p_temp, p;
        vector<int> digits;
        vector<string> answers;
        string number = "";
    
        cin >> t;
    
        for (int i = 0; i < t; i++)
        {
            cin >> p_0 >> p_1 >> q >> n;
            if (n == 1)
            {
                digits.push_back(p_0);
            }
            else
            {
                digits.push_back(p_0);
                digits.push_back(p_1);
                for (int i = 2; i <= (n - 1); i++)
                {
                    p_temp = (4 * digits[i - 1] + digits[i - 2]) % q;
                    digits.push_back(p_temp);
                }
            }
            for (int i = 0; i < digits.size(); i++)
            {
                number += to_string(digits[i]);
            }
            p = stoi(number);
            cout << number << endl;
            if (p % q == 0)
            {
                answers.push_back("YES");
            }
            else
            {
                answers.push_back("NO");
            }
            number = "";
        }
        for (int i = 0; i < answers.size(); i++)
        {
            cout << answers[i] << endl;
        }
    
    }

Everything I have done works fine, except for one thing, this part does not clear my number variable我所做的一切都很好,除了一件事,这部分没有清除我的number变量

    number = "";

And honestly I don't know why, could someone correct my mistakes and explain me what did I do wrong.老实说,我不知道为什么,有人可以纠正我的错误并解释我做错了什么。 Thanks.谢谢。

Your problem is with the digits vector.你的问题是数字向量。

Each loop the number string just gets repopulated with the digits vector which is never cleared.每个循环,数字字符串都会重新填充数字向量,而该数字向量永远不会被清除。

Use digits.clear() to empty the vector like so:使用 digits.clear() 来清空向量,如下所示:

#include <iostream>
#include <vector>
#include <string>

using namespace std;



int main()
{
    int t, q, n, p_0, p_1, p_temp, p;
    vector<int> digits;
    vector<string> answers;
    string number = "";

    cin >> t;

    for (int i = 0; i < t; i++)
    {
        cin >> p_0 >> p_1 >> q >> n;

        

        if (n == 1)
        {
            digits.push_back(p_0);
        }
        else
        {
            digits.push_back(p_0);
            digits.push_back(p_1);
            for (int i = 2; i <= (n - 1); i++)
            {
                p_temp = (4 * digits[i - 1] + digits[i - 2]) % q;
                digits.push_back(p_temp);
            }
        }

        for (int i = 0; i < digits.size(); i++)
        {
            number += to_string(digits[i]);
        }

        p = stoi(number);
        cout << number << endl;

        if (p % q == 0)
        {
            answers.push_back("YES");
        }
        else
        {
            answers.push_back("NO");
        }

        digits.clear();
        number = "";
    }

    for (int i = 0; i < answers.size(); i++)
    {
        cout << answers[i] << endl;
    }

}

To clear a string you can/should use std::string::clear() as:要清除字符串,您可以/应该使用std::string::clear()作为:

number.clear();

There may be other logical errors in your program which may be the reason for not getting the output you expect.您的程序中可能存在其他逻辑错误,这可能是没有得到您期望的 output 的原因。

Also instead of creating/initializing the string number using string number = "";也不是使用string number = "";创建/初始化字符串number , you should use ,你应该使用

string number;//no need to write = ""

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

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