gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 08:33:59 25 4
gpt4 key购买 nike

我正在为比赛练习(这就是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com