C ++中的算法编码,问题部分对此进行了描述 [英] Coding for the algorithm in C++ which is described in the problem section
问题描述
输入:每个代理req1,req2,req3的请求......
1. SUM< - 所有请求的总和。
2.而SUM> 0做(对于每个数据请求)
3. i< - SelectAgent()
4. k< - SelectObject()
5.将k添加到分配的i集合中。
6. SUM< - SUM - 1.
让代理是a1,a2,a3和数据对象是d1 ,d2,d3,d4,d5
a1得到了d1,d2,d3
a2得到了d4,d5
a3得到了d1,d4 < br $>
i本身会对select agent和select对象进行编码。现在我想知道我如何将这些对象(k)分配给代理商(i)????
我尝试了什么:
i考虑了2-d的数组,拿了[i] [k] ..
现在问题出在代理人得到的时候任何对象然后只应该移动它的行。但所有代理人的行都会转移...
例如)a1(即i = 1)得到d1(即k = 1)我们分配在[1] [1]然后a2(即i = 2)得到d4(即k = 4)。它将被分配在[2] [4] ...但是t应该在[2] [1]处分配。我怎么能这样做。请解释一下这个代码。
Input : Requests of each agent req1,req2,req3……
1. SUM <- sum of all requests.
2. while SUM > 0 do (for each data request)
3. i <- SelectAgent( )
4. k <- SelectObject( )
5. Add k to the allocated set of i.
6. SUM <- SUM – 1.
let agents are a1,a2,a3 and data objects are d1,d2,d3,d4,d5
a1 got d1,d2,d3
a2 got d4,d5
a3 got d1,d4
i will itself do the coding for select agent and select object. now i want to know how i allocate these objects(k) to agents(i)????
What I have tried:
i considered the array of 2-d by taking a[i][k]..
now the problem is this when agent get any object then only its row should be shifted. but row of all the agent will shift...
for eg) a1(i.e i=1) got d1(i.e k=1) we allocate at a[1][1] then a2(i.e i=2) got d4(i.e k=4).it will be allocated at a[2][4]...but t should be allocated at a[2][1]. how can i do this.please explain the code for this.
推荐答案
特别是,我只有一个解决你声称的问题的方法:
我实际上并不知道SelectAgent和SelectObject函数应该做什么,这就是我用两个陷阱替换它们的原因函数生成随机值,你需要替换为你执行正确的事情。
Particularly, I've got just one possible solution of the problem you've claimed:
I don't actually know what SelectAgent and SelectObject functions are supposed to do, that's why I replaced them with two "trap" functions generating random values, which you'll need to substitute with the ones the perform the right things for you.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define AGENTS_N 5 // The number of agents
#define OBJECTS_N 10 // The number of objects
// These are two "trap" functions which immitate selecting either agent or object
// You'll have to replace these two functions with your own to provide instant
// agent and object selection
int SelectAgent() { return rand() % AGENTS_N; }
int SelectObject() { return rand() % OBJECTS_N; }
// Another "trap" function which returns the sum of all requests
int GetSum() { return rand() % (AGENTS_N * OBJECTS_N) + 1; }
int main()
{
// Allocating 2-d pointer array to store requests values
// for each agent and object being selected
int** a = new int*[AGENTS_N];
// Allocating array of each row sizes
// and initially set each size to AGENTS_N;
int* sizes = new int[AGENTS_N];
for (int t = 0; t < AGENTS_N; t++)
sizes[t] = OBJECTS_N;
// Initializing each pointer of the 2-d
// array to store objects requests per agent
for (int i = 0; i < AGENTS_N; i++)
{
a[i] = new int[OBJECTS_N];
memset((void*)a[i], 0x00, sizeof(int) * OBJECTS_N);
}
// Obtaining the total sum value for all requests
int SUM = GetSum();
// For each data request dispatched we're performing
while (SUM > 0)
{
int i = SelectAgent(); // Obtaining the value of agent
int k = SelectObject(); // Obtaining the value of object
// Generating the random "trap" value for an object being selected
int rand_val = rand() % 0xFF + 1;
// Printing out the values of agent, object and value associated with it
printf("agent = %d object = %d value = %d\n", i, k, rand_val);
// Now, let's check if the value of the corresponding object of a certain agent
// has not yet been assigned. If not, simply assign random value to a specific
// data item of 2-d array
if (a[i][k] <= 0)
{
a[i][k] = rand_val;
// Printing out all data items of a row being selected
for (int x = 0; x < sizes[i]; x++)
printf("%d ", a[i][x]);
printf("\n");
}
else
{
// Otherwise, if the value of the object has been already assigned
// just shift all the values on the right to insert the value
int new_size = sizes[i] + 1;
int* temp = new int[new_size];
memcpy((void*)temp, (const void*)a[i], sizeof(int) * sizes[i]);
for (int t = sizes[i]; t >= k; t--)
a[i][t + 1] = a[i][t];
// Assigning the new random value and update the size of the row being selected
a[i][k] = rand_val; sizes[i] = new_size;
// Printing out all data items of a row being selected
for (int x = 0; x < sizes[i]; x++)
printf("%d ", a[i][x]);
printf("\n");
}
// Decrement SUM variable value
SUM = SUM - 1;
}
return 0;
}</conio.h></stdlib.h></stdio.h>
P.S.如果您有任何疑问,请在我的解决方案下发表您的问题评论。这就是所有人。
P.S. If you've got any questions, just post your comments under my solution for your question. That's all folks.
这篇关于C ++中的算法编码,问题部分对此进行了描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!