团体程序设计天梯赛L1-027 出租

本文介绍了一种将电话号码转换为特定编码格式的方法,并提供了一个示例程序来生成这种编码。该程序首先解析输入的电话号码,然后创建两个整数数组arr和index,最后输出这两行编码供进一步使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

L1-027. 出租

时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
陈越

下面是新浪微博上曾经很火的一张图:

一时间网上一片求救声,急问这个怎么破。其实这段代码很简单,index数组就是arr数组的下标,index[0]=2 对应 arr[2]=1,index[1]=0 对应 arr[0]=8,index[2]=3 对应 arr[3]=0,以此类推…… 很容易得到电话号码是18013820100。

本题要求你编写一个程序,为任何一个电话号码生成这段代码 —— 事实上,只要生成最前面两行就可以了,后面内容是不变的。

输入格式:

输入在一行中给出一个由11位数字组成的手机号码。

输出格式:

为输入的号码生成代码的前两行,其中arr中的数字必须按递减顺序给出。

输入样例:
18013820100
输出样例:
int[] arr = new int[]{8,3,2,1,0};
int[] index = new int[]{3,0,4,3,1,0,2,4,3,4,4};

—————————————————————————————————————


大体思路:开数组记录数字是否出现过,根据这个数组构造楚arr,然后依次在arr中查找构造index数组


#include<map>
#include<set>
#include<ctime>
#include<cmath>
#include<queue>
#include<bitset>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define inf 0x3f3f3f3f

int main()
{
    char s[100];
    int a[100];
    int cnt[10];
    int arr[100];
    int index[100];
    scanf("%s",s);
    int k=strlen(s);
    memset(cnt,0,sizeof cnt);
    for(int i=0; i<k; i++)
    {
        a[i]=s[i]-'0';
        cnt[a[i]]++;
    }
    int ct=0;
    for(int i=9; i>=0; i--)
    {
        if(cnt[i]>0)
        {
            arr[ct++]=i;
        }
    }
    for(int i=0; i<k; i++)
    {
        for(int j=0; j<ct; j++)
        {
            if(a[i]==arr[j])
                index[i]=j;
        }

    }
    printf("int[] arr = new int[]{");
    int q=0;
    for(int i=0; i<ct; i++)
    {
        if(q++)
            printf(",");
        printf("%d",arr[i]);
    }
    printf("};\n");


    printf("int[] index = new int[]{");
    q=0;
    for(int i=0; i<k; i++)
    {
        if(q++)
            printf(",");
        printf("%d",index[i]);
    }
    printf("};\n");




    return 0;
}


### 解析 L1-071 题目 #### 题目背景描述 静静姐正在处理一份来自某企业人力资源部门的要求,该企业希望获得一批通过特定筛选条件的学生名单。这些学生参加了2022年的团体程序设计天梯赛,并且满足一定的分数门槛和其他附加条件[^2]。 #### 问题核心需求 为了响应这一请求,需要编写一段程序来处理参赛者的成绩列表,从中挑选出符合条件的候选人并按照批次输出推荐名单。具体来说: - 推荐对象仅限于那些得分达到或超过175分的选手; - 同一推荐批次内的学生成绩需保持严格递增趋势; - 对于成绩相等情况下的特殊考量:如果一名学生的竞赛成绩与前一位相同,但是此人在PAT考试中有合格表现,则允许例外加入同一组别。 #### 数据输入说明 给定一组整数表示各参赛者在比赛中的最终得分以及他们是否具备额外资格(即是否有有效的PAT成绩)。此外还需指定K值代表总共要产生的独立推荐批次数量。 #### 输出预期结果 对于每一个k (1 ≤ k ≤ K),依次打印出第k批被选中成员的成绩序列,每行一个数值直到结束当前轮次的选择过程为止。 --- ```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; struct Student { int score; bool hasPat; // 是否有PAT成绩 bool operator<(const Student& other) const { return score < other.score || (score == other.score && !hasPat); } }; int main() { vector<Student> students; int n, m, patLine, lgeScore; cin >> n >> m >> patLine; for(int i=0;i<n;++i){ cin>>lgeScore; if(lgeScore>=175){ students.push_back({lgeScore,false}); }else{ continue; } char c; cin>>c; if(c=='Y'){ students.back().hasPat=true; } } sort(students.begin(),students.end()); int batchCount = 0; while (!students.empty()) { ++batchCount; cout << "Batch #" << batchCount << ":" << endl; auto it = unique_if_not_pat(students.begin(), students.end(), [](Student a, Student b){return a.score==b.score&&!a.hasPat&&b.hasPat;}); for(auto iter = students.begin();iter!=it;++iter){ cout<<(*iter).score<<"\n"; } students.erase(it, students.end()); if(batchCount >= m){ break; } } } ``` 上述代码实现了基本逻辑框架,但`unique_if_not_pat`函数并未定义,在实际应用时应当替换为标准库提供的合适算法或者自定义实现以完成去重操作,确保当存在重复分数而后者拥有PAT证书的情况下能够保留后者进入下一轮比较。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值