ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

2020.6.9 习题练习五

2020-06-13 09:01:34  阅读:220  来源: 互联网

标签:int 2020.6 练习 .... cin #### ###### 习题 include


A - Sum of Odd Integers

题意:给出n和k,问n是否能表示为k个不同的奇数的和

做法:根据规律,n和k如果奇偶性不同是无法满足题意的,而且k个正奇数相加的最小数字一定等于k * k,所以如果k * k>n的话也不满足题意,只需要判断这些条件即可

代码:

//去吧马里奥!把AC公主救回来!
//        ********
//       ************
//       ####....#.
//     #..###.....##....
//     ###.......######
//        ...........
//       ##*#######
//    ####*******######
//   ...#***.****.*###....
//   ....**********##.....
//   ....****    *****....
//     ####        ####
//   ######        ######
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<string>
#include<map>
#include<sstream>
#include<cstring>
#include<vector>
#include<iomanip>
#include<queue>
#include<set>
#define LL long long
#define _64 __int64
const double PI = atan(1.)*4.;
using namespace std;

int main(){
    int t;
    cin >> t;
    while(t--){
        LL n,k;
        cin >> n >> k;
        if(((n % 2 == 0 && k % 2 == 0) || (k % 2 == 1 && n % 2 == 1)) && n >= k*k){
            cout << "YES" << endl;
        }else{
            cout << "NO" << endl;
        }
    }
}

B - Princesses and Princes

题意:题干劈里啪啦一大堆,其实就是说给公主和王子找对象,每个公主喜欢的王子不一样,每个公主只能和一个王子配对,问能不能全部配成功,不能就手动配一对,随便哪对都行

做法:开三个数组,分别储存公主有没有对象,王子有没有,和公主的意愿,之后遍历匹配就完了,匹配到一个就跳出找下一个公主,最后根据情况输出就行,还有就是这个用memset铁定会超时,需要用循环进行数组初始化

代码:

//去吧马里奥!把AC公主救回来!
//        ********
//       ************
//       ####....#.
//     #..###.....##....
//     ###.......######
//        ...........
//       ##*#######
//    ####*******######
//   ...#***.****.*###....
//   ....**********##.....
//   ....****    *****....
//     ####        ####
//   ######        ######
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<string>
#include<map>
#include<sstream>
#include<cstring>
#include<vector>
#include<iomanip>
#include<queue>
#include<set>
#define LL long long
#define _64 __int64
const double PI = atan(1.)*4.;
using namespace std;

int g[200005],w[200005],wish[200005];

int main(){
    int t;
    cin >> t;
    while(t--){
        //memset(g,0,sizeof(g));
        //memset(w,0,sizeof(w));
        //memset(wish,0,sizeof(wish));

        int n;
        cin >> n;
        for (int i = 1; i <= n;i++){
            g[i]=0;
            w[i]=0;
            wish[i]=0;
        }
        for(int i = 1;i <= n;i++){
            int x;
            cin >> x;
            for(int j = 1;j <= x;j++){
                cin >> wish[j];
            }
            for(int k = 1;k <= x;k++){
                if(w[wish[k]] == 0){
                    w[wish[k]] = 1;
                    g[i] = 1;
                    break;
                }
            }
        }
        int flag = 0;
        for(int i = 1;i <= n;i++){
            if(g[i] == 0){
                cout << "IMPROVE" << endl;
                for(int j = 1;j <= n;j++){
                    if(w[j] == 0){
                        cout << i << ' ' << j << endl;
                        flag = 1;
                    }
                    if(flag == 1)break;
                }
            }
            if(flag == 1)break;
        }
        if(flag == 0){
            cout << "OPTIMAL" << endl;
        }

    }
}

C - EhAb AnD gCd

题意:给你x,要求GCD(a,b)+LCM(a,b)=x

做法:GCD是可以被a和b整除的最大数,LCM是可以整除a和b的最小数,之后我们知道GCD(a,1)=1,LCM(1,a)=a,由此可知,最后要等于x,那把a换成a-1就行,也就是直接输出x-1和1就行

代码:

//去吧马里奥!把AC公主救回来!
//        ********
//       ************
//       ####....#.
//     #..###.....##....
//     ###.......######
//        ...........
//       ##*#######
//    ####*******######
//   ...#***.****.*###....
//   ....**********##.....
//   ....****    *****....
//     ####        ####
//   ######        ######
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<string>
#include<map>
#include<sstream>
#include<cstring>
#include<vector>
#include<iomanip>
#include<queue>
#include<set>
#define LL long long
#define _64 __int64
const double PI = atan(1.)*4.;
using namespace std;

int main(){
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        cout << n-1 << ' ' << 1 << endl;
    }
}

D - CopyCopyCopyCopyCopy

题意:有一个数组长度为n,要求n个这样的数组链接在一起,通过删去一部分数得到最长的递增序列

做法:先链接(其实也不用),直接用其中一段就行,因为是重复的,从第一组找最小的,第二组找第二小的……

之后其实直接用一组之后去重就可以了,而且关键是题目要求输出的是序列长度,直接排序之后用去重unique跑一下就ok

代码:

//去吧马里奥!把AC公主救回来!
//        ********
//       ************
//       ####....#.
//     #..###.....##....
//     ###.......######
//        ...........
//       ##*#######
//    ####*******######
//   ...#***.****.*###....
//   ....**********##.....
//   ....****    *****....
//     ####        ####
//   ######        ######
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<string>
#include<map>
#include<sstream>
#include<cstring>
#include<vector>
#include<iomanip>
#include<queue>
#include<set>
#define LL long long
#define _64 __int64
const double PI = atan(1.)*4.;
using namespace std;

int a[200005];

int main(){
    int t;
    cin >>t;
    while(t--){
        int n;
        cin >> n;
        for(int i = 0;i < n;i++){
            cin >> a[i];
        }
        sort(a,a+n);
        int num;
        num = unique(a,a+n)-a;
        cout << num << endl;
    }
}

F - Yet Another Tetris Problem

题意:俄罗斯方块,有n个数,从左到右表示这个位置有几个方块,现在可以给每个位置叠方块,一次只能加上两个方块,问能不能将所有方块消除

做法:先排序找出最大的,之后将剩下的挨个和最大的比较,如果差值为奇数那就不可能了

代码:

//去吧马里奥!把AC公主救回来!
//        ********
//       ************
//       ####....#.
//     #..###.....##....
//     ###.......######
//        ...........
//       ##*#######
//    ####*******######
//   ...#***.****.*###....
//   ....**********##.....
//   ....****    *****....
//     ####        ####
//   ######        ######
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<string>
#include<map>
#include<sstream>
#include<cstring>
#include<vector>
#include<iomanip>
#include<queue>
#include<set>
#define LL long long
#define _64 __int64
const double PI = atan(1.)*4.;
using namespace std;

int a[20005];

int main(){
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        for(int i = 0;i < n;i++){
            cin >> a[i];
        }
        sort(a,a+n);
        int num;
        int flag = 0;
        for(int i = 0;i < n;i++){
            num = a[n-1]-a[i];
            if(num % 2 == 1){
                cout << "NO" << endl;
                flag = 1;
                break;
            }
        }
        if(flag == 0){
            cout << "YES" << endl;
        }
    }
}

G - Yet Another Palindrome Problem

题意:给一个数组,问能否删去一些数使其变成长度至少为3的回文串

做法:只需要直接判断是否存在长度为3的回文就行,从头开始判断即可,用两个循环

代码:

//去吧马里奥!把AC公主救回来!
//        ********
//       ************
//       ####....#.
//     #..###.....##....
//     ###.......######
//        ...........
//       ##*#######
//    ####*******######
//   ...#***.****.*###....
//   ....**********##.....
//   ....****    *****....
//     ####        ####
//   ######        ######
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<string>
#include<map>
#include<sstream>
#include<cstring>
#include<vector>
#include<iomanip>
#include<queue>
#include<set>
#define LL long long
#define _64 __int64
const double PI = atan(1.)*4.;
using namespace std;

int a[20005];

int main(){
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        for(int i = 0;i < n;i++){
            cin >> a[i];
        }

        int flag = 0;
        for(int i = 0;i < n-2;i++){
            for(int j = i+2;j < n;j++){
                if(a[i] == a[j]){
                    cout << "YES" << endl;
                    flag = 1;
                    break;
                }
            }
            if(flag == 1){
                break;
            }
        }

        if(flag == 0){
            cout << "NO" << endl;
        }

    }
}

H - Frog Jumps

题意:一只青蛙,在0的位置,要跳到n+1的位置,中间有长度为n,包含L或R的字符串,L往左跳,R往右跳,最多能跳d米,问在保证能跳到最右面的情况下,d的最小值

做法:因为是要往右跳,所以R才是关键,因此只要求出相邻的r之间的最大值就是至少要跳的距离

代码:

//去吧马里奥!把AC公主救回来!
//        ********
//       ************
//       ####....#.
//     #..###.....##....
//     ###.......######
//        ...........
//       ##*#######
//    ####*******######
//   ...#***.****.*###....
//   ....**********##.....
//   ....****    *****....
//     ####        ####
//   ######        ######
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<string>
#include<map>
#include<sstream>
#include<cstring>
#include<vector>
#include<iomanip>
#include<queue>
#include<set>
#define LL long long
#define _64 __int64
const double PI = atan(1.)*4.;
using namespace std;

int main(){
    int t;
    cin >> t;
    while(t--){
        string s;
        cin >> s;
        int ans = 0;
        int lo = -1;
        int n = s.size();
        for(int i = 0;i < n;i++){
            if(s[i] == 'R'){
                ans = max(i-lo,ans);
                lo = i;
            }
        }

        ans = max(n-lo,ans);
        cout << ans << endl;
    }
}

 

标签:int,2020.6,练习,....,cin,####,######,习题,include
来源: https://www.cnblogs.com/CCCCrack/p/13111612.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有