模拟题目所给的歌的随机播放算法
代码如下:
Code
1 #include<stdio.h>
2 int r[10002];
3 int findnum(int n)
4 {
5 int i,max;
6 max=1;
7 for(i=2;i<=n;i++)
8 {
9 if(r[i]>r[max])
10 max=i;
11 }
12 return max;
13 }
14 void process(int played,int n)
15 {
16 int i,j;
17 for(i=1;i<=n;i++)
18 if(i!=played)
19 r[i]+=r[played]/(n-1);
20 if(r[played]%(n-1)!=0)
21 {
22 j=r[played]%(n-1);
23 for(i=1;i<=n&&j>=1;i++)
24 if(i!=played)
25 {
26 r[i]++;j--;}
27 }
28 r[played]=0;
29 }
30 int main()
31 {
32 int n,t,played,i;
33 scanf("%d %d",&n,&t);
34 for(i=1;i<=n;i++)
35 scanf("%d",&r[i]);
36 while(t--)
37 {
38 played=findnum(n);
39 printf("%d\n",played);
40 process(played,n);
41 }
42 return 0;
43 }
posted on
2008-11-21 08:51
pandy
阅读(
282)
评论()
编辑
收藏
举报