暑假第一周D,E,F题

D - Valued Keys
You found a mysterious function f. The function takes two strings s1 and s2. These strings must consist only of lowercase English letters, and must be the same length.

The output of the function f is another string of the same length. The i-th character of the output is equal to the minimum of the i-th character of s1 and the i-th character of s2.

For example, f("ab", "ba") = "aa", and f("nzwzl", "zizez") = "niwel".

You found two strings x and y of the same length and consisting of only lowercase English letters. Find any string z such that f(x, z) = y, or print -1 if no such string z exists.
Input
The first line of input contains the string x.

The second line of input contains the string y.

Both x and y consist only of lowercase English letters, x and y have same length and this length is between 1 and 100.
Output
If there is no string z such that f(x, z) = y, print -1.

Otherwise, print a string z such that f(x, z) = y. If there are multiple possible answers, print any of them. The string z should be the same length as x and y and consist only of lowercase English letters.

Example
Input
ab
aa
Output
ba
Input
nzwzl
niwel
Output
xiyez
Input
ab
ba
Output

-1


题目大意:给定两个字符串x,y,找到一个字符串z,满足f(x,z)=y,如果z存在输出z,如果不存在,输出-1。也就是找到一个字符串z,通过对x和z中的字符串进行比较,取其中最小的元素构成新的字符串y,求Z是否存在。
题解:如果y字符串的每一个字符比在X字符串中对应的字符小,那么就可以直接输出y,如果出现一个y中的元素比x中对应元素大,那么z就不存在,输出-1.

代码如下:

#include<stdio.h>
#include<string.h>
int main()
{
	char s1[101],s2[101];
	int a = 0, s, i;
	gets(s1);
	gets(s2);
	s = strlen(s1);
	for(i = 0; i < s; i++)
	{
		if(s1[i] < s2[i])
		{
			a=1;
			break;
		}
	}
	if(a == 1)
	{
		printf("-1\n");
    }
    else
    	printf("%s\n",s2);
    return 0;
} 




E - Vicious Keyboard 
Tonio has a keyboard with only two letters, "V" and "K".

One day, he has typed out a string s with only these two letters. He really likes it when the string "VK" appears, so he wishes to change at most one letter in the string (or do no changes) to maximize the number of occurrences of that string. Compute the maximum number of times "VK" can appear as a substring (i. e. a letter "K" right after a letter "V") in the resulting string.
Input
The first line will contain a string s consisting only of uppercase English letters "V" and "K" with length not less than 1 and not greater than 100.
Output
Output a single integer, the maximum number of times "VK" can appear as a substring of the given string after changing at most one character.

Example
Input
VK
Output
1
Input
VV
Output
1
Input
V
Output
0

题目大意:输入一个字符串,在只改变其中一个字符的前提下,尽量使字符串中“VK”的数量最多。

题解:从字符串中的第一个字符开始转换成对应的字符,如果是“V”,则换成“K”,如果是“K”,则换成“V,”一个一个试。

#include<stdio.h>
#include<string.h>
int main() {
	char str[105];
	int sum = 0,t = 0;
	int i,j;
	int max;
	gets(str);
	for(i = 0 ; i < strlen(str) ; i++) {
		if(str[i] == 'V' && str[i + 1] == 'K')
			sum++;
	}
	for(i = 0; i < strlen(str); i++) {

		if(str[i] == 'V')
			str[i] = 'K';
		else
			str[i] = 'V';
		t = 0;
		for(j = 0 ; j < strlen(str) ; j++) {

			if(str[j] == 'V' && str[j + 1] == 'K')
				t++;
		}
		if(sum < t) {
			sum = t;
		}
		if(str[i] == 'V')
			str[i] = 'K';
		else 
			str[i] = 'V';
	}
	printf("%d\n",sum);
	return 0;
}



F - Carrot Cakes
In some game by Playrix it takes t minutes for an oven to bake k carrot cakes, all cakes are ready at the same
moment t minutes after they started baking. Arkady needs at least n cakes to complete a task, but he currently
don't have any. However, he has infinitely many ingredients and one oven. Moreover, Arkady can build one more
similar oven to make the process faster, it would take d minutes to build the oven. While the new oven is being
built,only old one can bake cakes, after the new oven is built, both ovens bake simultaneously. Arkady can't
build more than one oven.

Determine if it is reasonable to build the second oven, i.e. will it decrease the minimum time needed to get n
cakes or not. If the time needed with the second oven is the same as with one oven, then it is unreasonable.

Input
The only line contains four integers n, t, k, d (1 ≤ n, t, k, d ≤ 1 000) — the number of cakes needed, the time
needed for one oven to bake k cakes, the number of cakes baked at the same time, the time needed to build
the second oven.
Output
If it is reasonable to build the second oven, print "YES". Otherwise print "NO".

Example
Input
8 6 4 5
Output
YES
Input
8 6 4 6
Output
NO
Input
10 3 11 4
Output
NO
Input
4 2 1 4
Output
YES
   题目大意:现在有n个蛋糕,有1台机器,每运行一次所需的时间为t,运行一次可以烤k个蛋糕。问题是有没有必要在制作一台新机器用来提高效率。如果有,输出YES,如果没有,输出NO.

  题解:可以通过判断需要烤蛋糕的次数来判断,如果在修建一台新机器的时间后,剩余次数小于或者等于1,那就没有必要修建新的机器来提高效率。

#include<stdio.h>
int main()
{
	int n, t, k, d;
	int z, r, s;
	scanf("%d %d %d %d", &n, &t, &k, &d);
	
	z = n / k;
	if(n % k)
		z++;        // 烤完n个蛋糕所需要的总次数; 
		
	s = d / t;		//修建烤箱花费的时间里机器运转的次数;
	 
	if(z - s <= 1)		//如果剩余的次数小于等于一,也就是蛋糕数小于等于K, 
	{                       //就没必要修建新的机器; 
		printf("NO\n"); 
	} 
	else
		printf("YES\n");
	return 0;
}
 

随手附上JAVA代码:


import java.util.Scanner;

public class Main {

	private static Scanner in;

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		in = new Scanner(System.in);
		int n, t, k, d;
		n = in.nextInt();
		t = in.nextInt();
		k = in.nextInt();
		d = in.nextInt();
		int z, s;
		z = n / k;
		if(n%k != 0)
			z++;
		
		s = d / t;
		if(z - s <= 1)
			System.out.println("NO");
		else
			System.out.println("YES");
		
		
				
		
	}

}




                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值