-1
코드 포스 (Code Forces) - http://codeforces.com/problemset/problem/680/B에서 문제를 해결하려고합니다. 이미 로컬에서 해결했지만 Code Forces에 업로드하면 출력이 달라집니다. 그러나C 코드가 다른 출력을합니다.
3
:
는 다음과 같은 입력#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;
}
내 콘솔에서 내가 입력 :
6 3
1 1 1 0 1 0
출력이
현재이 내 코드입니다 다음 코드가 실행됩니다.
입력
6 3
1 1 1 0 1 0
출력
1998776724
대답
3
그것은 모두 같은 코드입니다. 왜 이런 일이 생길까요?
코드가 수행해야 할 작업을 설명하십시오. – Yunnosch
if (l> = 0) criminals_by_dist [i] + = cities [l-1];이 잘못되었습니다. 'l == 0'이라면? 'l-1'은 무엇입니까? 나중에 같은 루프에있는 'r'과 동일합니다. –
'if (1> = 0) criminals_by_dist [i] + = cities [l-1];'로 인해 접근 범위를 벗어날 수 있습니다. –