新生训练赛002 补题

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插空构成
思路:
做的时候没想出来,其实只要在任何一个字符前面,满足1的个数不大于0就可
代码:

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

int main(void)
{
    int n;
    char c[1005];
    scanf("%d",&n);
    while(n--)
    {
        scanf("%s",c);
        int p=strlen(c),x=0,y=0,sign=1;
        for(int i=0;i<p;i++)
        {
            if(c[i]=='0') x++;
            else y++;
            if(x<y) {sign=0;break;}
        }
        if(sign) printf("YES\n");
        else printf("NO\n");
    }
    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

题意:
求从1到n中,全部仅由1和0构成的数的个数

思路:
可以用排列组合的方法来思考。数的每一位有三种可能分别是0,1,[2,9]。
现在从最高位逐次看:

  1. “0”显然不会出现。
  2. “1”它已经包含了低一位所有的符合题意的数,例如1000包含了所有小于它的符合题意的数,其数量为2^(n-1)其中n是1的位次(从右往左,1000时n=4),继续看下一位。
  3. “[2,9]”它已经包含了本位所有的符合题意的数,数量为2^n-1因为不包含0000这种情况,并且不用继续,它就是最终答案。

再看除了最高位的情况:

  1. “0”跳过。
  2. “1”同上,把对应的数量加进去,continue。
  3. “[2,9]”同上,数量还是2^n-1,因为在上一位中?000这种情况已经算入了,同样遇“[2,9]”就break。

代码:

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

int main(void)
{
    int n,count,a[10],pos[10],i;
    double sum;
    while(scanf("%d",&n)!=EOF)
    {

        count=0,i=1,sum=0.0;
        while(n)
            a[i]=n%10,n/=10,count++,pos[i++]=count;

        while(--i)
            {
                if(a[i]>=2) {sum+=pow(2,pos[i])-1;break;}
                else if(a[i]==1) sum+=pow(2,pos[i]-1);
            }
        printf("%.0lf\n",sum);
    }

}

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

题意:
求矩形面积

思路:
(上底+下底)/高*2

代码:

#include<stdio.h>

int main(void)
{
    int x,y,z,t;
    double l,h,k,area;
    while(scanf("%d%d%d%d",&x,&y,&z,&t)!=EOF)
    	printf("%.1lf\n",(y+t)*(z-x)/2.0);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值