第三周补题+总结

第三周补题加总结

A: Permutation

题目大意描述:

主要是涉及到了求模的过程,pi≡0mod(|pi-pi-2|)等价于是pi%|pi-pi-2|==0。

基本思路如下:

既然要满足这个条件,我们就从简单的做起,假如说绝对值里的数为1,那么无论pi是多少都是满足条件的,所以我们只需要保证|pi-pi-2|==1。
可以将所有的数从小到大从中间分开,然后在分别填到数组的奇数和偶数位置上,如下例所示:
题中共有t个用例。

输入:

1
6

输出:

1 4 2 5 3 6

代码如下:

#include <iostream>
#include <bits/stdc++.h>

using namespace std;
typedef unsigned long long ll;

int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        if(n&1)
        {
            int k=n/2+1;
            int a=1,b=1;
            for(int i=1;i<=n;i++)
            {
                if(i&1)
                {
                    printf("%d ",a);
                    a++;
                }
                else
                {
                    printf("%d ",k+b);
                    b++;
                }
            }
            printf("\n");
        }
        else
        {
            int k=n/2;
            int a=1,b=1;
            for(int i=1;i<=n;i++)
            {
                if(i&1)
                {
                    printf("%d ",a);
                    a++;
                }
                else
                {
                    printf("%d ",k+b);
                    b++;
                }
            }
            printf("\n");
        }
    }
}

B: A Simple Stone Game

题目大意描述:

就是有n堆石头,要在不同石堆之间移动石头,让所有的石堆的石头数目都是某个数的倍数,求最小的移动次数。第一行是测试用例的数量。
下面这个链接上介绍的思路很明确,大家可以学习一下:
链接
可以参考这一片博客里的思路和代码,很详细。

输入:

2
5
1 2 3 4 5
2
5 7

输出:

2
1

D: Dogs and Cages

题目大意描述:

Jerry likes dogs. He has N dogs numbered 0,1,…,N−1. He also has N cages numbered 0,1,…,N−1. Everyday he takes all his dogs out and walks them outside. When he is back home, as dogs can’t recognize the numbers, each dog just randomly selects a cage and enters it. Each cage can hold only one dog.
One day, Jerry noticed that some dogs were in the cage with the same number of themselves while others were not. Jerry would like to know what’s the expected number of dogs that are NOT in the cage with the same number of themselves.

Input

The first line of the input gives the number of test cases, T. T test cases follow.Each test case contains only one number N, indicating the number of dogs and cages.1≤T≤105,1≤N≤105.

Output

For each test case, output one line containing “Case #x: y”, where x is the test case number (starting from 1) and y is the expected number of dogs that are NOT in the cage with the same number of itself. y will be considered correct if it is within an absolute or relative error of 10−6 of the correct answer.

Sample Input

2
1
2

Sample Output

Case #1: 0.0000000000
Case #2: 1.0000000000

基本思路如下:

这道题与其说是编程题,不如说是数学题,最主要的就是推公式。基本思路如下:
当1在1的位置上的时候,有 (n-1) ! 种情况
那么1不在它的位置上有 n! - (n-1) ! 种情况
一共有 n 个这样的数字,所以乘n
最后除以 n!,化简得到 n-1

代码如下:

#include <iostream>
#include <bits/stdc++.h>

using namespace std;
typedef unsigned long long ll;
int a[1001]={0};

int main()
{
    int t,n;
    scanf("%d",&t);
    for(int i=1;i<=t;i++)
    {
        scanf("%d",&n);
        printf("Case #%d: %d\n",i,n-1);
    }
}

E: Evil Forest

题目大意描述:

The Bear, king of the forest, loves painting very much. His masterpiece – “Back in time” is very famous around all over the forest. Recently he wants to hold a series painting competition to select those animals who are good at painting as well. So the Bear appoints his minister Tiger to help him prepare the painting competitions.
Tiger owns a stationery factory which produces sketchpad, so he wants to let all the competitions use the sketchpads produced by his factory privately. The Bear plans to hold N painting competitions, and informs Tiger of the number of competitors who are ready for each competition. One competitor needs only one sktechpad in one competition. For each competition, at least 10% extra sketchpads are required as backup just in case.
The Tiger assigns you, his loyal subordinate to calculate the minimum number of sketchpads produced
by his factory.

Input

The first line of the input gives the number of test cases, T. T test cases follow.
For each test case, the first line contains an integer N, where N is the number of competitions. The next line contains N integers a1,a2,…,aN representing the number of competitors in each competition.
1≤T≤100
1≤N≤1000
1≤ai≤100

Output

For each test case, output one line containing “Case #x: y”, where x is the test case number (starting from 1) and y is the minimum number of sketchpads that should be produced by Tiger’s factory.

Sample Input

1
6
13 11 11 11 13 11

Sample Output

Case #1: 82

基本思路如下:

对于每个用例来说,有n个元素,每个元素要求每个元比原来要大%10,那么就在给每个元素赋值的时候让这个元素加上原来的十分之一。如果原来的十分之一不是整数的话就向上取整。最后将各个元素相加求和就行了。

代码如下:

#include <iostream>
#include <bits/stdc++.h>

using namespace std;
typedef unsigned long long ll;
int a[1001]={0};

int main()
{
    int t,n;
    scanf("%d",&t);
    for(int i=1;i<=t;i++)
    {
        scanf("%d",&n);
        int sum=0;
        memset(a+1,0,sizeof(int)*n);
        for(int j=1;j<=n;j++)
        {
            scanf("%d",&a[j]);
            int b=a[j]%10;
            if(b==0)
            {
                a[j]+=(a[j]/10);
            }
            else
            {
                a[j]+=(a[j]/10+1);
            }
            sum+=a[j];
        }
        printf("Case #%d: %d\n",i,sum);
    }
}

G: Rich Game

题目大意描述:

One day, God Sheep would like to play badminton but he can’t find an opponent. So he request Mr. Panda to play with him. He said: “Each time I win one point, I’ll pay you X dollars. For each time I lose one point, you should give me Y dollars back.”
God Sheep is going to play K sets of badminton games. For each set, anyone who wins at least 11 points and leads by at least 2 points will win this set. In other words, normally anyone who wins 11 points first will win the set. In case of deuce (E.g. 10 points to 10 points), it’s necessary to lead by 2 points to win the set.
Mr. Panda is really good at badminton so he could make each point win or lose at his will. But he has no money at the beginning. He need to earn money by losing points and using the money to win points.
Mr. Panda cannot owe God Sheep money as god never borrowed money. What’s the maximal number of sets can Mr. Panda win if he plays cleverly?

Input

The first line of the input gives the number of test cases, T. T test cases follow.
Each test case contains 3 numbers in one line, X, Y , K, the number of dollars earned by losing a point, the number of dollars paid by winning a point, the number of sets God Sheep is going to play.
1≤T≤105.
1≤X,Y,K≤1000.

Output

For each test case, output one line containing “Case #x: y”, where x is the test case number (starting from 1) and y is the maximal number of sets Mr. Panda could win.

Sample Input

2
10 10 1
10 10 2

Sample Output

Case #1: 0
Case #2: 1

基本思路如下:

这道题也没什么思路,还是要先推出相应的关系公式。
在推关系的过程中我们可以分为三种情况,就是根据x,y之间的关系。
当x>y时:这种情况下,直接就可以赢k局;
当x<=y时:这种情况下就要推公式了,就是要保证输的时候比分为0:11,而赢的时候比分为11:9。

代码如下:

#include <iostream>
#include <bits/stdc++.h>

using namespace std;
typedef unsigned long long ll;

int main()
{
    int t,x,y,k;
    scanf("%d",&t);
    for(int i=1;i<=t;i++)
    {
        scanf("%d %d %d", &x, &y, &k);
        printf("Case #%d: %d\n",i,x > y ? k : 11*k*x/(11*y+2*x));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值