【ACM】2022.08.21训练赛

给出一个正整数N,将N写为若干个连续数字和的形式(长度 >= 2)。例如 N = 15,可以写为 1 + 2 + 3 + 4 + 5,也可以写为 4 + 5 + 6,或 7 + 8。如果不能写为若干个连续整数的和,则输出 No Solution

输入1个数N (3 <= n <="10^9)</code">&#x3002;<!--=-->

输出连续整数中的第1个数,如果有多个按照递增序排列,如果不能分解为若干个连续整数的和,则输出 No Solution

1
4
7

直接暴力的话,肯定会TLE
已知等差数列的公式(S=na_1+\frac{n(n-1)}{2}d),并且公差(d=1),得(a_1=\frac{2S-n(n-1)}{2n})

#include
#include
#include
#include
#include

using namespace std;

int main()
{
    int n;
    cin >> n;

    int q = sqrt(2*n);
    bool flag = false;

    for(int i = q + 1;i > 1;i --)
    {
        if( (2*n - i*(i-1)) % (2*i) == 0 && 2*n - i*(i-1) > 0)
        {
            cout << (2*n - i*(i-1) ) / (2*i) << endl;
            flag = true;
        }
    }

    if(!flag)
        cout << "No Solution" << endl;
    return 0;
}

对于半径为r的球,其体积的计算公式为(V=4/3*πr^3),这里取 &#x3C0;= 3.14。现给定r,求V。

输入为一个不超过100的非负实数,即球半径,类型为double。

输出一个实数,即球的体积,保留到小数点后2位。

267.95

给出公式了,直接写

#include
#include
#include
#include
#include

using namespace std;

#define PI 3.14

int main()
{
    double r;
    cin >> r;

    double ans = 4.0/3.0 * PI * r * r * r;

    printf("%.2lf",ans);

    return 0;
}

一个自然数的因数是指能整除它的所有自然数。例如6的因数为:1,2,3,6。
现在给出一个数n,求因数之和为n的最小的正数是多少(如果找不到这样的数,输出-1)。

一个数 n &#xFF08;1 <= n <="10000&#xFF09;</code"><!--=-->

一个数 a

从1开始循环到n,判断有没有符合条件的.

其中cheak()函数,就是计算x的因数和

#include
#include
#include
#include
#include

using namespace std;

int n;

bool check(int x)
{
    int ans = 0;
    for(int i = 1;i > n;
    for(int i = 1;i

身份证号是我国公民的唯一识别码,它由 18 位数字或者字母组成(只可能最后一位是字母)。18 位身份证号码的含义如下:第 1~2 为省、自治区、直辖市代码:第 3~4 位为地级市、盟、自治州代码;第 5~6 位为县、县级市、区代码。第 7~14 位为出生年月日,比如 19970401 代表 1997 年 4 月 1 日;第 15~16 位为顺序号,第 17 位代表性别,男为单数,女为双数,第 18 位为校验码,0~9 和 X。作为尾号的校验码,是把前十七位数字代入统一的公式计算出来的。解答本题你不用关心是如何计算出来的。现在给你 n 个身份证号码,请你按照出生年月日的字典序(年龄从大到小)输出第 m 个人的身份证号。

一些解释:虽然造数据的人非常辛苦的制造各种各样的身份证号(并且让他们生日互不相同),但是你并不需要验证关于身份证号的任何合法性,包括省市区是否合法,出生年月日是否合法,校验值是否合法,你需要做的仅仅是输出年龄从大到小第m个人的身份证号。

输入第一行包含两个正整数 n 和 m,两数之间用一个空格分隔,接下来的 n 行每行为一个形如上述格式的身份证号码(不需要关心校验码的正确性,不影响本题解答)。 &#xFF08;1&#x2264;n&#x2264;100&#xFF0C;1&#x2264;m&#x2264;n&#xFF09;

输出仅包含一行,为题目要求的一个身份证号码。

13021119640203652X

模拟,用sort函数排序后,直接输出第m大的身份证号

#include
#include
#include
#include
#include

using namespace std;

const int N = 110;
string s[N];

bool cmp(string a,string b)
{
    for(int i = 6;i < 14;i ++)
    {
        if(a[i] != b[i])
            return a[i] < b[i];
    }
}

int main()
{
    int n , m;
    cin >> n >> m;

    for(int i = 1;i > s[i];

    sort(s+1,s+n+1,cmp);

    cout << s[m] << endl;

    return 0;
}

给出一个长度为 (n) 的单词,写出一个程序,执行以下操作:

  • 删除单词里的元音字母。
  • 辅音字母前加上字符 .
  • 所有大写字母全部转化为小写字母。

注:元音字母有 (\text{A,O,Y,E,U,I}),其余的全部都是辅音字母。

输入仅一行,为一个单词(仅包含大小写字母)。
输出仅一行,代表经程序处理后的单词。

数据范围:(1\leqslant n\leqslant 100)。

Petya started to attend programming lessons. On the first lesson his task was to write a simple program. The program was supposed to do the following: in the given string, consisting if uppercase and lowercase Latin letters, it:

  • deletes all the vowels,
  • inserts a character “.” before each consonant,
  • replaces all uppercase consonants with corresponding lowercase ones.

Vowels are letters “A”, “O”, “Y”, “E”, “U”, “I”, and the rest are consonants. The program’s input is exactly one string, it should return the output as a single string, resulting after the program’s processing the initial string.

Help Petya cope with this easy task.

The first line represents input string of Petya’s program. This string only consists of uppercase and lowercase Latin letters and its length is from $ 1 $ to $ 100 $ , inclusive.

Print the resulting string. It is guaranteed that this string is not empty.

tour
.t.r
Codeforces
.c.d.f.r.c.s
aBAcAba
.b.c.b

模拟。
先将所有的字母变成小写,然后在循环找出辅音字母

#include
#include
#include
#include
#include

using namespace std;

char vow[] = {'a','o','y','e','u','i'};

int main()
{
    string s , ans;
    cin >> s;

    for(int i = 0;i < s.size();i ++)
    {
        if(s[i] >= 'A' && s[i]

你有 (n) 个长度为 (m) 的单词,第 (i) 个单词是 (s_i)。

在一个操作中,你可以把任何一个单词的任何一个位置上的字母改成它相邻的字母,比如:

  • 你可以把 ‘e’ 改成 ‘d’ 或 ‘f’。
  • 你只能把 ‘a’ 改成 ‘b’。
  • 你只能把 ‘z’ 改成 ‘y’。

(s_i) 与 (s_j) 的差异度为使 (s_i=s_j) 所需的操作次数,如 “best” 与 “cost” 差异度为 (1+10+0+0=11)。

你要找到 (s_i) 与 (s_j)(满足 (i

You are given $ n $ words of equal length $ m $ , consisting of lowercase Latin alphabet letters. The $ i $ -th word is denoted $ s_i $ .

In one move you can choose any position in any single word and change the letter at that position to the previous or next letter in alphabetical order. For example:

  • you can change ‘e’ to ‘d’ or to ‘f’;
  • ‘a’ can only be changed to ‘b’;
  • ‘z’ can only be changed to ‘y’.

The difference between two words is the minimum number of moves required to make them equal. For example, the difference between “best” and “cost” is $ 1 + 10 + 0 + 0 = 11 $ .

Find the minimum difference of $ s_i $ and $ s_j $ such that $ (i < j) $ . In other words, find the minimum difference over all possible pairs of the $ n $ words.

The first line of the input contains a single integer $ t $ ( $ 1 \le t \le 100 $ ) — the number of test cases. The description of test cases follows.

The first line of each test case contains $ 2 $ integers $ n $ and $ m $ ( $ 2 \leq n \leq 50 $ , $ 1 \leq m \leq 8 $ ) — the number of strings and their length respectively.

Then follows $ n $ lines, the $ i $ -th of which containing a single string $ s_i $ of length $ m $ , consisting of lowercase Latin letters.

For each test case, print a single integer — the minimum difference over all possible pairs of the given strings.

6
2 4
best
cost
6 3
abb
zba
bef
cdu
ooo
zzz
2 7
aaabbbc
bbaezfe
3 2
ab
ab
ab
2 8
aaaaaaaa
zzzzzzzz
3 1
a
u
y
11
8
35
0
200
4

For the second test case, one can show that the best pair is (“abb”,”bef”), which has difference equal to $ 8 $ , which can be obtained in the following way: change the first character of the first string to ‘b’ in one move, change the second character of the second string to ‘b’ in $ 3 $ moves and change the third character of the second string to ‘b’ in $ 4 $ moves, thus making in total $ 1 + 3 + 4 = 8 $ moves.

For the third test case, there is only one possible pair and it can be shown that the minimum amount of moves necessary to make the strings equal is $ 35 $ .

For the fourth test case, there is a pair of strings which is already equal, so the answer is $ 0 $ .

从数据范围来看(508100 = 40000)
因此可以双重循环,遍历,模拟出答案

#include
#include
#include
#include
#include

using namespace std;

const int N = 1010;
const int INF = 0x3f3f3f3f;

int n , m;
string s[N];

void solve()
{
    cin >> n >> m;

    for(int i = 0;i < n;i ++)
        cin >> s[i];

    int ans = INF;

    for(int i = 0;i < n;i ++)
    {
        for(int j = i +1;j < n;j ++)
        {
            int sum = 0;
            for(int k = 0;k < m;k ++)
                sum += abs(s[i][k] - s[j][k]);
            ans = min(ans,sum);
        }
    }

//  cout << "ans = ";
    cout << ans << endl;
}

int main()
{
    int T;
    cin >> T;

    while(T --) solve();

    return 0;
}

Alice 和 Bob 从他们的父母那里收到 (n) 颗糖果。每一颗糖果重达 (1) 或 (2) 克。现在,他们想要公平地将糖果分为两组,使得两组糖果的重量只和相等。

请你判断是否可以实现。注意,糖果都不能被切成两半。

输入第一行一个整数 (t)((1\le t\le 10^4)),表示测试数据的组数。

接下来,每组数据第一行一个整数 (n)((1\le n\le 100)),表示两人收到的糖果数量。

接下来一行 (n) 个整数 (a_1,a_2,\dots,a_n),表示每颗糖果的重量。糖果的重量为 (1) 或 (2)。

输出共 (t) 行。对于每组测试数据,输出一行一个字符串、如果可以分成重量相等的两组,则输出 YES,否则输出 NO

Alice and Bob received $ n $ candies from their parents. Each candy weighs either 1 gram or 2 grams. Now they want to divide all candies among themselves fairly so that the total weight of Alice’s candies is equal to the total weight of Bob’s candies.

Check if they can do that.

Note that candies are not allowed to be cut in half.

The first line contains one integer $ t $ ( $ 1 \le t \le 10^4 $ ) — the number of test cases. Then $ t $ test cases follow.

The first line of each test case contains an integer $ n $ ( $ 1 \le n \le 100 $ ) — the number of candies that Alice and Bob received.

The next line contains $ n $ integers $ a_1, a_2, \ldots, a_n $ — the weights of the candies. The weight of each candy is either $ 1 $ or $ 2 $ .

It is guaranteed that the sum of $ n $ over all test cases does not exceed $ 10^5 $ .

For each test case, output on a separate line:

  • “YES”, if all candies can be divided into two sets with the same weight;
  • “NO” otherwise.

You can output “YES” and “NO” in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive).

5
2
1 1
2
1 2
4
1 2 1 2
3
2 2 2
3
2 1 2
YES
NO
YES
NO
NO

In the first test case, Alice and Bob can each take one candy, then both will have a total weight of $ 1 $ .

In the second test case, any division will be unfair.

In the third test case, both Alice and Bob can take two candies, one of weight $ 1 $ and one of weight $ 2 $ .

In the fourth test case, it is impossible to divide three identical candies between two people.

In the fifth test case, any division will also be unfair.

对于糖果来说,有以下四种情况

  • 总糖果数为
  • a1为偶数,a2随意
    必定能够平分
  • a1为奇数时
    无法平分
  • 总糖果数为
  • a1为偶数,a2随意
    必定能够平分
#include
#include
#include
#include
#include

using namespace std;

int main()
{
    int T;
    cin >> T;

    while(T --)
    {
        int n;
        cin >> n;

        int a1 = 0;
        int a2 = 0;
        while(n --)
        {
            int x;
            cin >> x;
            if(x == 1)
                a1 ++;
            else
                a2 ++;
        }

        if((a1+a2) % 2)
        {
            if(a1 % 2 == 0 && a1 != 0)
                puts("YES");
            else
                puts("NO");
        }
        else
        {
            if(a1 % 2 == 0)
                puts("YES");
            else
                puts("NO");
        }
    }

    return 0;
}

Original: https://www.cnblogs.com/heystar/p/16610211.html
Author: HeyStar
Title: 【ACM】2022.08.21训练赛

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/587902/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • Java对象序列化和反序列化

    Java类的序列化和反序列化 序列化:指将对象转换为字节序列的过程,也就是将对象的信息转换成文件保存。 反序列化:将字节序列转换成目标对象的过程,也就是读取文件,并转换为对象。 几…

    数据结构和算法 2023年6月16日
    064
  • 力扣102. 二叉树的层序遍历

    102. 二叉树的层序遍历 102. 二叉树的层序遍历难度:中等描述:给你二叉树的根节点 root ,返回其节点值的 层序遍历 。 (即逐层地,从左到右访问所有节点)。 示例 1:…

    数据结构和算法 2023年6月16日
    0120
  • [数据结构]ODT(珂朵莉树)实现及其应用,带图

    需要一种这样的数据结构,需要支持区间的修改,区间不同值的分别操作。 一般的,我们会想到用线段树或者Splay等支持序列操作的数据结构。但是我们这里讲引入一种更加简单的数据结构。 O…

    数据结构和算法 2023年6月7日
    085
  • D.Extreme Subtraction(贪心,构造)【CF 1443】

    传送门 样例(x): 8 15 16 17 19 27 36 29 33 结果(t1) 15 15 16 18 26 35 28 32 思路:我们可以把最左端和最右端当做两个水龙头…

    数据结构和算法 2023年6月7日
    077
  • 区块链demo-python

    import hashlib import json from textwrap import dedent from time import time from uuid imp…

    数据结构和算法 2023年6月7日
    083
  • A与B的对话之线索二叉树

    A:什么是线索二叉树? B:先别着急,你知道二叉树的定义的存储结构是什么吗? A:当然知道,就是包含一个值域和两个指针域的结构体,下图所示 typedef struct node …

    数据结构和算法 2023年6月7日
    075
  • 将博客搬至CSDN

    现将本博客内容迁移至csdn posted @2022-08-29 20:22 Johnson-Hugo 阅读(8 ) 评论() 编辑 Original: https://www….

    数据结构和算法 2023年6月7日
    083
  • st表树状数组入门题单

    树状数组用来维护一个具有区间可减(加)性质的工具,所以可以用来维护区间前缀和。区间最值不具有区间可减性,所以不能使用树状数组进行维护,而使用st表。 初始化 &#x9884…

    数据结构和算法 2023年6月7日
    071
  • 匈牙利算法

    这就是NTR算法 ?? 渣男渣女算法 ?? 接下来要介绍的NTR算法,啊呸,不对不对,匈牙利算法,是一种确定二分图的最大匹配数量的一种非常高效的算法; 我们先介绍一下二分图的匹配以…

    数据结构和算法 2023年6月7日
    090
  • CF1468H K and Medians 题解

    每次删数都会删恰好 (k-1) 个,所以删的数总个数必须是 (k-1) 的倍数。考虑最终状态,如果所有数左边不足 (\frac{k-1}{2}) 个删掉的数或右边不足 (\frac…

    数据结构和算法 2023年6月12日
    064
  • 算法:斐波那契数列

    问题 写一个函数,输入 n ,求斐波那契(Fibonacci)数列的第 n 项(即 F(N))。斐波那契数列的定义如下: F(0) = 0, F(1) = 1 F(N) = F(N…

    数据结构和算法 2023年6月12日
    069
  • [笔记]浅谈四边形不等式

    0 前言 四边形不等式是用来优化区间dp的,它可以让 (O(n^3) \to O(n^2))。所以考虑一道区间dp的题是否要用四边形不等式优化,看 n 的范围。 1 四边形不等式怎…

    数据结构和算法 2023年6月8日
    073
  • 最短编辑距离

    给定 n 个长度不超过 10 的字符串以及 m 次询问,每次询问给出一个字符串和一个操作次数上限。 对于每次询问,请你求出给定的 n 个字符串中有多少个字符串可以在上限操作次数内经…

    数据结构和算法 2023年6月7日
    066
  • Simulink S-Function的使用(以串口接收MPU6050六轴陀螺仪参数为实例)

    S-Function 允许使用自定义C/C++函数作为传递函数,具有可移植性。也可以同样利用MATLAB函数进行相同的运算,看开发者熟悉程度而定。 项目流程 由系统串口接收数据包。…

    数据结构和算法 2023年6月7日
    072
  • 倒数第N个字符

    给定一个完全由小写英文字母组成的字符串等差递增序列,该序列中的每个字符串的长度固定为 L,从 L 个 a 开始,以 1 为步长递增。例如当 L 为 3 时,序列为 { aaa, a…

    数据结构和算法 2023年6月16日
    0111
  • 网课期间 の 总结

    7.19 T1 又是空间问题(养成看空间限制的习惯啊!!!),那个一前一后看成二元组也挺巧妙的。 T3: 有个 坐标旋转 的优化,感觉很妙诶,这样就可以避免一些特殊的数据点。 一个…

    数据结构和算法 2023年6月7日
    065
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球