gpt4 book ai didi

c - 两个元素相加的数据结构

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:24:09 25 4
gpt4 key购买 nike

我在 c 中遇到问题,我想使用下面编写的代码来实现这种事情。输入和输出应该是这样的:

      Input Freq                                  Output Freq
1 0,1,4,2,5,3 add two first (0+1) 0,1,4,2,5,3,1
2 4,2,5,3,1 add min and last (2+1) 0,1,4,2,5,3,1,3
3 4,5,3,3 add min and last (3+3) 0,1,4,2,5,3,1,3,6
4 4,5,6 **here we add(4+5)**(minimum two)0,1,4,2,5,3,1,3,6,9
5 9,6 minimum two 0,1,4,2,5,3,1,3,6,9,15
6 15

但是条件是必须没有元素的交换没有排序,**但是我们可以处理元素的索引进行比较,如果找到我们添加它们并放在数组最后的任何索引处的正确元素。

我正在尝试一些基本想法它在第一个 if 条件下工作正常,但在我想知道的其他两个 if 条件下写什么。请在那里帮助我。假设 data[i].freq={0,1,2,3,4,5} 和 data[i].next 指向下一个元素,就像上面示例中的第一步一样,0 指向 1,现在 1 指向到这两个获得的元素(所以这个 1 指向 1 at alst 而这个 1 at last 索引将指向下一个“1”(我们另外使用)所以下一个“1”是 4 ,所以最后一个元素“1”指向 4 并且我们保持索引指向的方式相同)。如果您不明白我的意思,请不要犹豫,问我。我想代码应该非常接近这个:

    data[data_size].freq=data[0].freq+data[1].freq; // here i add the first 2 elements "0" and "1" in the example i given below.
data[data_size].flag=0; //I am using flag variable to show which elements are added or which are not added even once. If flag ="1" then that element is added if it "0" then not added even once.
data[0].flag=1;
data[1].flag=1; //these two have been added.
int count=5;
do
{
for(i=0;data[i].next!=-1;i=data[i].next)
{
if(data[data[i].next].freq>data[data_size].freq && data[data[i].next].flag==0)//Here i am setting flag=0 for those elements who not have been added yet. Because we don't have to take in account for addition those elements who are already added once.(step1 and step2 are coming in this loop)
{
data[data_size+1].freq= data[data_size].freq+ data[data[i].next].freq;
data[data_size].flag=1;//those elements which we are adding we set their flag to 1
data[data[i].next].flag=1;
data[data_size+1].flag=0;//this is the element onbtained on result will be sent to last index.With 0 flag because it is not added yet.
data[data_size].next=data[i].next;
data[i].next=data_size;
data_size++;
}
if(data[data[i].next].freq<data[data_size].freq && data[data[i].next].flag==0)
{
//some code for step4 where 6>5 (in this case we added 5+4)
data_size++;
}
if(data[data[i].next].freq==data[data_size].freq && data[data[i].next].flag==0)
{
//Some code for step3 when element are equal
data_size++;
}
}
count--;
} while(count>0)

会有不同的条件,例如(最后一个元素=右侧找到的元素,例如在第 2 步中找到的 3+3=6),找到的元素比最后一个元素更小,例如 5+4=9(参见第 4 步)

知道在其他两个 if 条件下应该做什么吗?我的数组输入必须是 {0,1,4,2,5,3}(我的意思是数据 [i].freq)并且输出数组必须是 {0,1,4,2,5 ,3,1,3,6,9,15} (data[data_size].freq),没有任何排序,没有任何交换,只使用索引移动,只使用数组。请帮我写另外两个if条件。

最佳答案

终于我能够解决我的问题了。我使用一个队列来做到这一点,Front 和 Rear 变量指向 data[] 数组的起始索引和结束索引。如果遇到相同类型的问题,这里是任何 future 用户的等效帮助示例(请注意这里每件事都是静态的,但实现的逻辑是相同的:

#include <stdio.h>

void main() {
int max = 50;
int index = 0, Front = 0, Rear, min, min2, location, i, location2, flag[30], check = 0, j;

int data[50] = {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
};
Rear = 9;
for (i = 0; i <= Rear; i++) {
flag[i] = 0;
}
int count = Rear;
do {
if (Front == 0) {
printf("check1 \n ");

Rear++;
data[Rear] = data[Front] + data[1];
flag[Front] = 1;
flag[Rear] = 0;
flag[1] = 1;
printf("*****************dataRear: %d\n ", data[Rear]);
Front = Front + 2;
}

if (data[Front] == data[Rear] && flag[Rear] == 0 && flag[Front] == 0) {
printf("check3 \n ");
data[Rear + 1] = data[Front] + data[Rear];
printf("************dataRear[Rear+1]: %d\n ", data[Rear + 1]);
flag[Front] = 1;
flag[Rear] = 1;
flag[Rear + 1] = 0;
for (j = Front + 1; j <= Rear; j++) {
if (flag[j] == 0) {
Front = j;
break;
}
}
Rear++;
}

if (data[Front] < data[Rear] && flag[Rear] == 0 && flag[Front] == 0) {
int start = Front + 2;
min = data[Front];
for (j = Front + 1; j <= Rear; j++) {
if (flag[j] == 0) {
min2 = data[j];
location2 = j;
break;
}
}
location = Front;

for (i = start; i <= Rear; i++) {
if (data[i] < min && flag[i] == 0) {
min = data[i];
location = i;
min2 = min;
}
if (data[i] < min2 && flag[i] == 0) {
min2 = data[i];
location2 = i;
}
}
data[Rear + 1] = min2 + min;
flag[location2] = 1;
flag[location] = 1;
flag[Rear + 1] = 0;

for (j = location + 1; j <= Rear; j++) {
if (flag[j] == 0) {
Front = j;
break;
}
}
printf("*****************dataRear: %d\n ", data[Rear]);
Rear = Rear + 1;
}
count--;
} while (Front != Rear && count > 0);



for (i = 0; i < 21; i++) {
printf(" %d ", data[i]);
}
printf("\n");
}

关于c - 两个元素相加的数据结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21485398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com