c语言数组插队问题,插队问题C语言

发布时间:2022-11-29

本文目录一览:

  1. c语言程序:插队问题:有一个10个人的队伍,他们的身高分别为:1.73,1.75,1.7
  2. c语言程序:插队问题?
  3. 急求插队问题的C语言描述
  4. C语言排队插队问题以数字代表人数。例如输入12534,5插队了就输出数字1;654321,就是65432插队了,输出5
  5. C语言,用数组实现队列的入队,出队函数编程
  6. C语言模拟排队编程,急求完整答案

c语言程序:插队问题:有一个10个人的队伍,他们的身高分别为:1.73,1.75,1.7

#include<stdio.h>
int main()
{
    float x=1.82,a[10]={1.73,1.75,1.78,1.84,1.88,1.89,1.90};
    int i,j,n=7;
    for(i=n-1;i>=0&&a[i]>x;i--)
        a[i+1]=a[i];
    a[i+1]=x;
    n++;
    for(i=0;i<n;i++)
        printf("%.2f ",a[i]);
    printf("\n");
    return 0;
}

c语言程序:插队问题?

#include <stdio.h>
int main()
{
    float a[11] = {
        1.73,1.75,1.78,1.81,1.84,1.87,1.88,1.88,1.89,1.90
    };
    float b = 1.82;
    int i;
    for(i = 9; i >= 0; --i) {
        if(a[i] > b) a[i+1] = a[i];
        else break;
    }
    a[i+1] = b;
    for(i=0;i<11;++i) printf("%.2f ", a[i]);
}

急求插队问题的C语言描述

这题真麻烦,终于写完了。 我的思路是这样的,这道题其实就是维护2个队列。 我把每个组的人当成一个人,而这些人排队就可以看成组与组之间的排队,这是第一个队列,也就是gqueue 然后每个组也都是一个队列,里面顺序排着每个组的人。 描述完成了,然后就是进队出队的问题了。 进队时首先查看自己所在的组有没有在gqueue中,team_in数组保存了每个组在gqueue中的位置,当然一开始都是-1,表示没有入队。如果自己的组在gqueue中,那么就直接加入自己所在组的队列中。如果没有自己的组,就让他所代表的组入gqueue。 出队时出gqueue中第一个组的第一个人,判断一下这个组是不是没有人了,要是没有人了就把这个组dequeue。 大概思路就是这样,还有要注意的就是我用h_table存储哈希表,代表着每个人所在的组的编号,这样把人名哈希以下就可以知道他所处的组。

#include <stdio.h>
#include <string.h>
struct person
{
    char name[5];
    int team;
};
struct pqueue //组内队列
{
    struct person p[1001];
    int head, tail;
};
struct gqueue //组队列
{
    struct pqueue pq[1001];
    int head, tail;
};
int n, h_table[100000], team_in[1001];
struct gqueue q;
void p_enqueue (struct person a, struct pqueue *q)
{
    q->p[q->tail] = a;
    q->tail++;
    if (q->tail == 1001)
        q->tail = 0;
}
void g_enqueue (struct person a, struct gqueue *q)
{
    if (team_in[a.team] != -1)
        p_enqueue(a, &q->pq[team_in[a.team]]);
    else
    {
        q->pq[q->tail].head = 0;
        q->pq[q->tail].tail = 0;
        team_in[a.team] = q->tail;
        p_enqueue(a, &q->pq[q->tail]);
        q->tail++;
        if (q->tail == 1001)
            q->tail = 0;
    }
}
void p_dequeue(struct pqueue *q)
{
    printf("%s\n", q->p[q->head].name);
    q->head++;
    if (q->head == 1001)
        q->head = 0;
}
struct person g_dequeue(struct gqueue *q)
{
    int t = q->pq[q->head].p[q->pq[q->head].head].team;
    p_dequeue(&q->pq[q->head]);
    if (q->pq[q->head].tail == q->pq[q->head].head)
    {
        team_in[t] = -1;
        q->head++;
        if (q->head == 1001)
            q->head = 0;
    }
}
int shash(char *key)
{
    unsigned long h = 0;
    while(*key)
    {
        h = (h << 4) + *key++;
        unsigned long g = h & 0Xf0000000L;
        if (g) h ^= g >> 24;
        h = ~g;
    }
    return h % 100000;
}
void init()
{
    int i, j, t;
    struct person per;
    for (i = 0; i < n; i++)
    {
        team_in[i] = -1;
        scanf("%d", &t);
        for (j = 0; j < t; j++)
        {
            scanf("%s", per.name);
            per.team = i;
            h_table[shash(per.name)] = i;
        }
    }
    q.head = q.tail = 0;
}
void work()
{
    char command[10];
    struct person per;
    while (scanf("%s", command) != EOF)
    {
        if (strcmp(command, "STOP") == 0)
            break;
        if (strcmp(command, "DEQUEUE") == 0)
            g_dequeue(&q);
        if (strcmp(command, "ENQUEUE") == 0)
        {
            scanf("%s", per.name);
            per.team = h_table[shash(per.name)];
            g_enqueue(per, &q);
        }
    }
}
int main()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    int time = 1;
    while (scanf("%d", &n) != EOF && n)
    {
        printf("Scenario #%d\n", time++);
        init();
        work();
    }
    return 0;
}

C语言排队插队问题以数字代表人数。例如输入12534,5插队了就输出数字1;654321,就是65432插队了,输出5

5插队了干嘛输出1,还有654321就是65432插队输出5,你想干嘛呀,说都说不清

C语言,用数组实现队列的入队,出队函数编程

这样的话应该符合你的要求:

#include<stdio.h>
void add(int queue[],int x);
int Top(int queue[]);
void del(int queue[]);
int end=0;
int main()
{
    int n;
    scanf("%d",&n);//将要入队列n个元素
    int queue[1000];
    for(int i=1;i<=n;i++)//输入n个元素
    {
        add(queue,i);//将i加入队列
    }
    //验证加入队列的元素,将队列中的元素按照输入的顺序输出:
    for(int i=1;i<=n;i++)
    {
        printf("%d ",Top(queue));//Top函数返回队头元素
        del(queue);//删除队头元素
    }
    //验证输出已经出队列后的队列(数组)元素:
    printf("\n");
    for(i=1;i<=n;i++)
        printf("%d ",queue[i]);
    printf("\n");
    return 0;
}
void add(int queue[],int x)
{
    queue[++end]=x;
}
int Top(int queue[])
{
    return queue[1];//注意,这里的函数始终return queue[1];这里是和将普通数组中的元素输出最大的不同之处。
}
void del(int queue[])
{
    for(int i=2;i<=end;i++)
    {
        queue[i-1]=queue[i];
    }
    queue[end]=0;//将删除后的地方置0
    end--;
}

C语言模拟排队编程,急求完整答案

这个题有够无聊的, 不过这明显是个基础教学用PPT,是帮助理解的案例,不是用来考人的, 那这些模拟数字应该都是直接写入的吧:

//state1
int q[10];
//state2
q[0] = 9;
q[1] = 5;
q[2] = 2;
q[3] = 7;
//state3
q[4] = 6;
q[5] = 4;
//state4
for(int i = 0;i < 4;i++){q[i] = q[i+2];}
q[4] = 0;q[5] = 0;
//state5
for(int i = 4;i >= 3;i--){q[i] = q[i-1];}
q[4] = 3;
//state6
int k;
for(int i = 0;i <= 4;i++){if(q[i] == 3){k = i;break;}}
//state7
for(int i = k;i < 4;i++){q[i] = q[i+1];}
q[4] = 0;
//state8
for(int i = 0;i < 4;i++){cout<<q[i];}