CQUPT WEEKLY TRAINING (7)DIV2 解题报告

来源:互联网 发布:软件系统运行报告 编辑:程序博客网 时间:2024/06/07 17:15

A题:题意背景就不描述了,阅读题。

总的说来就是,有4种优先级的东西,按照优先级挨着输出。如果优先级相同就输出较前面的。
rat>woman==child>man>captain。
数据比较小且优先级种类少,我挨着扫4遍就行了。

#include <cstdio>#include <algorithm>#include <cstring>#include <cmath>#include <iostream>#include <string>using namespace std;string a[105],b[105];int main(){    int n;    while(scanf("%d",&n) != EOF)    {        for(int i=0;i<n;i++)        {            cin >> a[i] >> b[i];        }        for(int i=0;i<n;i++)        {            if(b[i] == "rat")                cout <<a[i] << endl;        }        for(int i=0;i<n;i++)        {            if(b[i] == "woman" || b[i] == "child")                cout <<a[i]<< endl;        }        for(int i=0;i<n;i++)        {            if(b[i] == "man")                cout <<a[i]<< endl;        }        for(int i=0;i<n;i++)        {            if(b[i] == "captain")                cout << a[i] << endl;        }    }    return 0;}

B题:n个有rankl的士兵进行训练。相同优先级视为一个group,每个group每轮只能有一个人训练升级。问需要多少代价把所有士兵的rank都提升为k。

统计每个优先级的士兵数,然后模拟训练过程,嗯哼!好像完了。

#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <cmath>#include <string>using namespace std;int n,k;int a[111]= {0};int rank[111]= {0};int main(){    cin >> n >> k;    for (int i=1; i<=n; i++)    {        cin >> a[i];        rank[a[i]]++;    }    int ans = 0;    bool ok = true;    while (ok)    {        ok=false;        for (int i=k-1; i>=1; i--)        {            if (rank[i] > 0)            {                rank[i]--;                rank[i+1]++;                ok = true;            }        }        if (ok) ans++;    }    cout << ans << endl;    return 0;}

C题:

Bulls and Cows是一个猜数字游戏,一个人写下一个数字,另一个人猜这个数字,其中b代表位置正确的数字有几个,而c代表位置不正确的数字有几个。
其中猜的数字由 0 ~ 9 的不重复的4个数字组成。
给你n组数据,每行有三个数字,第一个是你猜的数字,第二个是b,第三个是c
现在如果可能猜出的结果有1个输出该数字,如果不止一个输出Need more data
如果结果不肯能出现输出Incorrect data。
直接暴力求解10000以内所有的结果,如果遇到求解的数字为符合所有的可能,记cnt++
如果cnt == 1 输出结果,cnt == 0 输出Incorrect data,如果cnt > 1输出Need more data。

#include <iostream>#include <cstdio>#include <cstring>using namespace std;struct Node{    int num[4];    int a,b;} node[20];int ans[4];int vis[11];bool ok(int num){    memset(vis,0,sizeof(vis));    int t;    for(int i = 0; i < 4; i++)    {        t = num % 10;        if(vis[t])        {            return false;        }        ans[i] = t;        vis[t] = 1;        num /= 10;    }    return true;}int main(){    int n;    char arr[5];    while(scanf("%d",&n) != EOF)    {        for(int i = 0; i < n; i++)        {            scanf("%s %d %d",arr,&node[i].a,&node[i].b);            for(int j = 0; j < 4; j++)            {                node[i].num[j] = arr[j] - '0';            }        }        int cnt1,cnt2,cnt;        cnt = 0;        int res;        for(int i = 1; i < 10000; i++)        {            if(ok(i))            {                int j;                for(j = 0; j < n; j++)                {                    cnt1 = cnt2 = 0;                    for(int k = 0; k < 4; k++)                    {                        if(ans[k] == node[j].num[k])                        {                            cnt1++;                        }                        if(vis[node[j].num[k]])                        {                            cnt2++;                        }                    }                    if(cnt1 != node[j].a || cnt2 - cnt1 != node[j].b)                    {                        break;                    }                }                if(j == n)                {                    res = i;                    cnt++;                }            }        }        if(cnt == 1)        {            for(int i = 0; i < 4; i++)            {                printf("%d",res%10);                res /= 10;            }            printf("\n");        }        else if(cnt > 1)        {            printf("Need more data\n");        }        else        {            printf("Incorrect data\n");        }    }    return 0;}


D题:在一个字符串里面按着hello字符顺序找hello,找到就YES,否则NO;
贪心,挨着找过去就行了。

#include <cstdio>#include <algorithm>#include <cstring>#include <cmath>#include <iostream>#include <string>using namespace std;string s;string word = "hello";int main(){    while(cin >> s)    {        int flag = 0;        for(int i=0;i<s.size();i++)        {            if(s[i] == word[flag])                flag++;            if(flag == 5)                break;        }        if(flag == 5)            printf("YES\n");        else            printf("NO\n");    }    return 0;}

E题:生成保证一个数是前面出现所有数的约数的数组,保证数组最长。
只要枚举前一个数的所有约数,只要这个数能将前面一个数整除,必然能将前面的所有的数整除。
贪心思想:保证当前生出的约数最大,后面可找到更多的数。


#include <cstdio>#include <algorithm>#include <cstring>#include <cmath>#include <iostream>#include <string>using namespace std;int main(){    int n;    cin >> n;    cout << n;    while(n>1)    {        int i;        for(i=2; i<n; i++)            if(n%i == 0)            {                n = n/i;                cout << " " << n;                break;            }        if(i == n)        {            cout << " " << 1;            break;        }    }    return 0;

F题:给一个序列代表一排树。现在需要把这排树砍成beautiful的。怎么叫beautiful的看题目。

呜~ 大概你需要明白2件事情。
1.beautiful sequence is can be determined with any member
2.at least one tree will remain the same height.
然后我们就统计一下,前一半中相对距离相同数量最多的那些树,后一半镜像统计,这个数量表示的就是我们最多可以不动的树的数量。
然后用n个去减掉这个最大数量,就是需要改变最小数量。


#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 100010;int a[maxn], d[maxn];int main(){    int n;    while(scanf("%d", &n)!=EOF)    {        for(int i=1; i<=n; i++)            scanf("%d",&a[i]);        memset(d,0,sizeof(d));        int k = n/2 + n%2;        for(int i=1; i<=k; i++)        {            if(a[i]-i+1>0)                d[a[i]-i+1]++;        }        for(int i=n; i>k; i--)            if(a[i]+i-n>0)                d[a[i]+i-n]++;        int ma = -1;        for(int i=1; i<=100000; i++)            ma = max(ma, d[i]);        printf("%d\n", n - ma);    }    return 0;}

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 付款了!卖家不想卖了怎么办? 手机通讯录同步助手密码忘了怎么办 毕业生学位信息照片上传错了怎么办 报名计算机二级照片上传很慢怎么办 网上报名时照片上传错了怎么办 特岗教师报名时照片上传模糊怎么办 百家号申请过新手被拒绝三次怎么办 6岁儿童个子高不长肉体形瘦怎么办 网页游戏加载完毕卡了不动怎么办 小番茄未成熟下面就坏掉是怎么办 千牛快捷短语界面变得好宽怎么办 申请大王卡手机号填错了怎么办 别人盗用我手机号申请微信号怎么办 淘宝支付宝里没钱了买家退款怎么办 如果外包把员工社保忘交了怎么办 外包工人没有和包工头的证据怎么办 试用期辞职公司给交的社保怎么办 单位给交员工不想交社保怎么办 淘宝收不到卖家的信息怎么办 不小心把微信聊天记录删掉了怎么办 不小心把打印机驱动删除了怎么办 蓝牙不小心点到忽略此设备怎么办 千牛工作台无线开店确认不了怎么办 开通诚信通后营业执照注销了怎么办 淘宝标的货跟发的不一样怎么办 wps表格里单元之间重叠了怎么办 淘宝店铺停了一段时间没了怎么办 转转买家收货为敲诈卖家怎么办 淘宝被投诉盗用官网图片怎么办 淘宝订单买下后卖家告知无货怎么办 盗图被删除还是待处理违规该怎么办 如果买家说你们的买家秀一样怎么办 花呗唤起安全核身验证失败怎么办 淘宝买东西花呗分期额度不够怎么办 支付宝余额支付额度已达上限怎么办 我是淘宝卖家遇到无良买家怎么办 我的保证金被淘宝当做违约金怎么办 淘宝顾客不想退货申请仅退款怎么办 被买家提供证明说我卖假货怎么办 淘宝退货快递公司填错了俩次怎么办 淘宝上退货把运单号写错了怎么办?