[PTA] ladder match 7-9 seat assignment

Title repeat

Ladder tournament every year a large number of team members, to ensure that all members of the same school are not adjacent, assigned seating has become a troublesome thing. To this end we have developed the following strategy: Suppose a stadium participating schools have N, i-schools have M [i] teams, 10 players per team. So that every school player in a single file, i + 1-team players behind the i-team players. From the first a school, the school No. 1 player in turn seated, then the school's first two players ...... and so on. If last only a school's team has not been assigned a seat, you will need to arrange their players compartments sit. This question requires you to write a program to automatically generate a seat number for the school team, numbering from 1.

Input formats:

Participating in the given input row University number N (a positive integer not exceeding 100); a second row of N gives a positive integer not exceeding 10, wherein the i-th number corresponding to the number of teams i universities, digital room separated by a space.

Output formats:

From a university of the first teams to start sequentially outputting players seat number. Each team per line, between the seat number separated by a space, the line from beginning to end may not have the extra space. In addition, the first line of each universities by "#X" output number of school X, starting at 1.

Sample input:

3
3 4 2

Sample output:

#1
1 4 7 10 13 16 19 22 25 28
31 34 37 40 43 46 49 52 55 58
61 63 65 67 69 71 73 75 77 79
#2
2 5 8 11 14 17 20 23 26 29
32 35 38 41 44 47 50 53 56 59
62 64 66 68 70 72 74 76 78 80
82 84 86 88 90 92 94 96 98 100
#3
3 6 9 12 15 18 21 24 27 30
33 36 39 42 45 48 51 54 57 60

answer

Take the sample, it gave the three schools, starting from 1 turns assign numbers to these three schools, if there is a school that everyone has a number, do not give this after school.

have to be aware of is:

  1. When the initial additional judge there is only one school, this time lets start id starting at -1, concrete can see the code.
  2. When only one school, let them sit apart, that is, between the need for numbers separated by 2.

C ++ AC Code

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main()
{
    int M[110];
    int N;
    vector<int> vs[110];
    cin>>N;
    //cnt初始为:还有几个学校未分配完
    int cnt=N;
    int cnt_tp=0;
    //标识哪个学校已经分配完了,默认都没有
    bool flag[110];
    memset(flag,0,sizeof(flag));
    for(int i=1; i<=N; i++)
    {
        cin>>M[i];
        M[i]*=10;
        cnt_tp+=M[i];

    }
    int id=0;
    if(N==1)
    {
        id=-1;
    }
    //总体while循环
    while(cnt_tp--)
    {
        for(int i=1; i<=N; i++)
        {
            //如果这个学校还没分配完
            if(M[i]!=vs[i].size())
            {
                if(cnt==1)
                {
                    id+=2;
                }
                else
                {
                    id++;
                }
                vs[i].push_back(id);
            }
            //已分配完的学校
            else
            {
                //如果这个学校的标识还是false,cnt--
                if(!flag[i])
                {
                    cnt--;
                    flag[i]=true;
                }
            }
        }

    }
    //输出元素
    for(int i=1; i<=N; i++)
    {
        cout<<"#"<<i<<endl;
        for(int j=0; j<vs[i].size(); j++)
        {
            if(j%10==0&&j!=0)cout<<endl;
            if(j%10!=0)cout<<" ";
            cout<<vs[i][j];
        }
        cout<<endl;
    }

    return 0;
}

Published 200 original articles · won praise 99 · views 40000 +

Guess you like

Origin blog.csdn.net/weixin_43889841/article/details/103983451