2021年ICPC国际大学生程序设计竞赛暨陕西省第九届大学生程序设计竞赛(正式赛)I.Rabbit

链接:登录—专业IT笔试面试备考平台_牛客网
来源:牛客网
 

题目描述

What a cute rabbit! Mr.Gree has a lot of cute rabbits in the farm, and every rabbit has a number a1,a2,a3...ana_1, a_2, a_3...a_na1​,a2​,a3​...an​. Different rabbits may have same numbers. Mr.Gree thinks The Cute Value of these rabbits is a1∗a2∗a3∗...∗ana_1 * a_2 * a_3 * ... * a_na1​∗a2​∗a3​∗...∗an​. One day, he wants to make The Cute Value changed, so he takes away exactly one rabbit and The Cute Value is changed.

Mr.Gree wants to know how many different numbers The Cute Value could be?

For example, Mr.Gree has three rabbits numbered [3,4,5], now The Cute Value is 3∗4∗5=603*4*5 = 603∗4∗5=60. If he takes away a rabbit numbered 3, The Cute Value will change to 4∗5=204*5=204∗5=20. And if he takes away a rabbit numbered 4, now The Cute Value will change to 3∗5=153*5=153∗5=15. And if he take away a rabbit numbered 5, now The Cute Value will change to 3∗4=123*4=123∗4=12. So The Cute Value could change to 12, 15 or 20 three numbers after one rabbit is took away.

输入描述:

The first line of input contains a single integer n(2≤n≤105)n(2\leq n\leq 10^5)n(2≤n≤105)

The second line of input contains nnn integer ai(−109≤ai≤109)a_i(-10^9 \leq a_i \leq 10^9)ai​(−109≤ai​≤109) means the number that the i−thi-{th}i−th rabbit has.

输出描述:

 

print one line contains an integer.

示例1

输入

复制3 3 4 5

3
3 4 5

输出

复制3

3

示例2

输入

复制4 1 1 2 2

4
1 1 2 2

输出

复制1

1

示例3

输入

复制4 -1 -1 1 1

4
-1 -1 1 1

输出

复制1

1

说明

In the second case, initially The Cute Value is 1∗1∗2∗2=41*1*2*2=41∗1∗2∗2=4, if Mr.Gree takes away the rabbit numbered 1, The Cute Value is still 4, not changed.But if Mr.Gree takes away the rabbit numbered 2, The Cute Value is 1∗1∗2=21*1*2=21∗1∗2=2, The Cute Value is changed to 2, so he can make The Cute Value be one different number.

In the third case, the only way to make The Cute Value change is take away the rabbit numbered -1. 

题目分析:

        我们可以知道,这个题目啊,就是让我们先在第一行输入一个整数n,然后呢,在第二行输入n个数据。然后呢,我们假设,这些数据的积是sum,即sum=a1*a2*...*an。然后呢,题目差不多问的是我们把sum中的一个因子拿掉,那么,这些数积会因此改变吗?

        此时呢,我们要分析会出现哪些情况了。这时候,就特别适合列数据来解释问题啦。

        第一种情况:假设sum=2*3*4*5*6,那么,我拿走sum中的任何一个因子,sum的值都会改变。那么此时我输出的值应该与因子的个数相等。

        第二种情况:假设sum=1*2*3*4,那么,此时如果我拿走的是1,那么sum的值是不会变的,那么此时我输出的数,应该是sum的因子的数目减1.

        第三种情况:因子中有一个0,假设sum=0*6*7*8,那么此时我只有拿走因子0,sum的值才会有所改变,所以此时应当输出0。

        第四种情况:因子中有超过两个0的存在,假设sum=0*3*6*6*0,那么无论我拿走那个因子,sum的值都不会改变了,此时应当输出0。

        第五种情况:因子中有重复的数字时,例如sum=2*4*6*4*6,那么我输出的数应当是sum的值减去重复的数字。

        当然我们输入的数据有可能包含上边的多种情况,所以接下来我们要一个个排除,然后就完成了!

代码实现:

#include<stdio.h>
void shell_sort(int *data, int length);//希尔排序
int main()
{
    int n;
    scanf("%d",&n);
    int flag=n;
    int a[100000];
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    shell_sort(a,n);
    int cnt=0;
    int z=0;
    for(int i=0;i<n;i++)
    {
        if(a[i]==0)
        {
            cnt++;  //算输入的数字中有几个0
        }
    }
    if(cnt==1)
    {
        printf("1");  //这种情况可直接得到结果时1
    }
    else if(cnt>=2)
    {
        printf("0");  //这种情况可直接得到结果是0
    }
    else{
        if(a[0]==1)
        {
            z=1; //看代码中是否有1
        }
        for(int i=1;i<n;i++)
        {
            if(a[i]==1) 
            {
                z=1; //看代码中是否有1
            }
            if(a[i-1]==a[i])
            {
                flag--;  //减去重复代码
            }
        }
        printf("%d",flag-z);  //得到结果
    }
    return 0;
}
void shell_sort(int *data, int length)
{
	int gap = 0;
	int i = 0, j = 0;
	int temp;
 
	for (gap = length / 2;gap >= 1; gap /= 2)
    {  //外层循环用来分组--步长
		for (i = gap;i < length;i ++)
        {
			temp = data[i];
			for (j = i-gap;j >= 0 && temp < data[j];j = j - gap)
            {
				data[j+gap] = data[j];
			}
			data[j+gap] = temp;
		}
	}
}

        最后身为小菜鸡的我终于把它解出来了,真好。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值