for(int i = 0; i < A.size(); i++){
if(mp.find(A[i]) == mp.end()){
if(mp.find(B-A[i]) == mp.end())
mp[B-A[i]] = i;
}
Why are these three lines of code used here? explain in detail please
complete solution link
Related
In Copper Nickel Mine (Cloud) Simulation, In MinePanel Agent, there is a function Called setupTunnelLayout.
The original code in the above function is as following:
//create corridor of already empty rooms
`RoomBlock emptyRoom;
for ( int j = 0; j < nColumns; j++ ) {
emptyRoom = add_roomBlocks();
emptyRoom.jumpToCell( 0, j );
if ( j == nColumns - 1 )
emptyRoom.isStartBlock = true;
emptyRoom.isTunnel = true;
}`
But in my scenario all the cells are accessible initially, so all can be tunnel (path to move), not only the (0, j) row as the above example!
I was thinking I can change it as the followings; (1) or (2);
(1)
//create corridor of already empty rooms
RoomBlock emptyRoom;
for ( int j = 0; j < nColumns; j++ )
for (int i = 0; i < nRows; i++){
emptyRoom = add_roomBlocks();
emptyRoom.jumpToCell( 0, j );
emptyRoom.jumpToCell( i, 0 );
if ( j == nColumns - 1 )
emptyRoom.isStartBlock = true;
emptyRoom.isTunnel = true;
}
Or it can be like this;
(2)
//create corridor of already empty rooms
`RoomBlock emptyRoom;
for ( int j = 0; j < nColumns; j++ )
for (int i = 0; i < nRows; i++){
emptyRoom = add_roomBlocks();
emptyRoom.jumpToCell( i, j );
if ( j == nColumns - 1 )
emptyRoom.isStartBlock = true;
emptyRoom.isTunnel = true;
}`
'Can you please let me know if (1) or (2) are correct? Which one is preferred?
Thank you so much,
Neda.'
The second is preferable; there is no point jumping the agent to the column and then the row when you can directly move it to the exact cell coordinates.
In both cases though you are missing a closing } for your extra i loop (and you're missing an opening one in your second case).
//create corridor of already empty rooms
RoomBlock emptyRoom;
for (int j = 0; j < nColumns; j++) {
for (int i = 0; i < nRows; i++) {
emptyRoom = add_roomBlocks();
emptyRoom.jumpToCell(i, j);
if (j == nColumns - 1) {
emptyRoom.isStartBlock = true;
}
emptyRoom.isTunnel = true;
}
}
[I used the StackOverflow formatting to mark the full block as Java code. It's also best-practice (although AnyLogic doesn't do it many of their example models) to always use curly brackets for loops, etc. even if their body only has one line.]
Plus whether the change will do what you want or not obviously depends on the rest of the model and how it handles the grid of cells and tunnels.
Today, when trying quicksort, instead of taking last element as pivot and partitioning,i took the first element as pivot, But it is not producing the correct partitioned output.
int pivot = ar[0];
int pindex = 0;
for(int i = 0;i < ar.size();i++)
{
if(ar[i] <= pivot)
{
swap(ar[i],ar[pindex]);
pindex++;
}
}
swap(ar[pindex],ar[ar.size()-1]);
I could not understand why, i always use this for partition, but this is not working when i take first element as partition.
But this worked even if i took first element as partition
int i, j, pivot, temp;
pivot = ar[0];
i = 0;
j = ar.size()-1;
while(1)
{
while(ar[i] < pivot && ar[i] != pivot)
i++;
while(ar[j] > pivot && ar[j] != pivot)
j--;
if(i < j)
{
temp = ar[i];
ar[i] = ar[j];
ar[j] = temp;
}
else
{
break;
}
}
What are the differences between them.
At last found that, this method is Hoare's partition method, where as the typical quick sort method we all follow is lomuto's partition.
See this wiki page, it has all details https://en.wikipedia.org/wiki/Quicksort
I'm trying to scan a file that contains 13 ints which are to be stored in 13 variables. Is there a way to loop over this, while skipping the i-th element? I'm anticipating there might be a solution, which have yet eluded me, perhaps similar to the code below:
int i;
for (i = 0; i < 13; i++)
fscanf(file, %d, &variables[i]); // somehow apply i to %d
instead of the obvious but lengthy and unclean:
fscanf(file, %d, &variable1);
fscanf(file, %*d, %d, &variable2);
fscanf(file, %*d %*d, %d, &variable3); // etc
thanks
int *variables[] = { &variable1, &variable2, &variable3, ... };
for (int i = 0; i < 13; i++) {
fscanf(file, "%d", variables[i]);
}
On coffeescript loop 'for'
eg.
if 1 < x, code like below:
console.debug i for i in [1..0]
Generated code is:
var i;
for (i = 1; i >= 0; i--) {
console.debug(i);
}
if 1 > x,code like below:
console.debug i for i in [1..2]
Generated code is:
var i;
for (i = 1; i <= 2; i++) {
console.debug(i);
}
If i want write that javascript.How to ?
for(var i=1;i<=0;i++){
console.debug(i);
}
Because i don't know the condition is greater than left side or less than left side.
But i just want it i++
What's wrong with me?
EDIT BELOW:
For coffeescript's feature,I add condition before the loop or add condition on for loop.
eg:
if x - y >=1
console.debug i for i in [1..x-y]
or
console.debug i for i in [1..x-y] and x-y >=1
That's my way.Some one have good advice?
It looks like you want to do this:
console.debug i for i in [1..x-y] by 1
Which gets compiled to:
var i, _i, _ref;
for (i = _i = 1, _ref = x - y; _i <= _ref; i = _i += 1) {
console.debug(i);
}
for(var i=1;i<=0;i++){
console.debug(i);
}
is equivalent to
var i = 1;
while(true) {
console.debug(i);
i++;
}
which in coffeescript is written as
i = 1
while true
console.debug(i);
i++;
I have followed the tutorial:
www.edumobile.org/iphone/iphone-programming-tutorials/a-simple-stopwatch-for-iphone
and I get 1 error and 1 warning, both on the same line 71
for (int i = [timeArray count] – 1; i >= 0; i–) {
error – a parse issue Expected )
warning – Unused entity issue Expression result unused
Any ideas what is wrong?
Change this,
for (int i = [timeArray count] – 1; i >= 0; i–) {
to,
for (int i = [timeArray count] – 1; i >= 0; i--) {
Compiler is saying that it is not able to parse the character '–'. If it is not able to recognize the for loop syntax and parse it, it will throw this error.
As ACB mentioned, the expression needs to be i-- instead of i-.
Just a couple of notes - Douglas Crawford actually recommends to avoid using -- and ++ in favor of doing i -= 1. While a smidgen verbose, there is no room for doubt over what it actually does versus something like
int example = --i + b;
may confuse some to the value of i after the end of the expression.
Also, as a minor optimization, you should put the size of the array in a local value as opposed to calling [timeArray count] every loop iteration
int timeArraySize = [timeArray count] - 1;
for (int i = timeArraySize; i >= 0; i -= 1) {
Hope that helps!