洛谷刷题C语言:honoka的键盘、umi的函数、成绩、Modulo、Herman

记录洛谷刷题QAQ,一些不太优雅的代码


一、honoka的键盘

题目背景

honoka 有一个只有两个键的键盘。

题目描述

一天,她打出了一个只有这两个字符的字符串。当这个字符串里含有 VK 这个字符串的时候,honoka 就特别喜欢这个字符串。所以,她想改变至多一个字符(或者不做任何改变)来最大化这个字符串内 VK 出现的次数。给出原来的字符串,请计算她最多能使这个字符串内出现多少次 VK(只有当 VK 正好相邻时,我们认为出现了 VK。)

输入格式

第一行给出一个数字 n n n,代表字符串的长度。

第二行给出一个字符串 s s s

输出格式

第一行输出一个整数代表所求答案。

样例 #1

样例输入 #1

2
VK

样例输出 #1

1

样例 #2

样例输入 #2

2
VV

样例输出 #2

1

样例 #3

样例输入 #3

1
V

样例输出 #3

0

样例 #4

样例输入 #4

20
VKKKKKKKKKVVVVVVVVVK

样例输出 #4

3

样例 #5

样例输入 #5

4
KVKV

样例输出 #5

1

提示

对于 100 % 100\% 100% 的数据, 1 ≤ n ≤ 100 1\le n\le 100 1n100

代码如下

//先把字符串中所有VK找到,然后只要再找到一个VV或者KK就可以了
#include<string.h>
#include<stdio.h>
#include<math.h>
#include <stdlib.h>

int main()
{	
	int n;//字符串的长度
	scanf("%d",&n);
	
	char num[n];
	scanf("%s",&num);
	
	
	int sum = 0;
	int m = n;
	
	for(int i = 0;i < n-1;i++)
	{
		if(num[i] == 'V'&&num[i+1] == 'K')
		{
			sum++;
			num[i] = 'A';
			num[i+1] = 'A';
		}
		
	}
	
	for(int i = 0;i < n-1;i++)
	{
		if(num[i] == 'V'&&num[i+1] == 'V'||num[i] == 'X'&&num[i+1] == 'X')
		{
			sum++;
			break;
		}
	}
	
	printf("%d\n",sum);
	return 0; 
}

二、umi的函数

题目背景

umi 找到了一个神秘的函数 f。

题目描述

这个函数接受两个字符串 s1,s2。这些字符串只能由小写字母组成,并且具有相同的长度。这个函数的输出是另一个长度与 s1,s2 相同的字符串 g。g 的第 i 个字符等于 s1 的第i 个字符和 s2 的第 i 个字符的最小值,简单来说,g[i]=min(s1[i],s2[i])。

例如:f(“ab”,“ba”)= “aa”, f(“nzwzl”,“zizez”)=“niwel”.

她现在有两个相同长度的只有小写字母的字符串 x,y。找出任何一个满足 f(x,z)=y 的

字符串 z。如果找不到这样的字符串的话,请输出-1。

输入格式

第一行给出以下两个字符串的长度 n。

第二行给出一个字符串 x。

第三行给出一个字符串 y。

输出格式

第一行输出一个字符串,代表你找到的符合条件的字符串。找不到的话,请输出-1。

样例 #1

样例输入 #1

2
ab
aa

样例输出 #1

ba

样例 #2

样例输入 #2

5
nzwzl
niwel

样例输出 #2

xiyez

样例 #3

样例输入 #3

2
ab
ba

样例输出 #3

-1

提示

对于 100%的数据,1<=n<=100。

代码如下

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

int main()
{	
	long n;//字符串的长度 
	scanf("%lld",&n);
	
	char one[n+1], ans[n+1];
	scanf("%s",&one);
	scanf("%s",&ans);
	
	for(long i = 0;i <= n;i++)
	{
		if((int)one[i] < (int)ans[i])
		{
			printf("-1");
			return 0;	
		}	
	}	
	printf("%s\n",ans);
	return 0; 
}

三、[NOIP2017 普及组] 成绩

题目背景

NOIP2017 普及组 T1

题目描述

牛牛最近学习了 C++ 入门课程,这门课程的总成绩计算方法是:

总成绩=作业成绩$ \times 20%+ 小测成绩 小测成绩 小测成绩×30%+ 期末考试成绩 期末考试成绩 期末考试成绩 \times 50%$

牛牛想知道,这门课程自己最终能得到多少分。

输入格式

三个非负整数 A , B , C A,B,C A,B,C,分别表示牛牛的作业成绩、小测成绩和期末考试成绩。相邻两个数之间用一个空格隔开,三项成绩满分都是 100 100 100 分。

输出格式

一个整数,即牛牛这门课程的总成绩,满分也是 100 100 100 分。

样例 #1

样例输入 #1

100 100 80

样例输出 #1

90

样例 #2

样例输入 #2

60 90 80

样例输出 #2

79

提示

输入输出样例 1 说明

牛牛的作业成绩是 100 100 100 分,小测成绩是 100 100 100 分,期末考试成绩是 80 80 80 分,总成绩是 100 × 20 % + 100 × 30 % + 80 × 50 % = 20 + 30 + 40 = 90 100 \times 20\%+100 \times 30\%+80 \times 50\%=20+30+40=90 100×20%+100×30%+80×50%=20+30+40=90

输入输出样例 2 说明

牛牛的作业成绩是 60 60 60 分,小测成绩是 90 90 90 分,期末考试成绩是 80 80 80 分,总成绩是 60 × 20 % + 90 × 30 % + 80 × 50 % = 12 + 27 + 40 = 79 60 \times 20\%+90 \times 30\%+80 \times 50\%=12+27+40=79 60×20%+90×30%+80×50%=12+27+40=79

数据说明

对于 30 % 30\% 30% 的数据, A = B = 0 A=B=0 A=B=0

对于另外 30 % 30\% 30% 的数据, A = B = 100 A=B=100 A=B=100

对于 100 % 100\% 100% 的数据, 0 ≤ A , B , C ≤ 100 0≤A,B,C≤100 0A,B,C100 A , B , C A,B,C A,B,C 都是 10 10 10 的整数倍。

代码如下

//一道简单的题

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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	int A, B, C;
	while(scanf("%d%d%d",&A,&B,&C)!=EOF){
		int sum = A*0.2+B*0.3+C*0.5;
		printf("%d\n",sum); 
	} 
	return 0;
}

四、[COCI2006-2007#1] Modulo

题面翻译

描述

给出10个整数,问这些整数%42后有多少个不同的余数。
输入

输入包含10个小于1000的非负整数,每行一个。
输出

输出它们%42后,有多少个不同的余数。
说明

第一个样例的十个结果是1,2,3,4,5,6,7,8,9,10,有10个不同的结果;第二个样例结果都是0,只有一个不同的结果;第三个样例余数是39,40,41,0,1,2,40,41,0,1,有0,1,2,39,40,41这六个不同的结果。

感谢@ACdreamer 提供的翻译

注明:%42为除以42取余

题目描述

Given two integers A and B, A modulo B is the remainder when dividing A by B. For example, the numbers 7, 14, 27 and 38 become 1, 2, 0 and 2, modulo 3. Write a program that accepts 10 numbers as input and outputs the number of distinct numbers in the input, if the numbers are considered modulo 42.

输入格式

The input will contain 10 non-negative integers, each smaller than 1000, one per line.

输出格式

Output the number of distinct values when considered modulo 42 on a single line.

样例 #1

样例输入 #1

1
2
3
4
5
6
7
8
9
10

样例输出 #1

10

样例 #2

样例输入 #2

42
84
252
420
840
126
42
84
420
126

样例输出 #2

1

样例 #3

样例输入 #3

39
40
41
42
43
44
82
83
84
85

样例输出 #3

6

提示

In the first example, the numbers modulo 42 are 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10.
In the second example all numbers modulo 42 are 0.
In the third example, the numbers modulo 42 are 39, 40, 41, 0, 1, 2, 40, 41, 0 and 1. There are 6 distinct numbers.

代码如下

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

int main()
{	
	int num[10];
	int ans[10];
	for(int i = 0;i < 10;i++)
	{	
		scanf("%d",&num[i]);
		ans[i] = num[i] % 42;
	}
	int sum = 10;
	for(int i = 0;i < 10;i++)
	{
		for(int j = i+1;j < 10;j++)
		{
			if(ans[i] == ans[j])
			{
				sum--;
				break;
			}
		}
	} 
	
	
	printf("%d\n",sum);
	return 0; 
}

五、[COCI2006-2007#1] Herman

题面翻译

19世纪的德国数学家赫尔曼·闵可夫斯基(Hermann Minkowski)研究了一种名为出租车几何学的非欧几何。
在出租车几何里 T 1 ( x 1 , y 1 ) T_1(x_1,y_1) T1(x1,y1) T 2 ( x 2 , y 2 ) T_2(x_2,y_2) T2(x2,y2)两点之间的距离被定义为 d i s ( T 1 , T 2 ) = ∣ x 1 − x 2 ∣ + ∣ y 1 − y 2 ∣ dis(T_1,T_2)=|x_1-x_2|+|y_1-y_2| dis(T1,T2)=x1x2+y1y2(曼哈顿距离)。
其他定义均与欧几里得几何相同。
例如圆的定义:在同一平面内,到定点(圆心)的距离等于定长(半径)的点的集合。

我们对欧几里得几何与出租车几何两种定义下半径为 R R R的圆的面积很感兴趣。解决这个问题的重担就落在你身上了。

输入输出格式

输入格式

仅有一行为圆的半径 R R R ( R ≤ 10000 ) (R \leq 10000) (R10000)

输出格式

第一行输出欧几里得几何下半径为 R R R的圆的面积,第二行输出出租车几何下半径为 R R R的圆的面积。

注意:你的输出与标准答案绝对误差不超过 0.0001 0.0001 0.0001将会被认为正确

题目描述

The 19th century German mathematician Hermann Minkowski investigated a non-Euclidian geometry, called the taxicab geometry. In taxicab geometry the distance between two points T1(x1, y1) and T2(x2, y2) is defined as:
D(T1,T2) = |x1 - x2| + |y1 - y2|
All other definitions are the same as in Euclidian geometry, including that of a circle:
A circle is the set of all points in a plane at a fixed distance (the radius) from a fixed point (the centre of the circle).
We are interested in the difference of the areas of two circles with radius R, one of which is in normal (Euclidian) geometry, and the other in taxicab geometry. The burden of solving this difficult problem has fallen onto you.

输入格式

The first and only line of input will contain the radius R, an integer smaller than or equal to 10000.

输出格式

On the first line you should output the area of a circle with radius R in normal (Euclidian) geometry.
On the second line you should output the area of a circle with radius R in taxicab geometry.
Note: Outputs within ±0.0001 of the official solution will be accepted.

样例 #1

样例输入 #1

1

样例输出 #1

3.141593
2.000000

样例 #2

样例输入 #2

21

样例输出 #2

1385.442360
882.000000

样例 #3

样例输入 #3

42

样例输出 #3

5541.769441
3528.000000

代码如下


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

int main()
{	
	int r;
	scanf("%d",&r);
	//报错的时候我就知道精度有问题,但是这么多小数,我还是要谢

	double circle1 = r*r*3.1415926535897932384626433832;
	double circle2 = r*r*2.0;
	
	printf("%.6lf\n",circle1);
	printf("%.6lf\n",circle2); 
	return 0; 
};
	
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值