PAT 天梯赛 L1-049. 天梯赛座位分配 【循环】

本文介绍了一种用于比赛座位分配的算法实现,通过使用二维数组记录各校队员的座位号,并确保相邻座位来自不同学校,以达到均匀分布的效果。最终按每组10个座位输出结果。

题目链接

https://www.patest.cn/contests/gplt/L1-049

思路

用一个二维数组来保存一个学校每个队员的座位号

然后需要判断一下 目前的座位号 与该学校当前状态下最后一个分配的队员的座位号 是不是只相差1 如果是 那么就要再加1 因为当前的状态 应该就是只剩下这个学校的队伍了 要隔一个

最后 对于每个学校 10个一组 10个一组 输出来就可以了

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>

#define CLR(a) memset(a, 0, sizeof(a))
#define pb push_back

using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

const double PI  = 3.14159265358979323846264338327;
const double E   = exp(1);
const double eps = 1e-6;

const int INF  = 0x3f3f3f3f;
const int maxn = 1e5 + 5;
const int MOD  = 1e9 + 7;

vector <int> ans[100];
int vis[100];

int main()
{
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &vis[i]);
        vis[i] *= 10;
    }   
    int count = 1;
    while (1)
    {
        int flag = 0;
        for (int i = 0; i < n; i++)
        {
            if (ans[i].size() < vis[i])
            {
                if (ans[i].size() && count - ans[i][ans[i].size() - 1] == 1)
                    count ++;
                ans[i].pb(count++);
                if (ans[i].size() < vis[i])
                    flag = 1;
            }
        }
        if (flag == 0)
            break;
    }
    for (int i = 0; i < n; i++)
    {
        printf("#%d\n", i + 1);
        for (int j = 0; j < vis[i]; j++)
        {
            printf("%d", ans[i][j]);
            if (j && (j +1) % 10 == 0)
                printf("\n");
            else 
                printf(" ");
        }
    }
}
### 关于 PAT 天梯赛 L1-006 连续因子的解题思路 此问题的核心在于找到给定正整数 \( N \) 的最长连续因子序列,并按照题目要求输出其长度以及具体的连续因子表达形式。以下是详细的解析: #### 问题分析 对于任意正整数 \( N \),我们需要找出一组连续的因子,使得它们的乘积等于 \( N \) 并且该组因子的数量尽可能多。如果存在多个满足条件的连续因子组合,则应返回起始数值最小的那一组。 为了高效解决这一问题,可以采用如下方法: - **枚举起点**:从可能的小因数开始逐一尝试作为连续因子区间的左端点。 - **逐步扩展区间右边界**:计算当前范围内所有数相乘的结果是否能被原数\( N \) 整除。一旦发现无法继续延伸或者超出目标值范围就停止对该特定起点进一步探索。 - **记录最佳方案**:在整个过程中保持跟踪所遇到的最大合法连续因子集合及其对应的信息以便最终输出。 #### 示例代码实现 (Python 版本) 下面提供了一个基于上述逻辑编写的 Python 实现版本: ```python def find_longest_consecutive_factors(N): max_len = 0 best_seq = [] # 枚举可能成为连续因子序列的第一个成员的位置i for i in range(2, int(N**(1/2)) + 2): product = 1 seq = [] j = i while True: if j > N or product * j > N: break product *= j seq.append(j) if N % product == 0 and len(seq) >= max_len: max_len = len(seq) best_seq = seq[:] j += 1 return max_len, "*".join(map(str,best_seq)) if __name__ == "__main__": import sys try: input_line = sys.stdin.read().strip() number = int(input_line) length, factors_str = find_longest_consecutive_factors(number) print(length) print(factors_str) except Exception as e: pass ``` 以上程序通过双重循环结构实现了对每一个潜在初始位置的有效探测,并利用列表存储临时结果,在每次更新更优解答时同步刷新全局最优状态变量 `max_len` 和 `best_seq`. --- #### 注意事项与特殊情形处理 需要注意的是当输入为质数时,由于不存在任何大于一且小于自身的其他自然数能够将其完全分解成两个以上的连贯部分之积的形式,因此在这种情况下应当单独考虑并直接返回单一元素构成的答案即该质本身[^1]。 另外还需留意某些极端情况下的性能表现优化措施比如提前终止不必要的迭代操作等策略的应用价值所在之处[^3]. ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值