A.连续整数的和【数论】
题目描述
给出一个正整数N,将N写为若干个连续数字和的形式(长度 >= 2)。例如 N = 15
,可以写为1 + 2 + 3 + 4 + 5
,也可以写为4 + 5 + 6
,或7 + 8
。如果不能写为若干个连续整数的和,则输出No Solution
。
输入
输入1个数N(3 <= N <= 10^9)
。
输出
输出连续整数中的第1个数,如果有多个按照递增序排列,如果不能分解为若干个连续整数的和,则输出No Solution
。
输入样例
15
输出样例
1
4
7
思路
直接暴力的话,肯定会TLE
已知等差数列的公式\(S=na_1+\frac{n(n-1)}{2}d\),并且公差\(d=1\),得\(a_1=\frac{2S-n(n-1)}{2n}\)
C++代码
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
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;
}
B.计算球的体积【签到】
题目描述
对于半径为r的球,其体积的计算公式为\(V=4/3*πr^3\),这里取π= 3.14
。现给定r,求V。
输入
输入为一个不超过100的非负实数,即球半径,类型为double。
输出
输出一个实数,即球的体积,保留到小数点后2位。
样例输入
4
样例输出
267.95
思路
给出公式了,直接写
C++代码
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
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;
}
C.因数之和为n的最小正数【模拟】
题目描述
一个自然数的因数是指能整除它的所有自然数。例如6的因数为:1,2,3,6。
现在给出一个数n,求因数之和为n的最小的正数是多少(如果找不到这样的数,输出-1)。
输入
一个数 n(1 <= n <= 10000)
输出
一个数 a
输入样例
6
输出样例
5
思路
从1开始循环到n,判断有没有符合条件的.
其中cheak()函数,就是计算x的因数和
C++代码
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
int n;
bool check(int x)
{
int ans = 0;
for(int i = 1;i <= x;i ++)
{
if(x % i == 0)
ans += i;
}
if(ans == n)
return true;
else
return false;
}
int main()
{
cin >> n;
for(int i = 1;i <= n;i ++)
{
if(check(i))
{
cout << i << endl;
return 0;
}
}
cout << -1 << endl;
return 0;
}
F.第m大的身份证号码【模拟】
题目描述
身份证号是我国公民的唯一识别码,它由 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 行每行为一个形如上述格式的身份证号码(不需要关心校验码的正确性,不影响本题解答)。(1≤n≤100,1≤m≤n)
输出
输出仅包含一行,为题目要求的一个身份证号码。
输入样例
4 2
110108196004063022
13021119640203652X
420333197902112718
210222200012036512
输出样例
13021119640203652X
思路
模拟,用sort函数排序后,直接输出第m大的身份证号
C++代码
#include <iostream>
#include <Cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
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 <= n;i ++)
cin >> s[i];
sort(s+1,s+n+1,cmp);
cout << s[m] << endl;
return 0;
}
G.String Task【模拟】
题面翻译【转自洛谷】
给出一个长度为 \(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.
样例 #1
样例输入 #1
tour
样例输出 #1
.t.r
样例 #2
样例输入 #2
Codeforces
样例输出 #2
.c.d.f.r.c.s
样例 #3
样例输入 #3
aBAcAba
样例输出 #3
.b.c.b
思路
模拟。
先将所有的字母变成小写,然后在循环找出辅音字母
C++代码
#include <iostream>
#include <Cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
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] <= 'Z')
s[i] -= 'A' - 'a';
if(s[i] != vow[0] && s[i] != vow[1] &&s[i] != vow[2] &&s[i] != vow[3] &&s[i] != vow[4] &&s[i] != vow[5])
{
ans = ans + '.' + s[i];
}
}
cout << ans << endl;
return 0;
}
H.Most Similar Words【模拟】
题面翻译
你有 \(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<j\))的差异度的最小值,也就是每一对单词差异度的最小值。
题目描述
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.
样例 #1
样例输入 #1
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
样例输出 #1
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 $ .
思路
从数据范围来看\(50*8*100 = 40000\)
因此可以双重循环,遍历,模拟出答案
C++代码
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
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;
}
I.Fair Division【贪心】
题面翻译【转自洛谷】
题目描述
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
。
translate by @1289H2051N343O375S8。
题目描述
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).
样例 #1
样例输入 #1
5
2
1 1
2
1 2
4
1 2 1 2
3
2 2 2
3
2 1 2
样例输出 #1
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.
思路
对于糖果来说,有以下四种情况
- 总糖果数为偶数
1.a1为偶数,a2随意
必定能够平分
2.a1为奇数时
无法平分 - 总糖果数为奇数
1.a1为偶数,a2随意
必定能够平分
C++代码
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
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;
}
标签:输出,21,int,样例,ACM,训练赛,test,include,string
From: https://www.cnblogs.com/heystar/p/16610211.html