首屆全國中醫藥院校大學生程序設計競賽

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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章