ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1787. Turn for MEGA

Test 3 WRONG
Posted by Fspace 6 Nov 2013 20:52
#include <iostream>
using namespace std;
int main()
{
int k,n,l=1;
int r=0;
int* a = new int[1];
cin >> k >> n;
cin >> a[0];
for(int i=1;cin >> a[i];i++) l+=1;
for(int i=0;i<n;i++){
    if(i<l) r+=a[i];
    r-=k;
    if(r<0) r=0;
}
cout << r << endl;
system("pause");
return 0;
}

Что не так?

Edited by author 06.11.2013 23:12
Re: Test 3 WRONG
Posted by Noob 6 Nov 2013 22:29
int* a = new int[1];

LOLWUT?
Re: Test 3 WRONG
Posted by Fspace 6 Nov 2013 23:09
This is a dynamic array. The number in [] has no importance. An array is okay

Edited by author 06.11.2013 23:14
Re: Test 3 WRONG
Posted by Leonid 7 Nov 2013 00:29
Fspace wrote 6 November 2013 23:09
The number in [] has no importance.
RTFM:
1. http://en.wikipedia.org/wiki/New_(C++)#Syntax
2. http://www.cplusplus.com/reference/new/operator%20new[]/

BTW, did you came from Java? In C++, there is no garbage collectors, so you must release memory manually to avoid memleaks, by calling delete operator ( in this case - delete[] ).


Edited by author 07.11.2013 00:40
Re: Test 3 WRONG
Posted by Fspace 7 Nov 2013 11:02
All right. Thanks for the information. But I find it hard to understand how I should use it in the program. Please correct my code ..
Re: Test 3 WRONG
Posted by Leonid 7 Nov 2013 12:28
In this case, just don't use pointers. You can declare an array using following syntax:
Typename variableName[ arraySize ];

Exapmle:
int array[ 100 ];
In this example, an array of 100 int elements will be created.

I suppose, that most of problems from archive can be solved without using dynamic memory allocation.


Edited by author 07.11.2013 12:30
Re: Test 3 WRONG
Posted by Fspace 7 Nov 2013 16:05
Thanks for the help
Re: Test 3 WRONG
Posted by Logan`~ 14 Sep 2018 20:33
попробуйте
5 5
0 0 0 0 0