CodeForces刷题C语言:BerOS file system、Second Order Statistics、IQ test、Phone numbers、Almost Prime

63 篇文章 7 订阅
7 篇文章 0 订阅

记录CodeForcess刷题C语言QAQ


一、BerOS file system

题面翻译

题目描述

新的操作系统BerOS有一个很好的特性。可以在路径中使用任意数量的字符“/”作为分隔符,而不是使用一个传统的“/”。例如,字符串//usr///local//nginx/sbin///usr/local/nginx///sbin//是等效的。只有根目录的路径可以表示为单个字符“/”,才需要路径末尾的字符“/”(或此类字符的某些序列)。
如果路径包含尽可能少的字符数“/”,则称为normalized的路径。

您的任务是将给定路径转换为规范化形式。

输入格式

一行,仅包含小写字母和/的字符串,保证路径至少含有一个/,不超过 100 100 100 个字符。

输出格式

一行,一个字符串,表示简化后的路径。

题目描述

The new operating system BerOS has a nice feature. It is possible to use any number of characters ‘/’ as a delimiter in path instead of one traditional ‘/’. For example, strings //usr///local//nginx/sbin// and /usr/local/nginx///sbin are equivalent. The character ‘/’ (or some sequence of such characters) at the end of the path is required only in case of the path to the root directory, which can be represented as single character ‘/’.

A path called normalized if it contains the smallest possible number of characters ‘/’.

Your task is to transform a given path to the normalized form.

输入格式

The first line of the input contains only lowercase Latin letters and character ‘/’ — the path to some directory. All paths start with at least one character ‘/’. The length of the given line is no more than 100 characters, it is not empty.

输出格式

The path in normalized form.

样例 #1

样例输入 #1

//usr///local//nginx/sbin

样例输出 #1

/usr/local/nginx/sbin

代码如下

#include<string.h>
#include<stdio.h>
#include<math.h>
#include <stdlib.h>



int main()
{
	char num[10001];
	scanf("%s",&num);
	
	int len = strlen(num);
	int n = 0;
	
	while(num[len -1] == '/'&&len != 1)
		len--;
	for(int i = 0;i < len;i++)
	{
		if(num[i] != '/')
			printf("%c",num[i]);
		else
		{
			printf("/");
			while(num[i] == '/')
				i++;
			i--;
		}
	}
	return 0;
}

二、Second Order Statistics

题面翻译

题面描述

给定一个数组,输出其中第二小的整数(相等的整数只计算一次)。

输入格式

第一行,一个整数 n n n 1 ≤ n ≤ 100 1 \leq n \leq 100 1n100),表示数组长度。

第二行, n n n 个绝对值小于 100 100 100 的整数。

输出格式

一行。如果该数组存在第二小整数,则输出第二小整数。如果不存在,则输出NO

题目描述

Once Bob needed to find the second order statistics of a sequence of integer numbers. Lets choose each number from the sequence exactly once and sort them. The value on the second position is the second order statistics of the given sequence. In other words it is the smallest element strictly greater than the minimum. Help Bob solve this problem.

输入格式

The first input line contains integer $ n $ ( $ 1<=n<=100 $ ) — amount of numbers in the sequence. The second line contains $ n $ space-separated integer numbers — elements of the sequence. These numbers don’t exceed 100 in absolute value.

输出格式

If the given sequence has the second order statistics, output this order statistics, otherwise output NO.

样例 #1

样例输入 #1

4
1 2 2 -4

样例输出 #1

1

样例 #2

样例输入 #2

5
1 2 3 1 1

样例输出 #2

2

代码如下

#include<string.h>
#include<stdio.h>
#include<math.h>
#include <stdlib.h>




int main()
{
	int n;
	scanf("%d",&n);
	
	int num[n];
	for(int i = 0;i < n;i++)
	{
		scanf("%d",&num[i]);
	}
	
	for(int i = 0;i < n;i++)
	{
		for(int j = i+1;j < n;j++)
		{
			if(num[i] > num[j])
			{
				num[i] = num[i] + num[j];
				num[j] = num[i] - num[j];
				num[i] = num[i] - num[j];
			}
		}
	}
	
	int m = num[0];
	for(int i = 0;i < n;i++)
	{
		if(num[i] != m)
		{
			m = num[i];
			printf("%d\n",m);
			break;
		}
	}
	
	if(m == num[0])
	{
		printf("NO\n");
	}
	return 0;
}

三、IQ test

题面翻译

题目描述

B o b Bob Bob 准备参加 R P RP RP 测试。他需要从 n n n 个数字中找出一个与众不同或者不符合规律的数字。你需要帮助他检查答案。

输入格式

第一行, n n n 3 ≤ n ≤ 100 3\leq n \leq100 3n100);

第二行, n n n 个数字,以空格分开(保证有唯一解)。

输出格式

仅一行,与众不同或不符合规律的数字在原序列中是第几位(从 1 1 1 开始编号)。

题目描述

Bob is preparing to pass IQ test. The most frequent task in this test is to find out which one of the given $ n $ numbers differs from the others. Bob observed that one number usually differs from the others in evenness. Help Bob — to check his answers, he needs a program that among the given $ n $ numbers finds one that is different in evenness.

输入格式

The first line contains integer $ n $ ( $ 3<=n<=100 $ ) — amount of numbers in the task. The second line contains $ n $ space-separated natural numbers, not exceeding 100. It is guaranteed, that exactly one of these numbers differs from the others in evenness.

输出格式

Output index of number that differs from the others in evenness. Numbers are numbered from 1 in the input order.

样例 #1

样例输入 #1

5
2 4 7 8 10

样例输出 #1

3

样例 #2

样例输入 #2

4
1 2 1 1

样例输出 #2

2

代码如下

#include<string.h>
#include<stdio.h>
#include<math.h>
#include <stdlib.h>




int main()
{
	int n;
	scanf("%d",&n);
	
	int num[n+1];
	for(int i = 1;i <= n;i++)
	{
		scanf("%d",&num[i]);
	}
	
	int m = (num[1]%2 + num[2]%2 + num[3]%2)/2;
	
	for(int i = 1;i <= n;i++)
	{
		if(num[i]%2 != m)
		{
			printf("%d",i);
		}
	}
	return 0;
}

四、Phone numbers

题面翻译

题面描述

给一串长度为 n n n 的数字串,在两个或三个数字之间加-,使得这个数字串便于记忆。如果有多解,可随意输出其中一个。

数据范围与约定

2 ≤ n ≤ 100 2\leq n\leq 100 2n100

题目描述

Phone number in Berland is a sequence of $ n $ digits. Often, to make it easier to memorize the number, it is divided into groups of two or three digits. For example, the phone number 1198733 is easier to remember as 11-987-33. Your task is to find for a given phone number any of its divisions into groups of two or three digits.

输入格式

The first line contains integer $ n $ ( $ 2<=n<=100 $ ) — amount of digits in the phone number. The second line contains $ n $ digits — the phone number to divide into groups.

输出格式

Output any of divisions of the given phone number into groups of two or three digits. Separate groups by single character -. If the answer is not unique, output any.

样例 #1

样例输入 #1

6
549871

样例输出 #1

54-98-71

样例 #2

样例输入 #2

7
1198733

样例输出 #2

11-987-33

代码如下

#include<string.h>
#include<stdio.h>
#include<math.h>
#include <stdlib.h>

int n,m,tot;
int main(){
    scanf("%d",&n);getchar();
    if (n&1) m=(n-1)/2-1;
    else m=n/2-1;
    for (int i=1;i<=n;i++){
        printf("%c",getchar());
        if (m){tot++;if (tot==2) printf("-"),m--,tot=0;}
    }
    return 0;
}



五、Almost Prime

题面翻译

题目描述

一个数如果恰好有两个不同质因子,那它被叫做几乎是质数的数。例如:6,18,24就是几乎是质数的数,而4,8,9,42不是,请你找出 1 1 1 n n n 中几乎是质数的数有多少个。

输入格式

输入仅包含一个整数 n n n 1 ≤ n ≤ 3000 1\leq n\leq3000 1n3000)。

输出格式

输出 1 1 1 n n n 中几乎是质数的数的数量。

Translated by Khassar

题目描述

A number is called almost prime if it has exactly two distinct prime divisors. For example, numbers 6, 18, 24 are almost prime, while 4, 8, 9, 42 are not. Find the amount of almost prime numbers which are between 1 and $ n $ , inclusive.

输入格式

Input contains one integer number $ n $ ( $ 1<=n<=3000 $ ).

输出格式

Output the amount of almost prime numbers between 1 and $ n $ , inclusive.

样例 #1

样例输入 #1

10

样例输出 #1

2

样例 #2

样例输入 #2

21

样例输出 #2

8

代码如下


#include<string.h>
#include<stdio.h>
#include<math.h>
#include <stdlib.h>

int n, b[3001]={0}, s=0;
int main()
{
	b[1]=1;
	scanf("%d",&n);
	for(int i = 2;i <= n;i++)
	if(b[i]==0)
	{
		for(int j=2;j*i<=n;j++)
		b[i*j]++;
        
       
	}
	for(int i=1;i<=n;i++)
	if(b[i]==2) s++;
	printf("%d",s);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值