如果条件计算字符串中的 "1" 位数字未执行
if conditional for counting "1" digits in a string not executing
我必须将二进制字符串的每一位与1进行比较;如果位为1,则计数器增加1。
#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
int main(int argc, char const *argv[])
{
int n, d;
cin >> n >> d;
string a1("1");
int count = 0;
int sum[1000];
string a[1000];
for (int i = 0; i < d; i++) {
cin >> a[i];
for (int j = 0; j < n; j++) {
if (a[j].compare(a1) == 0) {
count++;
}
}
sum[i] = count;
cout << sum[i] << endl;
}
int kill = 0;
for (int i = 0; i < d; i++) {
if (sum[i] == 0) {
kill++;
} else if (sum[i] == sum[i + 1]) {
kill++;
}
}
//cout<<kill<<endl;
return 0;
}
我已经写了这个逻辑,但是if
条件不工作,因为它应该在a[j]
为1时执行,但它不是。
我怎样才能正确地做到这一点?
试着用strcmp()
代替compare
。
if (strcmp(a[j], a1) == 0)
{
count++;
}
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++,系统无法执行指定的程序
- 检查输入是否不是整数或数字
- 使用C++中的模板和运算符重载执行矩阵运算
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何用数字处理log(0)
- 如何通过使用 2 位或更多数字的 XOR 运算符来执行此操作C++问题
- 如何执行此程序,将键入的数字显示为数字值的多次
- 递归推动POP POSTFIX计算器无法正确执行操作,只需将最后一个数字作为结果
- 如何在包含数字的.bmp(二进制图像)中执行组件标记9 4 3 6
- 程序编译并执行,但输出的不合理大的数字作为最大值.如何显示长双倍?
- C++:如何对文本文件中的每个数字执行操作
- 为什么我的 c++ 计算器每减去我输入的两个数字,而不是执行我要求它执行的操作
- C++函数,它接受多个数字并执行不同的任务
- c++随机数生成器:如何在每次执行时使数字随机
- 如果条件计算字符串中的 "1" 位数字未执行
- 是否可以将数组数字作为函数来执行?