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 6 years ago.
Improve this question
There is given array of size > n , we need to select n number of element from an array.
For example : array contain 112 element and n = 50, so select 50 number such that distance between every two selected number is more or less equal (equal distance is not possible of course except for size%n == 0 ).
If anyone suggest any idea that would work .
Example :
array = 1 2 3 4 5
n = 1
output : 1 or any another number depending on proposed algo.
n = 2
output : 1 3 or 2 4 or 1 4...
n = 3
output : 1 3 5
n = 4
output : 1 3 4 5 or 1 2 4 5
n = 5 :
output 1 2 3 4 5
Basically in case of n=1,2,4 there are more then one possible combination so I need to devise an algo which would pick numbers in uequally distributed manner.
One approach would be dividing the number of elements by the number of desired elements in the selection in floating point, and using rounding to determine the index:
double dist = ((double)size) / n;
int *res = new int[n];
for (int i = 0 ; i != n ; i++) {
res[i] = orig[round(dist*i)];
}
For your example of 112 and 50 the value of dist would be 2.24 and the sequence of indexes selected from the array would be
0 0
1 2
2 4
3 7
4 9
5 11
......
45 101
46 103
47 105
48 108
49 110
Related
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 8 days ago.
Improve this question
Could you pls. help with algorithm (I use C++) which seemed so simple at first glance.
I need a total enumeration of all combinations of N parameters to run some target function for all combination and choose one optimal combination.
Let say there are three parameters (it can be any number set by user but for this example let’s assume 3)
Possible values (can be any number for any parameter - NOT a fixed number - number of values set a user before program starts)
name
value
value
value
value
param1
1
2
..
param2
10
20
..
param3
100
200
300
..
So the number of combinations = 12 (2 * 2 * 3 = 12)
All combinations
#
param1
param2
param3
1
1
10
100
2
2
10
100
3
1
20
100
4
2
20
100
5
1
10
200
6
2
10
200
7
1
20
200
8
2
20
200
9
1
10
300
10
2
10
300
11
1
20
300
12
2
20
300
Ok – let say the order may be different
#
param1
param2
param3
1
1
10
100
2
1
20
100
3
1
10
200
4
1
20
200
5
1
10
300
6
1
20
300
7
2
10
100
8
2
20
100
9
2
10
200
10
2
20
200
11
2
10
300
12
2
20
300
However it’s obviously one counter should change while others fixed and after one counter finish increment next in the chain
This approach seems quite simple but I still can’t find an implementation. I thought use a list for the parameters and as one counter finish itereates it's values it call the next parameter and increment next parameter value and reset the first parameter counter. But how put it in couple of loops … ? I intuitively feels it should be quite simple solution.
The another approach I think – use all combinations to build a graph and after that traverse the whole graph and in the end get an optimal combination. But if I fill the graph it means I already solve this problem and building a graph just a waste of time and memory.
For now there is a sketch (in pseudo-code) like this:
std::list<param> params
bool isDone = false
func(node* n)
{
if(n->prev)
{
n->GetCurrentValue() // return current value and increment by one
n->prev->reset();
func(n->prev)
Run(); // target function
if(n->IsDone()) // finish end of the values
{
if(n->next)
func(n->next);
else
isDone = true;
}
}
else // first node in the list
{
while(n->IsDone()) // finish end of the values
{
n->GetCurrentValue() // return current value and increment by one
Run() // target function
}
n.reset() // set internal counter for the node to 0
func(n->next())
}
}
while(!isDone)
{
for(p : params)
{
func(p)
}
}
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 3 years ago.
Improve this question
To print multiplication tables, I'm using a for loop which has fixed values up to 10th times table only. I need multiplication table as run time values. Is there an alternate loop to take the values at run time? Here is the code which i tried:
scanf(&num);
for(i=num;i<=num;i++)
for (j=1;j<=10;j++)
This is not a Python For loop
In python,you can use the Range function to have a nice multiplication table.
def table_choice(my_choice=None):
for a in range(10):
if my_choice != None:
print('{0} x {1} = {2}'.format(my_choice, a, my_choice * a))
else:
for b in range(10):
print('{0} x {1} = {2}'.format(a, b, a * b))
table_choice(my_choice = 7)
OUTPUT:
7 x 0 = 0
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
In case you execute table_choice() you will get the full table
See the Range documentation in : https://docs.python.org/3/library/functions.html#func-range
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 2 years ago.
Improve this question
lets add some flavor of ingenuity to it.Addition operation requires cost now, and the cost is the summation of those two to be added. So,to add 1 and 10, you need a cost of 11. If you want to add 1, 2 and 3. There are several ways1 + 2 = 3, cost = 31 + 3 = 4, cost = 42 + 3 = 5, cost = 53 + 3 = 6, cost = 62 + 4 = 6, cost = 61 + 5 = 6, cost = 6Total = 9Total = 10Total = 11I hope you have understood already your mission, to add a set of integers so that the cost is minimal.Input Each test case will start with a positive number,N(2N5000) followed by N positive integers(all are less than 100000). Input is terminated by a case where the value of N is zero. This case should not be processed.Output For each case print the minimum total cost of addition in a single line.
Sample Input
3
1 2 3
4
1 2 3 4
0
Sample Output
9
19
i tried to sort the given array and then took another array for cumsum (CS) and summed all element of CS except cs[0].. i am getting WA for this approach, please explain
int n,i,hold=0;
while(1)
{
cin>>n;
if(n==0){break;}
int arr[n],cs[n];
for(i=0;i<n;i++) cin>>arr[i];
sort(arr,arr+i);
cs[0]=arr[0];
for(i=1;i<n;i++){cs[i]=arr[i]+cs[i-1]; }
cs[0]=0;
int sum=0;
for(i=1;i<n;i++){sum+=cs[i]; }
cout<<sum<<endl;
sum=0;
}
input:
9
66 85 52 22 44 1 59 88 67
0
my out:
1822
expected result(udebug):
1454
getting WA
Your idea is wrong to solve this problem.
after taking all the elements on a data structure you should repeat this 3 points:
1)sort.
2)sum 1st two value,and remove 1st two value from the data structure
3)add the sum to the cost and data structure.
you can use priority_queue as the data structure.
Use min heap and add 2 smallest element. Example:
1 2 3 -> 3 3 -> 6.
1 2 3 4 -> 3 3 4 -> 4 6 -> 10.
Hope it helps.
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 7 years ago.
Improve this question
I have next file with thousands of lines
1 2 3 4
1 4 5
6
5 6 8 9
5
2 4 7 9
5 8 10
1 3 5 9
.......
Each line (the line number represents the regions) has maximum 4 values (represent labels) and these values are between 0 and 1000.
And I want to know in which lines (regions) are the values (labels) and order them, for that I will need a loop but I do not how.
For instance for the label number 1 is found in line (region) 1,2 and 8 so the output must be 1: 1 2 8
I want this output:
1: 1 2 8
2: 1 6
3: 1 8
4: 1 2 6
5: 2 4 5 7 8
6: 3 4
7: 6
8: 4 7
9: 4 6 8
10: 7
....
The output is the sorted labels from 0 to 1000 and the regions in which they are found.
I was thinking in a loop in Linux or c++ any idea?
You can have a std::map<int, std::list<int>> that maps int to list<int>.
For each value use:
auto iter = map.find(row);
if (iter != m.end() )
{
(iter->second).push_back(val);
} else {
map[row] = std::list<int>(1, val);
}
Then use an iterator and print all the key,value pairs at the end.
Perl to the rescue:
perl -lne 'push #{ $h{$_} }, $. for split
}{
print "$_: #{ $h{$_} }" for sort { $a <=> $b } keys %h
' < input.txt
$. is the line number, you just add it to the hash table for each of the numbers on the line. At the end, you sort the keys of the hash table numerically and print the corresponding arrays.
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 8 years ago.
Improve this question
I have two workers.If I have configured a strategy that 60% tasks are allocate to A worker and the rest to B worker.
how do you implement with c++.
what is your suggestion?
map<string,float> m_percent;
m_percent.insert(make_pair("countA",0.6));
m_percent.insert(make_pair("countB",0.1));
m_percent.insert(make_pair("countC",0.3));
map<string,int> m_count;
m_count.insert(make_pair("total",0));
map<string,int>::iterator it = m_count.find("countA");
map<string,int>::iterator itc =m_count.find("total");
map<string,float>::iterator itp=m_percent.find("countA");
if(it== m_count.end())//use countA
{
m_count.insert(make_pair("countA",1));
}
else
{
int &c = it->second;
if(itc!=m_count.end()&&itp!=m_percent.end())
{
float f=(c+1)*100/(itc->second+1)*100.0
if (f<=itp->second)
{
it->second=it->second+1;
}
}
}
if(itc!=m_count.end())
{
itc->second=itc->second+1;
}
If you're talking about number of tasks with no regard to complexity, simply keep a count of how many jobs have been allocated to each. Let's call these counts countA for the jobs allocated to A, and count for the total number of jobs (for simplicity in calculations), and initialise them to zero.
Then, when a job comes in, allocate as per the following:
if count is equal to zero, allocate it to A and increment both countA and count.
otherwise, if countA / count is less than 0.6, allocate it to A and increment both countA and count.
otherwise allocate it to B and just increment count.
This will, over the long term, tend to average out allocations so that A gets 60%:
countA count countA/count allocateTo
------ ----- ------------ ----------
0 0 ? A
1 1 1.000 B
1 2 0.500 A
2 3 0.667 B
2 4 0.500 A
3 5 0.600 B
3 6 0.500 A
4 7 0.571 A
5 8 0.625 B
5 9 0.556 A
6 10 0.600 B
6 11 0.545 A
7 12 0.583 A
8 13 0.615 B
8 14 0.571 A
9 15 0.600 B
9 16 0.563 A
10 17 0.588 A
11 18 0.611 B
11 19 0.579 A
12 20 0.600
... and so on.