Finding how many rows needed given number of buttons per row? - c++

I want to layout X buttons.
At the start, Y items can be in a row.
After the first row is laid out, only Y - 1 items can appear in the next row and so on.
So say I have 13 buttons and the first row can have up to 6 buttons, I would need 3 rows. The first would have 6 buttons the second 5 buttons, and the 3ed 2 buttons.
Thanks
What algorithm could be to do:
int getRowCount(int startCols, int numItems);
I know how to do it with MOD if the number of columns is constant but how would you do it if the maximum number of columns decreases with each row?

In situations like this, I try to translate the english into code.
int getRowCount(int startCols, int numItems) {
int currentCols = startCols;
int numRows = 0;
while (numItems > 0) { // as long as items remain
numRows += 1; // add another row
numItems -= currentCols; // reduce the remaining items by the current number of columns
currentCols--; // reduce the number of columns by one
}
}
It's always best to run through the scenario with some edge cases. Ask yourself questions like:
What answer do I get if numItems is 0?
What answer do I get if startCols is 0?
What answer do I get if numItems == startCols?

Related

Why is the time complexity of this problem only consider the previous recursive call and not the entire problem?

Here we have a box that is 4 * 7 and it can be filled with rectangles that are either 1 * 2 or 2 * 1. This depiction is from the book Competitive Programmer's Handbook.
To solve this problem most efficiently, the book mentions using the parts that can be in a particular row:
Since there are 4 things in this set, the maximum unique rows we can have is 4^m, where m is the number of columns. From each constructed row, we construct the next row such that it is valid. Valid means we cannot have vertical fragments out of order. Only if all vertical "caps" in the top row correspond to vertical "cups" in the bottom row and vice versa is the solution valid. (Obviously for the horizontal fragments, their construction is restricted in row creation itself, so it is not possible for there to be inter-row discrepancy here.)
The book then says this:
Since a row consists of m characters and there are four choices for
each character, the number of distinct rows is at most 4^m. Thus, the
time complexity of the solution is O(n4^{2m}) because we can go through
the O(4^m) possible states for each row, and for each state, there are
O(4^m) possible states for the previous row.
Everything is fine until the last phrase, "there are O(4^m) possible states for the previous row." Why do we only consider the previous row? There are more rows, and this time complexity should consider the entire problem, not just the previous row, right?
Here is my ad hoc C++ implementation for 2 by n matrix, which would not work in this case, but I was trying to abstract it:
int ways[251];
int f(int n){
if (ways[n] != 1) return ways[n];
return (ways[n] = f(n-1) + f(n-2));
}
int main(){
ways[0] = 1;
ways[1] = 1;
for (int i = 2; i <= 250; i++){
ways[i] = -1;
cout << f(250) << '\n';
}
}

C++ console - Formatting output

I have a nxn grid of randomly generated numbers. I have a label that displays the element numbers for the X and Y axis:
It aligns up properly for single digit numbers, but when the grid size increases the labels become out of proportion and don't line up like so:
I'm wondering if there is any way to make the label line up with the numbers within the grid and scales depending on the size. Is this even possible?
You can use this to determine the number of digits in an integer:
int getDigits(int col)
{
int len = 1;
while ( col/= 10 )
{
len++;
}
return len;
}
With this you can determine the number of " " to print as you loop through.
std::string space(getDigits(column), ' ');
std::cout<<space<<num;
You could use two rows for the top numbering and work out a simple calculation to determine whether or not to display the digit for that row.
For instance, the top row could be for the 'tens' column and the second row for the units. You know you have 20 columns, so you could display a space character or zero if the current count of a loop is less than 10. If it's not less than 10, display a 1 character.
The bottom row could then just cycle through 0 - 9 twice.

MFC List control and adding array to second column

I have a problem with adding an array in to a second column of list contol. The problem is, that the number of filled rows in the second column is the same as the number of filled rows in the first column. My code is :
enter code here
for (int i=0; i<velikost; i++) // velikost = 10 (velikost is size)
{
niz.Format(_T("%d"),polje[i]);
m_listCtrl.InsertItem(i,niz);
}
polje_stevcev = new int[max]; // new array with size of max number
for(int i=0; i<max + 1; i++) // lets say the max is 90
{
polje_stevcev[i] = 0;
niz2.Format(_T("%d"), polje_stevcev[i]);
m_listCtrl.SetItemText(i,1,niz2);
}`enter code here`
We see in the second for loop that the loop is performed 90 times, but in list control in in second column show me only first 10 rows, others are empty.
Picture (in second column should be 95 rows with number 0, but shows only 10) : Here is a screenshot
Thanks.

I can't think of a good algorithm [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
There is a magic board. The magic board has N*N cells: N rows and N columns. Every cell contains one integer, which is 0 initially. Let the rows and the columns be numbered from 1 to N.
There are 2 types of operations can be applied to the magic board:
•RowSet i x: it means that all integers in the cells on row i have been changed to x after this operation.
•ColSet i x: it means that all integers in the cells on column i have been changed to x after this operation.
And your friend sometimes has an interest in the total number of the integers 0s on some row or column:
•RowQuery i: it means that you should answer the total number of 0s on row i.
•ColQuery i: it means that you should answer the total number of 0s on column i.
Input
The first line of input contains 2 space-separated integers N and Q. They indicate the size of the magic board, and the total number of operations and queries from the friend.
Then each of the next Q lines contains an operation or a query by the format mentioned above.
Output
For each query, output the answer of the query.
Constraints
1 ≤ N, Q ≤ 500000 (5 * 105)
1 ≤ i ≤ N
x ∈ {0, 1} (That is, x = 0 or 1)
Sample Input:
3 6
RowQuery 1
ColSet 1 1
RowQuery 1
ColQuery 1
RowSet 1 0
ColQuery 1
Output:
3
2
0
1
How to go about it? The time constraint is 0.6 seconds and therefore the naïve algorithm of marking out the operations on a 2D array, wont work.
If you can't think of a good algorithm, try this technique:
Run an example using pencil and graph paper.
Run an example again, this time write down each detailed step you
perform.
Run an example again, using your steps. Adjust as necessary.
Convert your steps into code.
Using this technique, you can come up with more appropriate questions to search on Stackoverflow, such as "How do I implement a square area of memory / matrix?"
Or "How do I use a debugger?"
Or "Here is the smallest program that recreates my issue of ...., what am I doing wrong?"
Edit 1: (to advance my S.O. reputation)
Looks like, from the requirements, that you will need at least two functions: Set a row to the given value or set a column to the given value.
Let us start with something small like a 4x4 matrix.
And use the command: Set Row 1 0 // Set Row 1 to all zeros.
Remember that C++ indexes from 0 to N-1 not 1 to N as the requirements are, so we'll have to subtract one from our row number.
Let us use the notation: board[row][column] to represent a cell on the board.
By hand:
board[0][0] = 0;
board[0][1] = 0; // Note the incrementing column numbers.
board[0][2] = 0;
board[0][3] = 0; // Note the last column index is 3 not 4.
Looking at the above code, we can note a pattern, namely, the column index is changing each time, by 1. So we can put this into a loop:
Set column to zero.
While column is less than 4 do:
board[0][column] = 0;
column = column + 1;
end-while
The next step is to turn this into some code:
unsigned int column;
unsigned int board[4][4];
for (column = 0; column < 4; ++column)
{
board[0][column] = 0;
}
Since the the Set Row command allows for a variable row index and a variable row value, we make those variables and insert them into our code:
unsigned int row = 0;
unsigned int value = 0;
unsigned int column;
unsigned int board[4][4];
for (column = 1; column < 4; ++column)
{
board[row][column] = value;
}
We could make this into an free standing function by providing a function signature:
void Set_Row(unsigned int& array[4][4],
unsigned int row,
unsigned int value)
{
// Insert above code fragment here.
}
Next, make functions for the other commands.
Create a main function to read the commands.
Run the program, notice where any issues are, such as being able to declare a matrix of any size at run-time.
Add in code to resolve the issues.
Repeat.

Number of Items in a Column inside a grid

How do you find number of items in a column inside a grid?
I have a grid (listview control to be specific), and have some items.
Some times a given row might not be full. ANd can have values in fewer than maximum columns. I need to find Number of items in a given Column.
If the grid is like
1 2 3
4 5 6
7
and if input column is 1, then we need to output 3, and 2 for input of 2 or 3.
I have variables to for ItemCount, CoulmnCount and RowCount which track number of items, rows and columns.
A very rudimentar way would be something like this:
int iItemCount=0,iItemInColumn=0;
for(int iCol=0;iCol<iColumnCount;iCol++)
for(int iRow=0;iRow<iRowCount;iRow++,iItemCount++)
if(iCol==iInputCol && iItemCount<iTotalItems)
iItemInColumn++;
Can you guys think of any sophesticated way, which does not need loops? possible utilizing just 3 variables which I already have for tracking?
Assuming 0-based indexes:
def itemsInColumn(itemCount, columnCount, inputColumn):
lastItemColumn = (itemCount - 1) % columnCount
if inputColumn <= lastItemColumn:
return (itemCount + columnCount - 1) / columnCount
else:
return itemCount / columnCount
It depends on the total number of items (itemCount) and the number of columns (columnCount). It just computes itemCount / columnCount, and rounds up or down depending on whether the input column is less than or equal to the last item's column.
The computation "(itemCount + columnCount - 1) / columnCount" is just a trick for rounding up using integer division. In general, given positive integers a and b: ceil(a / b) = (a + b - 1) div b, where div is integer division.