• CodeForces刷题C语言:Next Test、Spit Problem、Traffic Lights、Reconnaissance、Borze


    记录洛谷刷题C语言


    一、Next Test

    题面翻译

    题面描述

    给出 n n n 个互不相同的整数 a i a_i ai ,从小到大找第一个没有出现过的整数。

    输入格式

    第一行一个正整数 n n n ,之后是 n n n 个整数 a i a_i ai

    输出格式

    一个整数 x x x ,即第一个没有出现过的整数。

    数据范围与约定

    1 ≤ n ≤ 3000 1\leq n\leq 3000 1n3000

    1 ≤ a i ≤ 3000 1\leq a_i\leq 3000 1ai3000

    题目描述

    «Polygon» is a system which allows to create programming tasks in a simple and professional way. When you add a test to the problem, the corresponding form asks you for the test index. As in most cases it is clear which index the next test will have, the system suggests the default value of the index. It is calculated as the smallest positive integer which is not used as an index for some previously added test.

    You are to implement this feature. Create a program which determines the default index of the next test, given the indexes of the previously added tests.

    输入格式

    The first line contains one integer $ n $ ( $ 1<=n<=3000 $ ) — the amount of previously added tests. The second line contains $ n $ distinct integers $ a_{1},a_{2},…,a_{n} $ ( $ 1<=a_{i}<=3000 $ ) — indexes of these tests.

    输出格式

    Output the required default value for the next test index.

    样例 #1

    样例输入 #1

    3
    1 7 2
    
    • 1
    • 2

    样例输出 #1

    3
    
    • 1

    代码如下

    #include
    #include
    #include
    #include 
    int num[300001];
    int main()
    {
    	int n;
    	scanf("%d",&n);
    //	for(int i = 1;i <= 3000;i++)
    //	{
    //		num[i] = 0;
    //	}
    	for(int i = 0;i < n;i++)
    	{
    		int m;
    		scanf("%d",&m);
    		num[m] = 1;
    	}
    	
    	for(int i = 1;i <= 300001;i++)
    	{
    		if(num[i] != 1)
    		{
    			printf("%d\n",i);
    			break;
    		}
    	}
    	
    	
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    二、Spit Problem

    题面翻译

    在一条数轴上有 n n n 个点,每个点都有位置 x i x_i xi 和系数 d i d_i di,求有没有两个点的编号 i , j i,j i,j i ≠ j i\neq j i=j)使得 x i + d i = x j x_i+d_i=x_j xi+di=xj 而且 x j + d j = x i x_j+d_j=x_i xj+dj=xi

    题目描述

    In a Berland’s zoo there is an enclosure with camels. It is known that camels like to spit. Bob watched these interesting animals for the whole day and registered in his notepad where each animal spitted. Now he wants to know if in the zoo there are two camels, which spitted at each other. Help him to solve this task.

    The trajectory of a camel’s spit is an arc, i.e. if the camel in position $ x $ spits $ d $ meters right, he can hit only the camel in position $ x+d $ , if such a camel exists.

    输入格式

    The first line contains integer $ n $ ( $ 1<=n<=100 $ ) — the amount of camels in the zoo. Each of the following $ n $ lines contains two integers $ x_{i} $ and $ d_{i} $ ( $ -10{4}<=x_{i}<=10{4},1<=|d_{i}|<=2·10^{4} $ ) — records in Bob’s notepad. $ x_{i} $ is a position of the $ i $ -th camel, and $ d_{i} $ is a distance at which the $ i $ -th camel spitted. Positive values of $ d_{i} $ correspond to the spits right, negative values correspond to the spits left. No two camels may stand in the same position.

    输出格式

    If there are two camels, which spitted at each other, output YES. Otherwise, output NO.

    样例 #1

    样例输入 #1

    2
    0 1
    1 -1
    
    • 1
    • 2
    • 3

    样例输出 #1

    YES
    
    • 1

    样例 #2

    样例输入 #2

    3
    0 1
    1 1
    2 -2
    
    • 1
    • 2
    • 3
    • 4

    样例输出 #2

    NO
    
    • 1

    样例 #3

    样例输入 #3

    5
    2 -10
    3 10
    0 5
    5 -5
    10 1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    样例输出 #3

    YES
    
    • 1

    代码如下

    #include
    #include
    #include
    #include 
    int a[10000],x[10000];//定义两个数组
    int main()
    {
        int n;
        scanf("%d",&n);
        for(int i=1; i<=n; i++)
        {
            scanf("%d%d",&a[i],&x[i]);
        }
        for(int i=1; i<=n; i++)//暴力枚举
        {
            for(int j=1; j<=n; j++)
            if(i!=j)//如果他们两不是同一只骆驼
            {
                if(a[i]+x[i]==a[j]&&a[j]+x[j]==a[i])//如果他们可以互相吐口水
                {
                    printf("YES\n");
                    return 0;//可以直接结束程序
                }
            }
        }
        printf("NO\n");
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    三、Traffic Lights

    题面翻译

    题目描述

    一辆汽车以 v v v 米每秒的速度由A点驶向B点。这个动作发生在X轴上。在距离A点 d d d 米的地方有一个红绿灯。从0时刻开始,在第一个 g g g 秒里绿灯是亮的,然后在接下来的 r r r 秒内红灯亮起,在接下来 g g g 秒,绿灯亮起,如此反复。

    这辆车可以瞬间从 0 0 0 加速到 v v v ,反之亦然,也可以从 v v v 瞬间减速至 0 0 0 。车在绿灯时可以立刻通过。如果车在红灯亮起的那一刻到达红绿灯处,那么车不能够通过。但如果在绿灯亮起时到达,则可以通过。车从0时刻离开A点。

    在不违反交通规则的前提下,车从A点移动到B点最少需要多长时间?

    输入格式:

    第一行包含整数 l , d , v , g , r l,d,v,g,r l,d,v,g,r 1 ≤ l , d , v , g , r ≤ 1000 , d < l 1\leq l,d,v,g,r\leq1000,d1l,d,v,g,r1000,d<l )— A与B间的距离(米),A与红绿灯的距离,车的速度,绿灯的持续时间和红灯的持续时间。

    输出格式:

    输出一个数 — 车从A到B所需要的最少时间。你的输出需要和标准输出相差不超过 1 0 − 6 10^{-6} 106

    Translated by Khassar

    题目描述

    A car moves from point A to point B at speed $ v $ meters per second. The action takes place on the X-axis. At the distance $ d $ meters from A there are traffic lights. Starting from time 0, for the first $ g $ seconds the green light is on, then for the following $ r $ seconds the red light is on, then again the green light is on for the $ g $ seconds, and so on.

    The car can be instantly accelerated from $ 0 $ to $ v $ and vice versa, can instantly slow down from the $ v $ to $ 0 $ . Consider that it passes the traffic lights at the green light instantly. If the car approaches the traffic lights at the moment when the red light has just turned on, it doesn’t have time to pass it. But if it approaches the traffic lights at the moment when the green light has just turned on, it can move. The car leaves point A at the time 0.

    What is the minimum time for the car to get from point A to point B without breaking the traffic rules?

    输入格式

    The first line contains integers $ l $ , $ d $ , $ v $ , $ g $ , $ r $ ( $ 1<=l,d,v,g,r<=1000,d

    输出格式

    Output a single number — the minimum time that the car needs to get from point A to point B. Your output must have relative or absolute error less than $ 10^{-6} $ .

    样例 #1

    样例输入 #1

    2 1 3 4 5
    
    • 1

    样例输出 #1

    0.66666667
    
    • 1

    样例 #2

    样例输入 #2

    5 4 3 1 1
    
    • 1

    样例输出 #2

    2.33333333
    
    • 1

    代码如下

    double ans;
    int main(){
    	double l,d,v,g,r;
    	scanf("%lf%lf%lf%lf%lf",&l,&d,&v,&g,&r);
    	if(l<=d)ans=l/v;
    	else{
    		double t=d/v;
    		while(t>=g+r)t-=g+r;
    		if(t<g)ans=l/v;
    		else ans=g+r-t+l/v;
    	}
    	printf("%.8lf",ans);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    四、Reconnaissance

    题面翻译

    题目描述

    根据Berland军队的规定,一个巡逻队应当包含两名士兵。由于这两名士兵不能相差太多,它们的身高相差不能超过 d d d 厘米。Bob上尉有 n n n 名士兵在他的支队中。他们的身高分别是 a 1 , a 2 , … , a n a_1,a_2,\ldots,a_n a1,a2,,an 厘米。一些士兵有着相同的身高。Bob想知道他有多少种能从他的支队中选拔一个巡逻队出来的方案。

    方案 ( 1 , 2 ) (1,2) (1,2) ( 2 , 1 ) (2,1) (2,1) 应当被视作是不同的

    输入格式:

    第一行两个整数 n n n d d d 1 ≤ n ≤ 1000 , 1 ≤ d ≤ 1 0 9 1\leq n\leq1000,1\leq d\leq10^9 1n1000,1d109 )— Bob的支队中的士兵的数量和最大所被允许的身高差距。第二行包含个空格分开的整数 — Bob支队中所有士兵的身高。这些数不会超过 1 0 9 10^9 109

    输出格式:

    输出一个数 — 高度差不超过 d d d 的士兵组成巡逻队的方案数

    Translated by Khassar

    题目描述

    According to the regulations of Berland’s army, a reconnaissance unit should consist of exactly two soldiers. Since these two soldiers shouldn’t differ much, their heights can differ by at most $ d $ centimeters. Captain Bob has $ n $ soldiers in his detachment. Their heights are $ a_{1},a_{2},…,a_{n} $ centimeters. Some soldiers are of the same height. Bob wants to know, how many ways exist to form a reconnaissance unit of two soldiers from his detachment.

    Ways $ (1,2) $ and $ (2,1) $ should be regarded as different.

    输入格式

    The first line contains two integers $ n $ and $ d $ ( $ 1<=n<=1000,1<=d<=10^{9} $ ) — amount of soldiers in Bob’s detachment and the maximum allowed height difference respectively. The second line contains $ n $ space-separated integers — heights of all the soldiers in Bob’s detachment. These numbers don’t exceed $ 10^{9} $ .

    输出格式

    Output one number — amount of ways to form a reconnaissance unit of two soldiers, whose height difference doesn’t exceed $ d $ .

    样例 #1

    样例输入 #1

    5 10
    10 20 50 60 65
    
    • 1
    • 2

    样例输出 #1

    6
    
    • 1

    样例 #2

    样例输入 #2

    5 1
    55 30 29 31 55
    
    • 1
    • 2

    样例输出 #2

    6
    
    • 1

    代码如下

    #include
    #include
    #include
    #include 
    
    
    
    int a[1001],n,d,ans;//按题意变量开这么大就够了
    int main(){
        scanf("%d%d",&n,&d);//个人建议用scanf和printf比较好
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        for(int i=2;i<=n;i++)           
            for(int j=1;j<i;j++) //遍历,加入简单的剪枝
                if(a[i]-a[j]<=d&&a[i]-a[j]>=-d) ans+=2;//如果身高差小于d就满足,而且一次加2,省时间
        printf("%d\n",ans);//换行,好习惯
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    五、Borze

    题面翻译

    题面描述

    三进制数字符号在Berland很受欢迎。如果用borze编码表示电报的三进制数。数字 0 , 1 , 2 0,1,2 0,1,2 分别被作为.-.--。你需要为borze编码解码。(把borze编码转换为三进制数)。

    输入格式

    第一行包含在Borze编码。字符串的长度介于 1 1 1 200 200 200 个字符之间。这是保证给定的字符串是一个有效的一些三元数borze编码(这个数可能有前导零)。

    输出格式

    一个三进制数(如果有前导零要输出)。

    题目描述

    Ternary numeric notation is quite popular in Berland. To telegraph the ternary number the Borze alphabet is used. Digit 0 is transmitted as «.», 1 as «-.» and 2 as «–». You are to decode the Borze code, i.e. to find out the ternary number given its representation in Borze alphabet.

    输入格式

    The first line contains a number in Borze code. The length of the string is between 1 and 200 characters. It’s guaranteed that the given string is a valid Borze code of some ternary number (this number can have leading zeroes).

    输出格式

    Output the decoded ternary number. It can have leading zeroes.

    样例 #1

    样例输入 #1

    .-.--
    
    • 1

    样例输出 #1

    012
    
    • 1

    样例 #2

    样例输入 #2

    --.
    
    • 1

    样例输出 #2

    20
    
    • 1

    样例 #3

    样例输入 #3

    -..-.--
    
    • 1

    样例输出 #3

    1012
    
    • 1

    代码如下

    #include
    #include
    #include
    #include 
    
    
    
    char s[1000];
    int main()
    {
        scanf("%s",&s);
        int len=strlen(s);//测算长度
        for(int i=0;i<len;++i)//循环判断
        {
            if(s[i]=='.')//“.”是0,此时不用重置(想一想,为什么)
                printf("0");
            if(s[i]=='-')//一条横分类讨论
            {
                if(s[i+1]=='.')//“-.”为1
                    printf("1");
                if(s[i+1]=='-')//“--”为2
                    printf("2");
                s[i+1]='?';//快乐重置
            }
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
  • 相关阅读:
    Linux下Qt启、停“.sh”脚本文件及获取终端打印信息
    【GAMES-104现代游戏引擎】4、引擎渲染基础(渲染基础数据、全局光照、PBR、阴影)
    js 回到顶部逻辑实现和elementUI源码解析
    【Java成王之路】EE初阶第十一篇:(网络编程) 5
    【C++ Exceptions】异常处理的成本
    解决EnableKeyword(“_Emission“)运行状态不起作用
    【机器学习】Samba-CoE实现高效推理部署
    易周金融分析 | 互联网系小贷平台密集增资;上半年银行理财子公司综合评价指数发布
    荧光标记氨基酸:荧光标记L-苯丙氨酸乙酯盐酸盐,L-phenylalanine ethylester labeled
    [excel与dict] python 读取excel内容并放入字典、将字典内容写入 excel文件
  • 原文地址:https://blog.csdn.net/weixin_62529383/article/details/126341781