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

本文目录一览:

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

#includestdio.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=0a[i]x;i–)

a[i+1]=a[i];

a[i+1]=x;

n++;

for(i=0;in;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;i11;++i) printf(“%.2f “, a[i]);

}

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

这题真麻烦,终于写完了。

我得思路是这样的,这道题其实就是维护2个队列。

我把每个组的人当成一个人,而这些人排队就可以看成组与组之间的排队,这是第一个队列,也就是gqueue

然后每个组也都是一个队列,里面顺序排着每个组的人。

描述完成了,然后就是进队出队的问题了。

进队时首先查看自己所在的组有没有在gqueue中,team_in数组保存了每个组在gqueue中的位置,当然一开始都是-1,表示没有入队。如果自己的组在gqueue中,那么就直接加入自己所在组的队列中。如果没有自己的组,就让他所代表的组入gqueue。

出队时出gqueue中第一个组的第一个人,判断一下这个组是不是没有人了,要是没有人了就把这个组dequeue。

大概思路就是这样,还有要注意的就是我用h_table存储哈西表,代表着每个人所在的组的编号,这样把人名shash以下就可以知道他所处的组。

#include stdio.h

#include string.h

struct person

{

        char name[ 5 ];

        int team;

};

struct pqueue //组内队列

{

        person p[ 1001 ];

        int head, tail;

};

struct gqueue //组队列

{

        pqueue pq[ 1001 ];

        int head, tail;

};

int n, h_table[ 100000 ], team_in[ 1001 ];

gqueue q;

void p_enqueue ( person a, pqueue q )

{

        q.p[ q.tail ] = a;

        q.tail++;

        if ( q.tail == 1001 )

                q.tail = 0;

}

void g_enqueue ( person a, 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( pqueue q )

{

        printf(“%s\n”, q.p[ q.head ].name );

        q.head++;

        if ( q.head == 1001 )

                q.head = 0;

}

person g_dequeue( 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;

        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 ];

        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语言,用数组实现队列的入队,出队函数编程

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

#includestdio.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( 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=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++){coutq[i];}

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/301065.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-12-29 14:19
下一篇 2024-12-29 14:19

相关推荐

  • Python官网中文版:解决你的编程问题

    Python是一种高级编程语言,它可以用于Web开发、科学计算、人工智能等领域。Python官网中文版提供了全面的资源和教程,可以帮助你入门学习和进一步提高编程技能。 一、Pyth…

    编程 2025-04-29
  • 如何解决WPS保存提示会导致宏不可用的问题

    如果您使用过WPS,可能会碰到在保存的时候提示“文件中含有宏,保存将导致宏不可用”的问题。这个问题是因为WPS在默认情况下不允许保存带有宏的文件,为了解决这个问题,本篇文章将从多个…

    编程 2025-04-29
  • Python导入数组

    本文将为您详细阐述Python导入数组的方法、优势、适用场景等方面,并附上代码示例。 一、numpy库的使用 numpy是Python中一个强大的数学库,其中提供了非常丰富的数学函…

    编程 2025-04-29
  • Python返回数组:一次性搞定多种数据类型

    Python是一种多用途的高级编程语言,具有高效性和易读性的特点,因此被广泛应用于数据科学、机器学习、Web开发、游戏开发等各个领域。其中,Python返回数组也是一项非常强大的功…

    编程 2025-04-29
  • AES加密解密算法的C语言实现

    AES(Advanced Encryption Standard)是一种对称加密算法,可用于对数据进行加密和解密。在本篇文章中,我们将介绍C语言中如何实现AES算法,并对实现过程进…

    编程 2025-04-29
  • 学习Python对学习C语言有帮助吗?

    Python和C语言是两种非常受欢迎的编程语言,在程序开发中都扮演着非常重要的角色。那么,学习Python对学习C语言有帮助吗?答案是肯定的。在本文中,我们将从多个角度探讨Pyth…

    编程 2025-04-29
  • Python去掉数组的中括号

    在Python中,被中括号包裹的数据结构是列表,列表是Python中非常常见的数据类型之一。但是,有些时候我们需要将列表展开成一维的数组,并且去掉中括号。本文将为大家详细介绍如何用…

    编程 2025-04-29
  • Python操作数组

    本文将从多个方面详细介绍如何使用Python操作5个数组成的列表。 一、数组的定义 数组是一种用于存储相同类型数据的数据结构。Python中的数组是通过列表来实现的,列表中可以存放…

    编程 2025-04-29
  • Python被称为胶水语言

    Python作为一种跨平台的解释性高级语言,最大的特点是被称为”胶水语言”。 一、简单易学 Python的语法简单易学,更加人性化,这使得它成为了初学者的入…

    编程 2025-04-29
  • Java Thread.start() 执行几次的相关问题

    Java多线程编程作为Java开发中的重要内容,自然会有很多相关问题。在本篇文章中,我们将以Java Thread.start() 执行几次为中心,为您介绍这方面的问题及其解决方案…

    编程 2025-04-29

发表回复

登录后才能评论