Ich versuche, ein Problem in Code Forces zu lösen - http://codeforces.com/problemset/problem/680/B. Ich habe es bereits lokal gelöst, aber wenn ich es auf Code Forces hochlade, gibt es unterschiedliche Ausgaben.C-Code gibt verschiedene Ausgabe
Zeit, dies ist mein Code:
#include <stdio.h>
int main()
{
int q, pos;
scanf("%i %i", &q, &pos);
int cities[q];
int criminal_count = 0;
//the greatest distance is naturally the number of cities
int criminals_by_dist[q];
for (int i = 0; i < q; ++i)
criminals_by_dist[i] = 0;
for (int i = 0; i < q; ++i)
scanf("%i", &cities[i]);
//now we have the cites, lets count
//first the centre
if (cities[pos - 1] > 0)
criminals_by_dist[0]++;
int l = 0, r = 0;
for (int i = 1; i < q; ++i)
{
//count how many on the left of current position
//first check if it is not out of range
l = pos - i;
if (l >= 0)
criminals_by_dist[i] += cities[l - 1];
//same with the right
//first check if it is not out of range
r = pos + i;
if (r < q)
criminals_by_dist[i] += cities[r - 1];
}
//count how many criminals can be secured in a particular city
//the centre is always confirmed because there is only one centre
criminal_count += criminals_by_dist[0];
int current = 0;
for (int i = 1; i < q; ++i)
{
current = criminals_by_dist[i];
if ((current == 2 || (pos - i - 1 >= 0 != pos + i - 1 < q)))
criminal_count += current;
}
printf("%i", criminal_count);
return 0;
}
In meiner Konsole, ich geben Sie die folgende Eingabe:
6 3
1 1 1 0 1 0
und der Ausgang ist:
3
jedoch in Codeforces passiert folgendes:
Eingang
6 3
1 1 1 0 1 0
Ausgabe
1998776724
Antwort
3
Es ist alles der gleiche Code. Warum passiert das?
Bitte erläutern Sie, was Ihr Code tun soll. – Yunnosch
'if (l> = 0) verbrecher_by_dist [i] + = städte [l-1];' ist falsch. Was ist wenn 'l == 0'? Was ist dann "l - 1"? Das Gleiche gilt für "r" später in der gleichen Schleife. –
'if (l> = 0) crafters_by_dist [i] + = cities [l-1];' kann zu einem Zugriff außerhalb des Rahmens führen. –