首届全国中医药院校大学生程序设计竞赛

1537: 序列的混乱程度

Description
有一个长度为n的正整数序列,一个序列的混乱程度定义为这个序列的最大值和最小值之差。请编写一个程序,计算一个序列的混乱程度。

Input
输入的第一行为一个正整数T (T<=1000),表示一共有T组测试数据。

每组测试数据的第一行为一个正整数n (1<=n<=1000),代表这个序列的长度。第二行为n个正整数,代表这个序列。序列中元素的大小不会超过1000。

Output
对于每个测试数据,输出一行包含一个整数,代表对应序列的混乱程度。

Sample Input

2
5
1 2 3 4 5
5
1 9 2 4 8

Sample Output

4
8

Code:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
typedef long long ll;

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d",&n);
        int maxx,minn;
        scanf("%d",&maxx);
        minn=maxx;
        for(int i=1; i<n; i++)
        {
            int a;
            scanf("%d",&a);
            if(maxx<a)
                maxx=a;
            if(minn>a)
                minn=a;
        }
        printf("%d\n",maxx-minn);
    }
    return 0;
}


1538: 随机数

Description
有一个rand(n)的函数,它的作用是产生一个在[0,n)的随机整数。现在有另外一个函数,它的代码如下:

int random(int n, int m)

{
return rand(n)+m;
}

显而易见的是函数random(n,m)可以产生任意范围的随机数。现在问题来了,如果我想要产生范围在[a,b)内的一个随机数,那么对应的n,m分别为多少?

Input
输入的第一行为一个正整数T (T<=1000),表示一共有T组测试数据。

对于每组测试数据包含两个整数a,b (a<=b)。

Output
对于每组测试数据,输出一行包含两个整数n和m,两个整数中间有一个空格分隔。

Sample Input

2
0 5
1 4

Sample Output

5 0
3 1

Code:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
typedef long long ll;

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int a,b;
        scanf("%d %d",&a,&b);
        printf("%d %d\n",b-a,a);
    }
    return 0;
}

1539: 完美序列
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 141 Solved: 46
[Submit][Status][Web Board]
Description
已知一个长度为l的序列:b1,b2,b3,…,bl (1<=b1<=b2<=b3<=…<=bl<=n)。若这个序列满足每个元素是它后续元素的因子,换句话说就是对于任意的i (2<=i<=l)都满足bi%bi-1=0 (其中“%”代表求余),则称这个序列是完美的。你的任务是对于给定的n和l,计算出一共有多少序列是完美序列。由于答案很大,所有输出答案对1000000007取余后的结果。

Input
输入的第一行为一个正整数T (T<=1000),代表一共有T组测试数据。

每组测试数据包含两个正整数n,l (1<=n, l<=2000),分别代表序列中元素大小的最大值和序列的长度。

Output
对于每组测试数据,输出一行包含一个整数,代表答案对1000000007取余后的结果。

Sample Input

3
3 2
6 4
2 1

Sample Output

5
39
2

HINT
先打个表
在这里插入图片描述
一点规律都没有= =。。然后拿3 2这组样例举例,
1 1,1 2, 2 2,1 3,3 3。
3 2(1 3,3 3)两组,其中的和为1+2+2=5
表2 通过init打出来,都满足bi%bi-1=0 以6 4为例,a[6][4]=a[5][4]+a[5][2]+a[5][1],即a[i][j]为a[i-1][w]的和,w为j的因子。然后打出a[2000][2000]的表就可以了
在这里插入图片描述

Code:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
typedef long long ll;
#define MOD 1000000007
ll f[2005][2005]={0};
void init()
{
    for(int i=1;i<=2000;i++)f[1][i]=1;
    for(int i=1;i<=2000;i++)
    {
        for(int j=1;j<=2000;j++)
        {
            for(int k=j;k<=2000;k+=j)//可以保证被前一个数整除
            {
                f[i+1][k]=(f[i+1][k]+f[i][j])%MOD;
            }
        }
    }
}
int main()
{
    int T;
    init();
    scanf("%d",&T);
    while(T--)
    {
        int n,m;
        scanf("%d %d",&n,&m);
        ll sum=0;
        for(int i=1;i<=n;i++)
            sum=(sum+f[m][i])%MOD;
        printf("%lld\n",sum);
    }
    return 0;
}


1540: 第k大数
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 739 Solved: 121
[Submit][Status][Web Board]
Description
有两个序列a,b,它们的长度分别为n和m,那么将两个序列中的元素对应相乘后得到的n*m个元素从大到小排列后的第k个元素是什么?

Input
输入的第一行为一个正整数T (T<=10),代表一共有T组测试数据。

每组测试数据的第一行有三个正整数n,m和k(1<=n, m<=100000,1<=k<=n*m),分别代表a序列的长度,b序列的长度,以及所求元素的下标。第二行为n个正整数代表序列a。第三行为m个正整数代表序列b。序列中所有元素的大小满足[1,100000]。

Output
对于每组测试数据,输出一行包含一个整数代表第k大的元素是多少。

Sample Input

3
3 2 3
1 2 3
1 2
2 2 1
1 1
1 1
2 2 4
1 1
1 1

Sample Output

3
1
1

HINT
设置两个数组a,b,然后从小到大分别排一下序,
所求的数一定在a[0]*b[0]—a[n-1]*b[n-1]之间,,设置一个mid等于两端除2,接下来寻找所有的乘出来的数中有多少比mid大的,得出mid是第几大元素,如果mid大于k,说明k在mid右边;否则k在mid左边,
然后在不断二分,直到mid=k为止,在找有多少比mid大的数的时候。
得到mid是第几大数,sum+=(m-j); 设a数组从后往前扫,b数组从前往后扫,如果a[i]*b[j]>=mid,则a[i-1]扫的时候直接从b数组的j开始扫,不用从0。拿1 2 3和12写个样例就懂了

Code:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
typedef long long ll;
ll a[100010],b[100010];
ll n,m;
ll jd(ll mid)
{
    ll sum=0;
    ll j=0;
    for(int i=n-1; i>=0; i--)
    {
        for(; j<=m-1; j++)
        {
            if(a[i]*b[j]>=mid)
            {
                sum+=(m-j);//统计比kmid大的个数
                break;
            }
        }
    }
    return sum;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int k;
        scanf("%lld %lld %d",&n,&m,&k);
        for(int i=0; i<n; i++)
            scanf("%d",&a[i]);
        for(int i=0; i<m; i++)
            scanf("%d",&b[i]);
        sort(a,a+n);
        sort(b,b+m);
        ll l,r,mid,ans;
        l=a[0]*b[0];
        r=a[n-1]*b[m-1];
        while(l<=r)
        {
            mid=(l+r)/2;
            int po=jd(mid);
            if(po>=k)
            {
                ans=mid;
                l=mid+1;
            }
            else
                r=mid-1;
        }
        printf("%lld\n",ans);
    }
    return 0;
}


1541: 选房子
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 503 Solved: 451
[Submit][Status][Web Board]
Description
栋栋和李剑已经大四了,想要出去找房子住。他们一共看中了n套房子。其中第i套房子已经住了ai个人了,它最多能住bi个人。栋栋和李剑想要住在一起,那么请问他们有几套可以选择的房子?

Input
输入的第一行为一个正整数T (T<=1000),代表一共有T组测试数据。

每组测试数据的第一行有一个正整数n (1<=n<=100),代表一共有n套房子。接下来n行,每行有两个正整数ai,bi (1<=ai<=bi<=100),分别代表现在已经住了ai个人和最多能住bi个人。

Output
对于每组测试数据,输出一行包含一个整数,代表他们可以选择房子的数量。

Sample Input

2
2
1 2
1 3
3
1 10
2 10
3 10

Sample Output

1
3

HINT
找到哪个房子还有两个空位就可以了
Code:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
typedef long long ll;
#define MOD 1000000007
int main()
{
    int T;
    scanf("%d",&T);
    int a[105],b[105],c[105];
    while(T--)
    {
        int n;
        int sum=0;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%d %d",&a[i],&b[i]);
            c[i]=b[i]-a[i];
            if(c[i]>=2)sum++;
        }
        printf("%d\n",sum);
    }
    return 0;
}


1543: Numbers

Description
DongDong is fond of numbers, and he has a positive integer P. Meanwhile, there is a rule that is:

A positive integer D that satisfies the following rules:

  1. D is one of the factors of P
  2. D and P have a same bit at least under the binary system.
    So DongDong wants to know how many positive integers D there are.

Input
The first line contains a positive integer T (T<=1000), which means the number of test cases. Then comes T lines, each line contains a positive integer P (1<=P<=1000000000).

Output
For each test case, print the number of positive integers D that satisfies the rules.

Sample Input

2
1
10

Sample Output

1
2

HINT
要求输出满足条件的个数,
1.必须是因数,
2.二进制中必须有一个相同
比如6的因数 1 2 3 6
其中2 3 6满足条件 输出3
if(check(d/i)&&(i!=sqrt(d)))//这个是检测因子的好办法了省时间
可以想一想 比如6中找到2为因数那么3一定是因数
Code:

#include<stdio.h>
#include<math.h>
int a[20];
int check(int n)
{
    int r;
    for(int i=0; n!=0; i++, n=n/2)
    {
        r = n%2;
        if(r==a[i])
            return 1;
    }
    return 0;
}
int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        long long p, d, sum=0;
        scanf("%lld", &p);
        d = p;
        for(int i=0; p!=0; i++)
        {
            a[i] = p%2;
            p = p/2;
        }
        for(int i=1; i<=sqrt(d); i++)
        {
            if(d%i==0)
            {
                if(check(i))
                    sum++;
                if(check(d/i)&&(i!=sqrt(d)))//这个是检测因子的好办法了省时间
                    sum++;
            }
        }
        printf("%lld\n", sum);
    }
    return 0;
}

1544: Counting Words
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 669 Solved: 374
[Submit][Status][Web Board]
Description
DongDong prefers English words to English sentences, so he wants to count the words of a sentence. Could you help him?

Input
The first line contains a positive integer T (T<=1000), which means T test cases. Then comes T lines, each line contains a string which combines with several words separated by spaces. Note that there may be more than one space to separate two words.

Output
For each test case, please print the number of words of the sentence.

Sample Input

3
 Every night in my dreams
   I see you  I feel you   
That  is  how I know you go on

Sample Output

5
6
8

HINT
判断当前这个是不是字母,下一个是不是空格即可。
然后再在外面判断最后一位是不是空格 如果不是那么输出+1.
Code:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
typedef long long ll;
#define MOD 1000000007
int main()
{
    int T;
    string a;
    scanf("%d",&T);
    getchar();
    while(T--)
    {

        getline(cin,a);
        //cout<<a<<endl;
        int l=a.size();
        int sum=0;
        for(int i=0;i<l-1;i++)
        {
            if(a[i+1]==' '&&((a[i]>='A'&&a[i]<='Z')||(a[i]>='a'&&a[i]<='z')))
            sum++;
            //printf("%d\n",sum);
        }
        if(a[l-1]!=' ')sum++;
        printf("%d\n",sum);
    }
    return 0;
}


  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值