记录洛谷刷题C语言qaq
[COCI2007-2008#5] PASCAL
题目描述
小 Frane 已经十年级了,但是在信息课上学习 Pascal 仍然很吃力。老师写下了如下的 Pascal 程序作为家庭作业,他需要根据输入的整数 N N N 来判断程序的输出。
readln(N);
counter := 0;
for i := N-1 downto 1 do begin
counter := counter + 1;
if N mod i = 0 then break;
end;
writeln(counter);
请您编写一个程序来解决这个问题。
输入格式
一行,一个整数 N N N。
输出格式
一行,表示以上程序的输出结果。
样例 #1
样例输入 #1
1
样例输出 #1
0
样例 #2
样例输入 #2
10
样例输出 #2
5
样例 #3
样例输入 #3
27
样例输出 #3
18
提示
对于 100 % 100\% 100% 的数据, 1 ≤ N ≤ 1 0 9 1\le N\le10^9 1≤N≤109。
本题分值按照原比赛设置,满分 30 30 30 分。
代码如下:
#include<string.h>
#include<stdio.h>
#include<math.h>
#include <stdlib.h>
int main(){
int n, m;
scanf("%d",&n);
for( m = 2;m*m <= n&&n%m;m++);
if(m*m>n)printf("%d\n",n - 1);
else printf("%d\n",n - n/ m);
return 0;
}
「Wdcfr-1」Beautiful Array
题面翻译
定义一个字符串为括号串当且仅当其仅由 (
和 )
组成。
试将一个长度为 n n n 的括号串分为 2 m 2m 2m 个子序列,子序列可以为空,且每个字符都必须分到恰好一个子序列中,使得至少 m m m 个子序列为匹配的括号串。空序列不算匹配的括号序列。无解请输出 0 0 0,否则输出 1 1 1。本题多组数据,其中数据组数为 T T T。
定义 A A A 为 B B B 的子序列当且仅当 A A A 能由 B B B 在顺序不改变的前提下删除若干元素后得到。
*样例
1
1
1 解释:你可以将第一个和第二个字符分入第一个子序列,让第二个子序列为空子序列。此时第一个子序列为 ()
,第二个为空,总计有一个匹配的括号序列,满足要求。
题目描述
In this problem, we define a sequence of (
and )
as a “bracket sequence”.
The definition of Regular Bracket Sequence is as follows:
()
is a Regular Bracket Sequence.- If
A
is a Regular Bracket Sequence, then(A)
is also a Regular Bracket Sequence. - If
A
andB
are Regular Bracket Sequences, thenAB
is also a Regular Bracket Sequence.
For example: ()
, (())
, and ()()
are all Regular Bracket Sequences, but )(
, ()(
are not.
In particular, an empty sequence is not a Regular Bracket Sequence sequence in this problem.
Now cute Ran gives you a bracket sequence
s
s
s of length
n
n
n. She wants you to construct
2
⋅
m
2\cdot m
2⋅m strictly increasing arrays. Let us denote them as
p
1
,
p
2
,
⋯
,
p
2
m
p_1,p_2,\cdots,p_{2 m}
p1,p2,⋯,p2m (you can leave any of them empty). You need to ensure that all integers between
1
∼
n
1\sim n
1∼n appear exactly once in these arrays.
An array p i = { r 1 , r 2 , ⋯ , r k } p_i=\{r_1,r_2,\cdots,r_k\} pi={r1,r2,⋯,rk} is Beautiful if { s r 1 , s r 2 , ⋯ , s r k } \{s_{r_1},s_{r_2},\cdots,s_{r_k}\} {sr1,sr2,⋯,srk} is a Regular Bracket Sequence.
Ran wonders whether it is possible to construct these arrays so that at least m m m of the 2 ⋅ m 2\cdot m 2⋅m arrays are “beautiful arrays”.
输入格式
Each test contains multiple test cases.
The first line contains an integer T T T, the number of test cases.
For each test case, the first line contains two integers n n n and m m m, and the second line contains a bracket sequence s s s.
输出格式
For each test case, print one line.
If it is possible to construct these arrays, print 1 1 1. Otherwise print 0 0 0.
样例 #1
样例输入 #1
2
2 1
()
2 99
()
样例输出 #1
1
0
提示
Explanation
For the first test case, we can construct p 1 = { 1 , 2 } p_1=\{1,2\} p1={1,2} and $ p_2={}$. So p 1 p_1 p1 is a “beautiful array”.
For the second test case, it is obvious that we cannot use two numbers to construct 99 99 99 beautiful arrays.
Constraints
1 ≤ T , n , m ≤ 200 1\le T,n,m\le 200 1≤T,n,m≤200.
代码如下:
#include<string.h>
#include<stdio.h>
#include<math.h>
#include <stdlib.h>
int main(){
int n,m,t;
char c;
scanf("%d",&t);
while(t--){
int to1 = 0,to2 = 0,ans = 0;
scanf("%d%d\n",&n,&m);
for(int i=1;i<=n;i++){
c=getchar();
if(c=='(') to1++;
else{
to2++;
if(to1) to1--,to2--,ans++;
}
}
if(ans<m) printf("0\n");
else printf("1\n");
}
}
[✗✓OI R1] 铝锤制作
题目背景
DPair 打算用米德拉什给大家带来笑容。
因为 Lillia 不知道什么是米德拉什,所以打算阻止 Ta。
为了阻止 DPair,你需要制作一个小铝锤。
如果你不知道如何制作小铝锤,你可以参考题目下方给出的视频。
或者,你也可以选择做这场月赛的签到题。
题目描述
构造一个正整数数列
a
a
a,使
a
a
a 中所有元素之积为
n
n
n,所有元素之和为
k
k
k。如果不存在这样的数列,输出 -1
。
输入格式
一行两个正整数 n , k n,k n,k。
输出格式
第一行一个整数
m
m
m,代表这个数列的长度。
接下来一行
m
m
m 个正整数
a
i
a_i
ai,代表这个数列。要求
1
≤
m
≤
1000
1\leq m \leq 1000
1≤m≤1000,
1
≤
a
i
≤
1000
1\leq a_i \leq 1000
1≤ai≤1000。
特别的,如果没有符合要求的数列,直接输出 -1
。
本题采用 Special Judge。如果有多种答案,输出任意一种即可。
样例 #1
样例输入 #1
67 68
样例输出 #1
2
1 67
样例 #2
样例输入 #2
100 1
样例输出 #2
-1
样例 #3
样例输入 #3
80 16
样例输出 #3
6
1 1 4 5 1 4
提示
【样例解释】
对于样例一,显然有
1
×
67
=
67
,
1
+
67
=
68
1\times67=67,1+67=68
1×67=67,1+67=68。
对于样例二,可以证明没有合法的解。
【数据范围】
对于
10
%
10\%
10% 的数据,
1
≤
n
,
k
≤
3
1\leq n,k \leq 3
1≤n,k≤3;
对于
30
%
30\%
30% 的数据,
1
≤
n
,
k
≤
10
1\leq n,k \leq 10
1≤n,k≤10;
另有
10
%
10\%
10% 的数据,
n
=
k
n=k
n=k;
对于
100
%
100\%
100% 的数据,
1
≤
n
,
k
≤
100
1\leq n,k \leq 100
1≤n,k≤100。
代码如下:
#include<string.h>
#include<stdio.h>
#include<math.h>
#include <stdlib.h>
int n, m, k, p, s;
int a[1007];
int main(){
scanf ("%d%d", &n, &k);
m = n;
for (int i=2; i<=m; i++){
while (n % i == 0){
a[++p] = i, n /= i, s += i;
if (s + n <= k){
a[++p] = n;
int yu = k - s - n;
printf ("%d\n", p + yu);
for (int j=1; j<=yu; j++) printf ("1 ");
for (int j=1; j<=p; j++) printf ("%d ", a[j]);
return 0;
}
}
}
puts ("-1");
return 0;
}
[COCI2021-2022#2] Kaučuk
题目描述
Kaučuk 程序只有下列三种命令:
- section \texttt{section} section:创建新的一级标题,序号从 1 1 1 开始标记。
- subsection \texttt{subsection} subsection:创建新的二级标题,序号在每个一级标题的基础上从 1 1 1 开始标记。
- subsubsection \texttt{subsubsection} subsubsection:创建新的三级标题,序号在每个二级标题的基础上从 1 1 1 开始标记。
给定 n n n 组命令及标题名称,输出所有标题序号及其名称。
输入格式
第一行一个正整数 n n n,表示命令的数量。
接下来的 n n n 行,每行输入命令名称( section \texttt{section} section、 subsection \texttt{subsection} subsection、 subsubsection \texttt{subsubsection} subsubsection 之一)和标题名称。标题名称由不超过 20 20 20 个小写英文字母组成。
输出格式
共 n n n 行,表示所有标题序号及其名称。
样例 #1
样例输入 #1
3
section zivotinje
section boje
section voce
样例输出 #1
1 zivotinje
2 boje
3 voce
样例 #2
样例输入 #2
4
section zivotinje
subsection macke
subsection psi
subsubsection mops
样例输出 #2
1 zivotinje
1.1 macke
1.2 psi
1.2.1 mops
样例 #3
样例输入 #3
4
section zivotinje
subsection psi
section voce
subsection ananas
样例输出 #3
1 zivotinje
1.1 psi
2 voce
2.1 ananas
提示
【数据规模与约定】
本题采用子任务捆绑测试。
- Subtask 1(10 pts): 1 ≤ n ≤ 3 1 \le n \le 3 1≤n≤3。
- Subtask 2(10 pts):只包含 section \texttt{section} section 命令。
- Subtask 3(10 pts):只包含 section \texttt{section} section 和 subsection \texttt{subsection} subsection 命令。
- Subtask 4(20 pts):无特殊限制。
对于 100 % 100\% 100% 的数据, n ≤ 100 n \le 100 n≤100。
【提示与说明】
题目译自 COCI 2021-2022 CONTEST #2 Task 1 Kaučuk。
本题分值按 COCI 原题设置,满分 50 50 50。
代码如下:
#include<string.h>
#include<stdio.h>
#include<math.h>
#include <stdlib.h>
int main()
{
int n;
scanf("%d",&n);
int a = 0, b = 0,c = 0;
for(int i = 0;i < n;i++)
{
char num[10001], m[10001];
scanf("%s%s",&num,&m);
if(strcmp(num,"section") == 0)
{
a++;
b = 0;
c = 0;
printf("%d %s\n",a,m);
}
else if(strcmp(num,"subsection") == 0)
{
b++;
c = 0;
printf("%d.%d %s\n",a,b,m);
}
else if(strcmp(num,"subsubsection") == 0)
{
c++;
printf("%d.%d.%d %s\n",a,b,c,m);
}
}
return 0;
}
[COCI2013-2014#4] NASLJEDSTVO
题目描述
有若干个金币,一个人将这堆金币尽量 N N N 等分并拿走了一份,剩下 O O O 个金币。
所谓『尽量 N N N 等分』,是指将这堆金币分成 N N N 堆,每堆所含的金币数是整数,且每两对金币的数量相差不超过 1 1 1。
我们约定这个人拿走的那一份金币是比较少的一份。
请你分别求出这堆金币原来最少有多少个和最多有多少个。
输入格式
第一行,一个正整数 N N N,表示分成了 N N N 等份;
第二行,一个正整数 O O O,表示拿走 N N N 份中的 1 1 1 份后剩下 O O O 个金币。
输出格式
一行,两个正整数,分别表示这堆金币原来最少有多少个和最多有多少个。
样例 #1
样例输入 #1
2
5
样例输出 #1
9 10
样例 #2
样例输入 #2
3
5
样例输出 #2
7 7
提示
【样例解释 #1】
这堆金币可能原来有 9 9 9 个,这个人可能将其分成 4 + 5 4+5 4+5 个,自己拿走 4 4 4 个;
这堆金币可能原来有 10 10 10 个,这个人可能将其分成 5 + 5 5+5 5+5 个,自己拿走 5 5 5 个。
【数据范围】
对于 100 % 100\% 100% 的数据, 2 ≤ N ≤ 15 2\le N\le 15 2≤N≤15, N ≤ O ≤ 100 N\le O\le 100 N≤O≤100。
【来源】
本题分值按 COCI 原题设置,满分 50 50 50。
题目译自 COCI2013-2014 CONTEST #4 T1 NASLJEDSTVO。
代码如下:
#include<string.h>
#include<stdio.h>
#include<math.h>
#include <stdlib.h>
int n,o;
int Max,Min;
int main() {
scanf("%d%d",&n,&o);
Max=o/(n-1.0)*n;
if(Max%n==0) Min=Max-1;
else Min=Max;
printf("%d %d",Min,Max);
return 0;
}