c++ 为什么这段代码有一个运行时错误(OutOfBounds);)[关闭]

wf82jlnq  于 2024-01-09  发布在  其他
关注(0)|答案(1)|浏览(150)

**已关闭。**此问题需要debugging details。目前不接受回答。

编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答问题。
5天前关闭。
Improve this question
为什么这段代码有运行时错误(OutOfBounds);)
这是USACO 2023年12月竞赛,铜牌1题
这里是问题链接https://www.usaco.org/index.php?page=viewproblem2&cpid=1347#

  1. #include <iostream>
  2. using namespace std;
  3. long long n_h[20009]; // cow height
  4. long long m_h[20009]; // cane height
  5. long long space_h[20009]; // 사탕수수 아래 빈 공간 크기
  6. long long caneat;
  7. int main()
  8. {
  9. ios_base::sync_with_stdio(false);
  10. cin.tie(NULL);
  11. cout.tie(NULL);
  12. long long n, m; // cow num // cane num
  13. cin >> n >> m;
  14. for(int i = 0; i < n; i++){
  15. cin >> n_h[i];
  16. }
  17. for(int i = 0; i < m; i++){
  18. cin >> m_h[i];
  19. }
  20. for(int i = 0; i < m; i++){
  21. for(int j = 0; j < n; j++){
  22. if(m_h[i] <= 0){
  23. break;
  24. }
  25. if(m_h[i] + space_h[i] <= n_h[j]){ // 키가 작으면 먹지 못함
  26. n_h[j] = n_h[j] + m_h[i]; // 소의 키 키우기
  27. m_h[i] = -1;
  28. break;
  29. }
  30. else{
  31. if(space_h[i] < n_h[j]){
  32. caneat = 0;
  33. caneat = n_h[j] - space_h[i];
  34. n_h[j] = n_h[j] + caneat;
  35. space_h[i] = space_h[i] + caneat;
  36. m_h[i] = m_h[i] - caneat;
  37. }
  38. }
  39. }
  40. }
  41. for(int i = 0; i < n; i++){
  42. cout << n_h[i] << "\n";
  43. }
  44. return 0;
  45. }

字符串

ztyzrc3y

ztyzrc3y1#

他不是一个程序员,也不需要重复使用代码。我同意竞争性编程不是学习编程的好方法。但通常竞争性编程的目的是爱好,学习算法,获得奖项,而不是学习工程。
你的数组的大小是20,009,比200,000小,n,m的限制。只需要再加一个零。

  1. long long n_h[200009]; // cow height
  2. long long m_h[200009]; // cane height
  3. long long space_h[200009]; // 사탕수수 아래 빈 공간 크기

字符串
也许你必须在在线评委网站的问题板上问这个问题。无论如何,这里不是问算法问题的最佳地方。

相关问题