小投入,大输出
Small input, large output?
本文关键字:输出 更新时间:2023-10-16
我对我写的这个短代码有一个问题:
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
int main() {
ofstream fout("hps.out");
ifstream fin("hps.in");
int N;
fin >> N;
int a1, a2, b1, b2, c1, c2 = 0;
for (int i = 0; i < N; i++) {
int x, y;
fin >> x;
fin >> y;
if (x == 1 && y == 2) {
a1++;
}
if (x == 1 && y == 3) {
a2++;
}
if (x == 2 && y == 1) {
b1++;
}
if (x == 2 && y == 3) {
b2++;
}
if (x == 3 && y == 1) {
c1++;
}
if (x == 3 && y == 2) {
c2++;
}
}
fout << a1 << " " << a2 << " " << b1 << " " << b2 << " " << c1 << " " <<
c2 << " " << 'n';
return 0;
}
所以这是输入:
5
1 2
2 2
1 3
1 1
3 2
这是输出:
32768 4197767 0 616536480 0 1
我想做的是计算 (1,2(、(1,3(、(2,1(、(2,3(、(3,1( 和 (3,3( 的对数,并将这些值存储在变量 a1、a2、b1、b2、c1、c2 中。然而,由于某种原因,我得到了这些巨大的数字,我不明白为什么。有什么东西溢出了吗?
这个问题的声明实际上是USACO青铜Jjanurary #2:
http://www.usaco.org/index.php?page=viewproblem2&cpid=688
我将不胜感激任何帮助!
问题出在这条线上:
int a1, a2, b1, b2, c1, c2 = 0;
这仅将 c2 初始化为零。其余的保存垃圾值。您需要为每个变量执行= 0
。
相关文章:
- 递归函数计算序列中的平方和(并输出过程)
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 请解释"函数1(p1,p2,p3);"的输出
- C++:将控制台输出存储在宏中更好吗
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 为什么我的代码在输出中增加了93天
- 如何从void函数输出字符串
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- AES加密到解密未正确输出
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- C++格式化输出问题
- 将值从二维数组输出到文本文件
- 集合上的输出迭代器:assign和increment迭代器
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- 为什么rk4.do_step不输出C++中的初始条件
- 输出没有重复元素的动态数组(收缩数组)C++
- 如何根据单词在文本中出现的概率输出单词