【问题标题】:what changes should i have to do to in my cpp code to get correct output?我应该对我的 cpp 代码进行哪些更改才能获得正确的输出?
【发布时间】:2021-03-10 11:39:50
【问题描述】:
//Question

/*一排有N个座位。给你一个长度为 N 的字符串 S;对于每个有效的 i,如果第 i 个座位是空的,则 S 的第 i 个字符为“0”,如果该座位上有人坐在,则为“1”。

如果两个人坐在一起,他们就是朋友。两个朋友总是属于同一群朋友。你能找到组的总数吗?

输入

输入的第一行包含一个整数 T,表示测试用例的数量。 T 测试用例的描述如下。 每个测试用例的第一行也是唯一一行包含一个字符串 S。

输出

对于每个测试用例,打印一行包含一个整数 - 组数。*/

//我的代码

#include <iostream>
#include <string>
using namespace std;
int main() {
    int t;
    cin>>t;
    int n=1e6;
    
    for(int i=0;i<t;i++){
        string g1;
        cin>>g1;
        int group;
        group = 0;
        for(int j=0;j<g1.length();j++){
            if(g1[j] == '1'){
                for(int h=1;h<n;h++){
                if(g1[j+h] == '1'){
                    h++;
                }else{
                    break;
                }
                group++;
                }
            } else{
                continue;
            }   
             
        }
        cout<<group<<endl;
    }
        return 0;}
 

示例输入

4

000

010

101

01011011011110

示例输出

0 1 2 4

//我的输出

0

0

0

9

【问题讨论】:

  • 我建议您对失败的测试用例进行硬编码,并在调试器中逐步检查代码以查看发生了什么以及它与您的预期有何不同。

标签: c++


【解决方案1】:

根据样本输出,您假设在零之间计算“1”,即您拥有的组数。这是您的实现,稍作修正即可。

#include <iostream>
#include <string>
using namespace std;
int main() {
    int t;
    cin >> t;
   //    int n = 1e6;  --> not used

   for (int i = 0; i < t; i++) {
       string g1;
       cin >> g1;
       int group;
       group = 0;
       for (size_t j = 0; j < g1.length(); j++) {
           if (g1[j] == '1') {
               group++;
               //skip all '1' before the first '0'
               while (g1[j] == '1' && j < g1.length())
                   j++;
        }
        else {
            continue;
        }
    }
    cout << group << endl;
}
return 0;

}

【讨论】:

  • 感谢您的回答,但请告诉我我的代码 valeca 中的错误
  • 我理解的方式是,您需要计算字符串有多少组'1'。描述不清楚(对我来说),所以我只是更新了一些你的实现来计算它们。
  • 你理解正确,你的实现也是正确的,但请你检查我的实现并告诉我实现中的错误或建议我这样做的方法。-valeca
  • 我在你的代码中放了一堆 cmets 来解释需要改变什么以及为什么。删除 cmets 会起作用,但我建议您添加一些更改以改进所提供实现的逻辑。在下面查看我的答案,因为评论不允许发布。
【解决方案2】:
// my code
#include <iostream>
#include <string>
using namespace std;
int main() {
    int t;
    cin >> t;
    // you don't need n variable
    // it is appropriate to use the length of the string instead
    // it also will remove one warning for initialization
    int n = 1e6; 

    for (int i = 0; i < t; i++) {
        string g1;
        cin >> g1;
        int group; // you can use int group = 0; avoiding the statement below
        group = 0;
        // size_t stringLength = g1.length();
        for (int j = 0; j < g1.length(); j++) {
            if (g1[j] == '1') {
                group++;  // you need to add it here to starts counting
                // better this way -> for (size_t h = 1; h < stringLength; h++) 
                for (int h = 1; h < n; h++) { // h < n && (j+h)<stringLength
                    if (g1[j + h] == '1') {
                        // you increasing h value twice - first in for statement, second here
                        // instead, you need to set j to j+h value 
                        //h++; 
                        // you just "moving" through the string till next'0'
                        j = j + h;
                    }
                    else {
                        break;
                    }
                // this will increase group count for each next '1' in the string
                // this is why you got 9 for the last string in your's example
                //group++;
            }
        }
        // this is not needed
        //else {
        //    continue;
        //}
    }
    cout << group << endl;
}
return 0;

}

【讨论】:

    猜你喜欢
    • 2022-01-03
    • 2019-07-12
    • 2022-01-05
    • 2011-12-04
    • 2016-06-14
    • 1970-01-01
    • 2020-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多