本文目錄一覽:
- 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
#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/zh-hant/n/301065.html