Нахождение макс суммы в ряде

и задачки для интервью.
Borovinskih
Posts: 4
Joined: 09 Jan 2002 10:01

Нахождение макс суммы в ряде

Post by Borovinskih »

по - моему можно так.

int main(int argc, char* argv[])
{
int arr[] = {-3,-1,100,-3,10,-1,-3};
int sum = arr[0];
int maxSum = arr[0];
int maxStart = 0;
int start = 0;
int count = sizeof(arr) / sizeof(arr[0]);

for(int i = 0; i < count; i++)
{
if(sum < 0)
{
sum = arr[i];
start = i;
}
else
{
sum += arr[i];
}

if(maxSum < sum)
{
maxSum = sum;
maxStart = start;
}
}
return 0;
}
Borovinskih
Posts: 4
Joined: 09 Jan 2002 10:01

Нахождение макс суммы в ряде

Post by Borovinskih »

блин, умерло форматирование кода и он стал совершенно не читаемым
AK70
Уже с Приветом
Posts: 3127
Joined: 10 Apr 2001 09:01
Location: MD

Нахождение макс суммы в ряде

Post by AK70 »

<blockquote><font size="1" face="Arial, Verdana, Helvetica, sans-serif">quote:</font><hr>Originally posted by Artem Borovinskih:
<strong>блин, умерло форматирование кода и он стал совершенно не читаемым</strong><hr></blockquote>


your code shouldn't work either [img:1ad7c7b18f]images/smiles/icon_smile.gif[/img:1ad7c7b18f] check with my sequence.
User avatar
tengiz
Уже с Приветом
Posts: 4468
Joined: 21 Sep 2000 09:01
Location: Sammamish, WA

Нахождение макс суммы в ряде

Post by tengiz »

<blockquote><font size="1" face="Arial, Verdana, Helvetica, sans-serif">quote:</font><hr>Originally posted by AK70:
it should be wrong<hr></blockquote>Well, let me put it this way: it's a classic (and very old) programming puzzle and have been known to have a linear solution (which was already published on Privet). It usually takes 10-20 minutes for smart people to crack it... So, why don't you give it another try? [img:424ba538dd]images/smiles/icon_razz.gif[/img:424ba538dd]
AK70
Уже с Приветом
Posts: 3127
Joined: 10 Apr 2001 09:01
Location: MD

Нахождение макс суммы в ряде

Post by AK70 »

<blockquote><font size="1" face="Arial, Verdana, Helvetica, sans-serif">quote:</font><hr>Originally posted by tengiz:
<strong>Well, let me put it this way: it's a classic (and very old) programming puzzle and have been known to have a linear solution (which was already published on Privet). It usually takes 10-20 minutes for smart people to crack it... So, why don't you give it another try? [img:c5141dc400]images/smiles/icon_razz.gif[/img:c5141dc400] </strong><hr></blockquote>

well, I don't like bla-bla, I didn't see the right solution yet. if u have ur own, show. or shut up.
User avatar
Melkor
Уже с Приветом
Posts: 1257
Joined: 03 Oct 2001 09:01
Location: Valinor->Utumno->Angband

Нахождение макс суммы в ряде

Post by Melkor »

<blockquote><font size="1" face="Arial, Verdana, Helvetica, sans-serif">quote:</font><hr>Originally posted by AK70:
<strong>

it should be wrong</strong><hr></blockquote>

В программировании употребление слова "should" обычно означает, что реально имеет место прямо противоположное утверждаемому [img:76d50e12cb]images/smiles/icon_smile.gif[/img:76d50e12cb] . Как и в данном случае, кстати.
clinger
Уже с Приветом
Posts: 276
Joined: 14 Sep 2001 09:01
Location: Donetsk, Ukraine -> Kansas City, MO -> Seattle, WA

Нахождение макс суммы в ряде

Post by clinger »

<blockquote><font size="1" face="Arial, Verdana, Helvetica, sans-serif">quote:</font><hr>Originally posted by AK70:
<strong>check this:
10,-1,100,0,1,-1,10

u see the problem yourself [img:58ed48d4fd]images/smiles/icon_smile.gif[/img:58ed48d4fd]

</strong><hr></blockquote>

Не понял. А в чем проблема? Печатает:

[10, -1, 100, 0, 1, -1, 10]

Или это не верно?

.pl
AK70
Уже с Приветом
Posts: 3127
Joined: 10 Apr 2001 09:01
Location: MD

Нахождение макс суммы в ряде

Post by AK70 »

<blockquote><font size="1" face="Arial, Verdana, Helvetica, sans-serif">quote:</font><hr>Originally posted by clinger:
<strong>

Не понял. А в чем проблема? Печатает:

[10, -1, 100, 0, 1, -1, 10]

Или это не верно?

.pl</strong><hr></blockquote>


well, it prints 100,0,1,-1,10 on my PC.
AK70
Уже с Приветом
Posts: 3127
Joined: 10 Apr 2001 09:01
Location: MD

Нахождение макс суммы в ряде

Post by AK70 »

<blockquote><font size="1" face="Arial, Verdana, Helvetica, sans-serif">quote:</font><hr>Originally posted by Melkor:
<strong>

В программировании употребление слова "should" обычно означает, что реально имеет место прямо противоположное утверждаемому [img:178d451283]images/smiles/icon_smile.gif[/img:178d451283] . Как и в данном случае, кстати.</strong><hr></blockquote>

intuitively, I feel that there must be a linear solution. but I didn't see it yet. which solution above is linear and correct at the same time? [img:178d451283]images/smiles/icon_smile.gif[/img:178d451283]
User avatar
Melkor
Уже с Приветом
Posts: 1257
Joined: 03 Oct 2001 09:01
Location: Valinor->Utumno->Angband

Нахождение макс суммы в ряде

Post by Melkor »

<blockquote><font size="1" face="Arial, Verdana, Helvetica, sans-serif">quote:</font><hr>Originally posted by AK70:
<strong>

intuitively, I feel that there must be a linear solution. but I didn't see it yet. which solution above is linear and correct at the same time? [img:a1560515b1]images/smiles/icon_smile.gif[/img:a1560515b1] </strong><hr></blockquote>

Ну, мое, например, а также clinger'a и Artem'a Borovinskih (по сути, это то же самое решение, только шаг разбиения на подмассивы неявно интегрирован в основной цикл; он, по большому счету, нужен только для исследования задачи, а при реализации его таки можно опустить).
AK70
Уже с Приветом
Posts: 3127
Joined: 10 Apr 2001 09:01
Location: MD

Нахождение макс суммы в ряде

Post by AK70 »

<blockquote><font size="1" face="Arial, Verdana, Helvetica, sans-serif">quote:</font><hr>Originally posted by tengiz:
<strong>Uh-oh, you got angry, my friend! [img:a998810a4f]images/smiles/icon_razz.gif[/img:a998810a4f] You shouldn't have, pal - only losers get angry, right? To see the right solution, just read the thread carefully. Or at least read the classics:
Programming Pearls by Jon Louis Bentley, Addison-Wesley Pub Co; ISBN: 0201657880..</strong><hr></blockquote>


ok, I'll check that book. I didn't open it since I first read it.

and, I wasn't angry, btw [img:a998810a4f]images/smiles/icon_smile.gif[/img:a998810a4f]
User avatar
tengiz
Уже с Приветом
Posts: 4468
Joined: 21 Sep 2000 09:01
Location: Sammamish, WA

Нахождение макс суммы в ряде

Post by tengiz »

<blockquote><font size="1" face="Arial, Verdana, Helvetica, sans-serif">quote:</font><hr>Originally posted by AK70:
well, I don't like bla-bla, I didn't see the right solution yet. if u have ur own, show. or shut up.<hr></blockquote>Uh-oh, you got angry, my friend! [img:901afb0277]images/smiles/icon_razz.gif[/img:901afb0277] You shouldn't have, pal - only losers get angry, right? To see the right solution, just read the thread carefully. Or at least read the classics:
Programming Pearls by Jon Louis Bentley, Addison-Wesley Pub Co; ISBN: 0201657880..
AK70
Уже с Приветом
Posts: 3127
Joined: 10 Apr 2001 09:01
Location: MD

Нахождение макс суммы в ряде

Post by AK70 »

ok, guys, there's a O(N) solution [img:02cd68f327]images/smiles/icon_smile.gif[/img:02cd68f327]

I was wrong. btw, it doesn't need the modified array with M elements

[ 15-01-2002: Message edited by: AK70 ]</p>

Return to “Головоломки”