Mapping 3 players to a 4 person table [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Say I have a table that goes clockwise like this:
2
1 3
0
This works fine in a 4 person game, but in a 3 person game, the server does not see a 4th chair. Thus, I must map 3 players, one of whom may sit at seat [3] to an array of 3 [2] elements.
So if players were sitting at 2,3,0 then 2 maps to 0, 3 maps to 1 and 0 maps to 2. If the seating was 1, 3, 0 it would map 1 to 0, 3 to 1, and 0 to 2.
What would be a good algorithm to do this if I wanted a std map for this?

Assuming you have an ordered structure of the seats, and making a few assumptions because the details of your implementation are not clear:
std::map<int, int> clockwisePositionMap;
int counter = 0;
for (Seat s : seatList) {
if(s.isOccupied()) {
clockwisePositionMap.insert( pair<int, int>(seat.getNumber(), counter));
counter++;
}
}
Note that this does not match either of your examples; it always gives 0 first precedence, but you can change the for loop as necessary if that is a problem.

Related

How can i find the integers that can be written as a sum of a power of 2, a power of 3 and a power of 5? C++ [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last year.
The community reviewed whether to reopen this question last year and left it closed:
Original close reason(s) were not resolved
Improve this question
So the program must compute all the numbers that can be written as a sum of a power of 2, a power of 3 and a power of 5 below 5.000.000.
For example 42 = 16 + 1 + 25 = 2^4 + 3^0 + 5^2. Any idea how can I do this?
you can get all powers of 2 and all powers of 3 and all powers of 5 under 5.000.000. first Then you can try all combinations
vector<int> solve(){
const int M = 5000000;
vector<int> p_2={1},p_3={1},p_5={1};
while(p_2.back()*2<M)p_2.push_back(p_2.back()*2);
while(p_3.back()*3<M)p_3.push_back(p_3.back()*3);
while(p_5.back()*5<M)p_5.push_back(p_5.back()*5);
set<int> st;//to remove duplicates
for(auto power_of_2 :p_2){
for(auto power_of_3:p_3){
for(auto power_of_5:p_5){
If(power_of_2+power_of_3+power_of_5<M)
st.insert(power_of_2+power_of_3+power_of_5);
}
}
}
return vector<int>(st.begin(),st.end());
}

Storing a given matrix [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have to store the matrix
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
in a variable called "a".I am given the following code block
for(i=1;i<=5;i++)
for(j=1;j<=5;j++)
.................
which I have to finish.I think there should be some connection between the elements of the matrix,but I can't see any.
EDIT:I must not use any additional variables.
You have an array of 5 times 5 integral values; i obviously stands for a row index, whereas j indicates a column index. The respective value in each cell then is (i-1)*5 + j.
But, probably a pitfall introduced by your teacher, be aware that array indizes in c++ start from 0, not 1. And that's why I'm giving more hints than usual for a question without any attempt of solving it:
int arr[5][5];
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
val = // enter the expression here
rowIndex = i-1;
columnIndex = j-1;
// insert the one missing statement here :-)
}
}

Reading int values from a vector in c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a problem to read a large int ( 1 to 10^100 ) into vector the problem is I cannot read it as numeric data-type and split it into the vector so I want a solution to read the number separately into the vector
Example:
45686469
vec[0] = 4
vec[1] = 5
...
vec[7] = 9
Here's one possible way to do it:
std::string yourinput;
cin>>yourinput; //capture your large number as a string
std::vector<char> vch;
for(size_t st=0;st<yourinput.length();++st)
{
vch.push_back(yourinput[st]); //move each character into the vector
}

Reducing Run Time C or C++ [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Can u Guys Please give me tips on how to reduce the compilation time of my c or c++ programmes...
Some basic simple techniques will be helpful.
I was solving a question through a site(https://www.codechef.com/problems/TRISQ)
The Question was :-
What is the maximum number of squares of size 2x2 that can be fit in a right angled isosceles triangle of base B.One side of the square must be parallel to the base of the isosceles triangle.Base is the shortest side of the triangle.
First line contains T, the number of test cases.
Each of the following T lines contains 1 integer B.
Output exactly T lines, each line containing the required answer.
Sample Input
11
1
2
3
4
5
6
7
8
9
10
11
Sample Output
0
0
0
1
1
3
3
6
6
10
10
MY CODE
#include<iostream>
using namespace std;
int main()
{
int T,N,a,i,j;
cin>>T;
while(T--)
{
a=0;
cin>>N;
N=N/2;
N--;
j=N;
for(i=0;i<j;i++)
{
a+=N;
N--;
}
cout<<a<<endl;
}
}
So how do u guys think that this code (for eg) can be edited for better compilation time?
First profile.
Second, turn up optimizations levels on you compiler.
Thirdly, replace your for loop with multiplication / algebra. For example, the line
a+=N
is the fundamental basis for multiplication (repetitive addition), and the loop can be replaced by:
a += j * N; N -= j;
Replacing the loop will speed up your program (if your compiler hasn't already replaced the loop).
Printing the assembly language for the function will show how the compiler applied optimizations.
Edit 1:
Less code means a faster build time as well. I don't know if time difference in building is measurable.

How to round to the nearest fourth [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm looking for a way to round a number to nearest number that can be divided by 4 without remainder
num = std::round(num / 4.0) * 4.0;
Here is some pseudo code. Probably not the most efficient way, but...
if num mod 4 == 0 then you are good
if num mod 4 == 1 then subtract 1
if num mod 4 == 2 then you decide (subtract/add 2)
if num mod 4 == 3 then add 1
Use the following MACRO:
#define ALIGN4(len) (((len) + 3) & ~3) // round up to 4 items