Home > Back-end >  C series
C series

Time:11-26


Topic as shown in figure, my code to run an error
What are the major problems for bosses I code, how to modify the
If you want to meet the requirements of the subject time and capacity and how to write
 
# include & lt; Iostream>
# include & lt; String>
using namespace std;

Int search (int n, int, int, int * s)
{
For (int x=R; X & lt;=n; X++)//the products of the first two, if for a digit, it is the value of the project; If for double-digit, ten for project, after the bits for a,
{
If (s/x - 2 * s [1] x & lt; 10)
{
S=s [x] [x - 2] * s [x - 1);
}
The else
{
S=s [x] [x - 2] * s [1] x/10;
S=s [x + 1] [2] x [1] x % * s 10;
}
}
cout return 0;
}

Int main ()
{
int k;
Cin & gt;> k;//the first line is an integer K, said the number of the sample,
//char a, b;//the first line of each sample are three integers a, b, Q (000) 1 Q 1 or less, or less, including Q said query the number of times,
Int Q, n;//output value of the sequence of the first n elements (1 n or less 1 or less, 000, 000, 000)
For (k; K & gt; 0; K -)
{
Int size=2;
Int * s=new int [size];
Int R=2;
Cin & gt;> S [0] & gt;> S [1] & gt;> Q;//the first value is a, the second value of b, (0 or less a, b or less 9)
for (int i=1; i <=Q; I++)
{
getchar();
Cin & gt;> n;
Size=Max (size, n);
Search (n, I, R, s);
R=Max (R, n);
}
The delete [] s.
}
system("pause");
return 0;
}

CodePudding user response:

Is access to cross-border, which is beyond the array,

CodePudding user response:

1. The line 16, s [x + 1], when x=n, have cross-border (because line 32 distribution n space only)
2. When n=1000000000, your line 39 size=n, then 32 rows, assign a n the size of the array, a int 4 bytes, need 4 G space, array size is too big
3. Even out an array of 4 G space, line 7, you traverse a n, time had exceeded 3000 ms (a command even 20 ns, cycle of 10 ^ 9 times, 20 seconds, and cycle time more than 20 ns)

Using a brute force search doesn't work, change the idea,

I played in front of dozens of data:
2, 3, 6, 1, 8, 8, 6, 4, 2, 4 , 8, 3, 2, 6, 1, 2, and 2, 4 , 8, 3, 2, 6, 1, 2, and 2, 4 , 8, 3, 2, 6, 1, 2, and 2, 4 , 8, 3, 2, 6, 1, 2, and 2, 4 , 8, 3, 2, 6, 1, 2, 2, 4
Observation data you can find, repeated, marked off (red) paragraphs 9 to 16 a cycle, followed by repeated,
That is to say this is a periodic function: cycle is 8

Other starting value also is a periodic function (that you use a computer is very easy to prove that because only 9 * 9=81, enumeration out line - of course title inside didn't ask you to prove)

So, this problem was converted to:
1. Looking for a cycle,
2, with modular arithmetic: n % cycle
3, the start to the first cycle of initial sequence x0
4. According to the three steps above, with the final result of general term formula to calculate the

To complete these, absolutely within the time and space requirements of you