新生训练赛002题解

J - 新年快乐

Frog Wa has many frogs, so he can hear GuaGuaGua every day. But one day a flappy bird ate his frogs, so Frog Wa decided to build a fence to protect his precious frogs. Frog Wa told you four points (x, 0), (x, y), (z, 0), (z, t) which represent for the vertex of the fence. he want to know the area of the fence, can you tell him?
Input
The input consist of four integers x, y, z and t. 0 <= x, y, z, t <= 10000, x < z and y < t.
Output
For each test case, print the area, numbers should accurate to one decimal places.
Sample Input
1 1 2 2
0 2 6 6
0 0 2 10
Sample Output
1.5
24.0
10.0
理解:

就是个简单的题…把题意读懂就可了,四个点在坐标轴上围成一个梯形,算梯形的面积。

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
	
	double x,y,z,t,k;
	while(cin>>x>>y>>z>>t)
	{
		k=(y+t)*(z-x)/2.0;
		printf("%.1lf\n",k);
	}
	return 0;
 } 

G - 0011

Alex likes to play with one and zero!One day he gets an empty string.So our cute boy wants to add one and zero in it. Every time he will add ‘01’in the string at any position and then get a new string.For example:if the string is “01” now ,he can get “0101” or “0011,Now give you a string that is Alex has get,you need to answer whether the string is legal?
Input
First is a integer n(n<=100)
Next contains n lines .Every line is a string whose legth is no more than 1000.
Output
For each case output “YES” in a single line if it’s legal.
Or you need to output “NO”;
Sample Input
3
0101
0110
0011
Sample Output
YES
NO
YES
理解:

题目意思是在字符串随意的位置插入’01’。样例输入的字符串看是否符合题目要求的插入规则。所以将字符串的第一个字符入栈,如果和后面的字符符合‘01’形式就出栈,否则继续存入。最后看栈是否为空。

#include<iostream>
#include<cstdio>
#include<stack>
#include<cstring>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		stack<char> x;
		char a[1010];
		cin>>a;
		int n=strlen(a);
		for(int i=0;i<n;i++)
		{
			if(a[i]=='0')
				x.push(a[i]);
			else if(a[i]=='1')
			{
				if(x.empty()||x.top()=='1')
					x.push(a[i]);
				else if(x.top()=='0')
					x.pop();	
			}
		} 
		if(x.empty())
			cout<<"YES"<<endl;
		else if(!x.empty())
			cout<<"NO"<<endl;
	}
	return 0;
}

I - 十进制中的二进制

hkhv学长最近对二进制数很感兴趣,喜欢一切0和1组成的数。现在有一个十进制整数n,问你1到n之间有多少个数是只有0和1组成的类似二进制的数,输出他们的个数。

Input
输入数据包含一个数n (1 <= n <=10^9).
Output
输出1到n中类似二进制的数的个数.
Sample Input
10
Sample Output
2
Hint
对于n = 10,1 和 10是类似二进制的数.
理解:

规律,一位数的有一个,两位数的有两个,三位数的有四个…k位数就有2的k-1次方个。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
	char s[1000];
	while(scanf("%s",s)!=EOF)
	{
		int L=strlen(s),m=1;
		for(int t=1;t<L;t++)
			m*=2;
		int k=0,count=0;
		for(int i=0;i<L;i++)
		{
			if(s[i]>'1')
				k=1;
			if(s[i]>='1'||k==1)
				count+=m;
			m/=2;
		}
		cout<<count<<endl;
	}
	return 0;
}

H - Perfect String

A string is called beautiful if no two consecutive characters are equal. For example, “ababcb”, “a” and “abab” are beautiful strings, while “aaaaaa”, “abaa” and “bb” are not.Ahcl wants to construct a beautiful string. He has a string ss, consisting of only characters ‘a’, ‘b’, ‘c’ and ‘?’. Ahcl needs to replace each character ‘?’ with one of the three characters ‘a’, ‘b’ or ‘c’, such that the resulting string is beautiful. Please help him!More formally, after replacing all characters ‘?’, the condition si≠si+1si≠si+1 should be satisfied for all 1≤i≤|s|−11≤i≤|s|−1, where |s||s| is the length of the string ss.
Input
The first line contains positive integer tt (1≤t≤10001≤t≤1000) — the number of test cases. Next tt lines contain the descriptions of test cases.Each line contains a non-empty string ss consisting of only characters ‘a’, ‘b’, ‘c’ and ‘?’.It is guaranteed that in each test case a string ss has at least one character ‘?’. The sum of lengths of strings ss in all test cases does not exceed 105105.
Output
For each test case given in the input print the answer in the following format:
If it is impossible to create a beautiful string, print “-1” (without quotes);
Otherwise, print the resulting beautiful string after replacing all ‘?’ characters. If there are multiple answers, you can print any of them.
Example
Input
3
a???cb
a??bbc
a?b?c
Output
ababcb
-1
acbac
Note
In the first test case, all possible correct answers are “ababcb”, “abcacb”, “abcbcb”, “acabcb” and “acbacb”. The two answers “abcbab” and “abaabc” are incorrect, because you can replace only ‘?’ characters and the resulting string must be beautiful.
In the second test case, it is impossible to create a beautiful string, because the 44-th and 55-th characters will be always equal.
In the third test case, the only answer is “acbac”.
理解:

字符更改,如果字符串中的某字符为’?’,则将它换成和它前后不同的字符。规定字符只有’a’,‘b’,‘c’,’?’,看是否最后由两个相邻相同的字符。
分两部分考虑,当它长度为1的时候,如果为?就将它改为任意一个数,再者长度不为一时,第一个字符为‘?’时,将它改为和后面不一样的字符,然后再依次查看下一个字符,最后查看最后一个字符。然后看是否有相邻相同的字符。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
 int n;
 cin>>n;
 while(n--)
 {
  char a[100010];
  scanf("%s",a);
  int L=strlen(a);
  if(L==1)
  {
   if(a[0]=='?')
   a[0]='a';
  }
  else if(a[0]=='?')
  {
   if(a[1]=='?')
    a[0]='a';
   else if(a[1]=='a')
    a[0]='b';
   else if(a[1]=='b')
    a[0]='c';
   else if(a[1]=='c')
    a[0]='b';
  }
  for(int i=1;i<L-1;i++)
  {
   if(a[i]=='?')
   {
    if(a[i-1]==a[i+1])
    {
     if(a[i-1]=='a')
      a[i]='b';
     else if(a[i-1]=='b')
      a[i]='c';
     else if(a[i-1]=='c')
      a[i]='a';
    }
    else if(a[i-1]!=a[i+1])
    {
     if(a[i-1]=='a')
     {
      if(a[i+1]=='b')
       a[i]='c';
      else
       a[i]='b';
     }
     if(a[i-1]=='b')
     {
      if(a[i+1]=='a')
       a[i]='c';
      else 
       a[i]='a';
     }
     if(a[i-1]=='c')
     {
      if(a[i+1]=='a')
       a[i]='b';
      else 
       a[i]='a';
     }
    }
   }
  }
  if(a[L-1]=='?'&&L>1)
  {
   if(a[L-2]=='a')
    a[L-1]='b';
   else if(a[L-2]=='b')
    a[L-1]='c';
   else if(a[L-2]=='c')
    a[L-1]='a';
  }
  int flag=0;
  for(int i=1;i<L;i++)
  {
   if(a[i]==a[i-1])
   flag=1;
  }
  if(flag==1)
   printf("-1\n");
  else
   cout<<a<<endl;
 }
 return 0;
}

D - Eat Candies

You have three piles of candies: red, green and blue candies:the first pile contains only red candies and there are rr candies in it,the second pile contains only green candies and there are gg candies in it,the third pile contains only blue candies and there are bb candies in it.Each day Tanya eats exactly two candies of different colors. She is free to choose the colors of eaten candies: the only restriction that she can’t eat two candies of the same color in a day.Find the maximal number of days Tanya can eat candies? Each day she needs to eat exactly two candies.
Input
The first line contains integer tt (1≤t≤10001≤t≤1000) — the number of test cases in the input. Then tt test cases follow.Each test case is given as a separate line of the input. It contains three integers rr, gg and bb (1≤r,g,b≤1081≤r,g,b≤108) — the number of red, green and blue candies, respectively.
Output
Print tt integers: the ii-th printed integer is the answer on the ii-th test case in the input.
Example
Input
6
1 1 1
1 2 1
4 1 1
7 4 10
8 1 4
8 2 8
Output
1
2
2
10
5
9
Note
In the first example, Tanya can eat candies for one day only. She can eat any pair of candies this day because all of them have different colors.In the second example, Tanya can eat candies for two days. For example, she can eat red and green candies on the first day, and green and blue candies on the second day.In the third example, Tanya can eat candies for two days. For example, she can eat red and green candies on the first day, and red and blue candies on the second day. Note, that two red candies will remain uneaten.

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
 int t;
 cin>>t;
 while(t--)
 {
  int a[4],count;
  for(int i=0;i<3;i++)
   cin>>a[i];
  sort(a,a+3);
  int t=a[2]-a[1];
  if(t<a[0])
   count=t+a[1]+(a[0]-t)/2;
  else if(t>=a[0])
   count=a[0]+a[1];
  printf("%d\n",count);
 }
 return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值