c++ - 在 C++ 中读入数组

标签 c++ arrays file-io

我正在为比赛练习(这就是 my previous question 的来源)。 我为这个问题整理出了算法,但我在实际编程中遇到了一些问题。这是一场单人比赛,所以我真的需要在参加之前解决这个问题。这就是问题。

TASK 3: GECKO During the rainy season, one of the walls in the house is infested with mosquitoes. The wall is covered by h × w square tiles, where there are h rows of tiles from top to bottom, and w columns of tiles from left to right. Each tile has 1 to 1000 mosquitoes resting on it. A gecko wants to eat as many mosquitoes as possible, subject to the following restrictions. It starts by choosing any tile in the top row, and eats the mosquitoes in that tile. Then, it moves to a tile in the next lower row, eats the mosquitoes on the tile, and so on until it reaches the floor. When it moves from one tile to a tile in the next lower row, it can only move vertically down or diagonally to the left or right (see Figure 1). Given the values of h and w, and the number of mosquitoes resting on each tile, write a program to compute the maximum possible number of mosquitoes the gecko can eat in one single trip from the top to the bottom of the wall.

An example input file would be:
Example

6 5
3 1 7 4 2
2 1 3 1 1
1 2 1 1 8
2 2 1 5 3
2 1 4 4 4
5 7 2 5 1

我遇到的问题是阅读数字(或问题列表的顶部)。 我当前的读入代码是:

ifstream read;
read.open("input.txt");
write.open("output.txt");
int width, height, wall[500][500];
read >> height;
read >> width;
for ( int count1 = 0; count1 < height; count1++)
{
   for ( int count2 = 0; count2 < width; count2++)
      { 
         read >> wall[count1][count2];
       }
}

当我用 cout 测试它打印所有读入的数字时,我得到的只是乱码。 现在我无法发现任何错误,任何人都可以看到问题吗?谢谢。 (固定)再次感谢。

我测试了读入,现在很完美。但是,我得到的苍蝇数量仍然减少。例如输入
1 1
23
应该给输出 23,但我得到 0 作为输出。 这是我的代码:

int h = 0, w = 0, compare1, compare2, compare3, currentInt;
for ( int count3=( height-2 ); count3 >= 0; count3--)
{  
   for( int count4 = 0; count4 < width; count4++)
     {
     h = count3;
     w = count4;
      currentInt = wall[h][w];   // read in affected integers.
      if( w != 0 )
       { compare1 = wall[h+1][w-1];
       }
       compare2 = wall[h+1][w];
       compare3 = wall[h+1][w+1]; 
       if( w!= 0)                     // Compare and replace. 
       {  
          if((( currentInt + compare1) >=(currentInt + compare2)) && ((currentInt + compare1)>=(currentInt + compare3)))
          { 
             wall[h][w] = ( currentInt+compare1);
          }
          if((( currentInt + compare2) >(currentInt + compare1)) && ((currentInt + compare2)>(currentInt + compare3)))
          { 
             wall[h][w] = ( currentInt+compare2);
          }
          if((( currentInt + compare3) >=(currentInt + compare2)) && ((currentInt + compare1)>(currentInt + compare1)))
          { 
             wall[h][w] = ( currentInt + compare3);
          }
       }
       else
       { 
          if ((currentInt + compare2) >= ( currentInt + compare3))
          {
             wall[h][w] = ( currentInt + compare2);
          }
          else
          {
             wall[h][w] = ( currentInt + compare3);
          }
       }
      }
}
int maxFlies=0;
for (int count5 = 1; count5 < width;count5++)
   {
   maxFlies = wall[0][0] ;
   if ( maxFlies < wall[0][count5])
   { maxFlies = wall[0][count5] ; }
   }
write<<maxFlies<<endl;
read.close();
write.close();
return 0;

我想用它做的基本上是:从倒数第二行读入。将它添加到下面它可以到达的唯一 block 。然后向上移动,最后测试最上面一行的哪一个是最大的整数。感谢您的快速回复。

最佳答案

二维数组在 C++ 中不能作为 Array[alpha,beta] 访问。

它以 Array[alpha][beta] 的形式完成。

关于c++ - 在 C++ 中读入数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/525625/

相关文章:

c++ - 循环C++中的段错误Openmp

c++ - 读取字符到(字符指针)

c++ - 无法为非 root 用户加载 libsctp.so

javascript - 如何将 Angular ui-grid 与字符串数组一起使用

更改文件

c++ - 有条件地编译为 Win32 GUI 或 Linux 控制台应用程序

javascript - 从数组中删除匹配的字符串

javascript - 如何在javascript中按时间对对象数组进行排序

java - Java 中的文件 I/O

java - 在 Windows 和 Linux 操作系统下访问 Java 应用程序中的外部文件