1. 程式人生 > >HYNU 第四次周賽題解

HYNU 第四次周賽題解

大自然的搬運工

大水題

題目描述

給你n個整數,再給出整數k,請你求出有幾個能被k整除?(所以整數都小於2^31-1)
輸入

多組輸入
每組第一行2個整數n,k(1<=n,k<=100)
第二行n個整數
輸出

每組一個整數,佔一行,代表能被整除的數的個數
樣例輸入

3 2
1 2 3
4 3
1 2 3 4
樣例輸出

1
1

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace
std; int main() { int n,k; while(scanf("%d%d",&n,&k)!=EOF) { int res=0; for(int i=0;i<n;i++){ int x; scanf("%d",&x); if(x%k==0) res++; } printf("%d\n",res); } return 0; }

B: Overs

英文題,英語太菜導致某些同學在題意理解上有問題,我們的鍋
樣例輸入

5
2 1 4 3 5
5
1 4 3 5 2
樣例輸出

2 1 4
1 5 3

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
int main()
{
    int n,k,rk[110];
    while(scanf("%d",&n)!=EOF)
    {
        int res=0;
        for(int i=1;i<=n;i++){
            scanf
("%d",&rk[i]); } for(int i=1;i<=3;i++){ for(int j=1;j<=n;j++){ if(rk[j]==i){ if(i==1) printf("%d",j); else printf(" %d",j); } } } printf("\n"); } return 0; }

問題 C: 矩形問題

題目描述

鮮花可以在一塊麵積大小固定的區域圍一個矩形柵欄,這樣他就可以把豬放進去養豬了,苦於資金不夠,他想在面積為S的矩形區域中,圍成柵欄的周長儘量小,且邊長是整數。這樣他就能夠儘量節省了。
輸入

多組例項輸入,每個例項一個整數S(1 <= S <= 1000)
輸出

最小周長
樣例輸入

24
樣例輸出

20

列舉S的因子即可

#include<cstdio>
#include<queue>
#include<iostream>
#include<vector>
#include<map>
#include<cstring>
#include<string>
#include<set>
#include<stack>
#include<cmath>
#include<algorithm>
#define cle(a) memset(a,0,sizeof(a))
#define inf(a) memset(a,ox3f,sizeof(a))
#define ll long long
#define Rep(i,a,n) for(int i=a;i<=n;i++)
using namespace std;
const int INF = ( 2e9 ) + 2;
//const int maxn =
int main()
{
    int s;
    while(~scanf("%d",&s))
    {
        int len=(int)sqrt(s+1);
        int ans=INF;
        for(int i=1; i<=len; i++)
        {
            if(s%i==0)
            {
                if(ans>2*(s/i+i))
                {
                    ans=2*(s/i+i);
                }
            }
        }
        printf("%d\n",ans);
    }

}

問題 D: TXN的新房

題目描述

TXN買了一套新房,她想要粉刷她臥室的一面牆,她現在有三種顏色的顏料,白,藍,黃。假設她的牆一開始為全白,問她經過一系列操作之後牆壁的顏色。
輸入

輸入有多組。

第一行為兩個正整數n,m,代表了牆的長度與運算元。(n<=100,m<=100)

接下來m行,每行包含兩個數字和一個字元,l,r,w/b/y。(1<=l<=r<=n),[l,r]代表了粉刷的區間,w,b,y分別代表了三種顏色白,藍,黃。(注意字元的輸入)
輸出

輸出包括一行

Case+組數:最後的牆壁
樣例輸入

5 2
1 2 b
2 3 y
樣例輸出

根據題意模擬
Case 1: byyww

#include<iostream>
#include<cstring>
#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
const int INF = (2e9) + 2;
int main()
{
    int n,m,cas=1;
    char w[110];
    while(~scanf("%d%d",&n,&m))
    {
        memset(w,'w',sizeof(w));
        while(m--)
        {
            int l,r;
            char c;
            scanf("%d%d",&l,&r);
            cin>>c;
            for(int i=l;i<=r;i++)
                w[i]=c;
        }
        printf("Case %d: ",cas++);
        for(int i=1;i<=n;i++)printf("%c",w[i]);
        puts("");
    }
}

問題 E: 回。。。迴文串

題目描述

給出一個字串,讓你求出有多少子串是迴文串,且字串長度為奇數。
迴文串為前後對稱的字串,例如:a,aa,aba;
子串為母串的一個連續子集,例如abc的字串:a,b,c,ab,bc,abc;
輸入

多組輸入,每組一個字串,佔一行。(所有字串長度不超過100)
輸出

每組一個整數,佔一行,代表迴文字子串個數
樣例輸入

abba
ababa
樣例輸出

4
9

暴力

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
using namespace std;
int main()
{
    int n,m,cas=0;
    char s[101];
    while(scanf("%s",s)!=EOF)
    {
        int len=strlen(s);
        int res=0;
        for(int i=0;i<len;i++)
        {
            for(int j=i;j<len;j++){
                if((j-i+1)%2==0) continue;
                bool flag=0;
                for(int a=i,b=j;a<b;a++,b--)
                {
                    if(s[a]!=s[b]){
                        flag=1;
                        break;
                    }
                }
                if(!flag) res++;
            }
        }
        printf("%d\n",res);
    }
    return 0;
}

問題 F: IP地址

題目描述

IP是英文Internet Protocol的縮寫,意思是“網路之間互連的協議”,也就是為計算機網路相互連線進行通訊而設計的協議。在因特網中,它是能使連線到網上的所有計算機網路實現相互通訊的一套規則,規定了計算機在因特網上進行通訊時應當遵守的規則。輸入IP地址被用來給Internet上的電腦一個編號。大家日常見到的情況是每臺聯網的PC上都需要有IP地址,才能正常通訊。我們可以把“個人電腦”比作“一臺電話”,那麼“IP地址”就相當於“電話號碼”,而Internet中的路由器,就相當於電信局的“程控式交換機”。IP地址是一個32位的二進位制數,通常被分割為4個“8位二進位制數”(也就是4個位元組)。IP地址通常用“點分十進位制”表示成(a.b.c.d)的形式,其中,a,b,c,d都是0~255之間的十進位制整數。例:點分十進IP地址(100.4.5.6),實際上是32位二進位制數(01100100.00000100.00000101.00000110)。—摘自百度百科

現在給你一些32位二進位制數,將這些數全部用點分十進位制表示

注意:如果要讀入字串,不要用gets(),用scanf() 或cin讀取!!!
輸入

輸入資料有多組,每個例項一個32位二進位制數
輸出

每個例項用“點分十進位制”表示這個數,輸出之
樣例輸入

11001000001111111010100100100110
10101110110110111010011111100100
樣例輸出

200.63.169.38
174.219.167.228
根據題意模擬

#include<iostream>
#include<cstring>
#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
const int INF = (2e9) + 2;
int main()
{
    char bits[33];
    int ans[5];
    while(~scanf("%s",bits))
    {
        int index=31;
        int cur=1,sum=0,cnt=0;
        while(index>=0)
        {
            if(bits[index]=='1')sum+=cur;
            cur<<=1;
            if(index%8==0)
            {
                ans[cnt++]=sum;
                cur=1;
                sum=0;
            }
            index--;
        }
        for(int i=cnt-1;i>=0;i--)
            if(i==0)printf("%d\n",ans[i]);
            else printf("%d.",ans[i]);

    }
}

問題 G: XY數

題目描述

HXY發明了一種數(那就叫XY數吧),XY數定義為:數的每一位只能為1或者0,例如1010,10,1,0,都是XY數。

而102,2,12,則不是。現在給你一個正整數N,問最少能分解成多少個XY數。並從大到小輸出這些數。
輸入

輸入多組資料,每組資料包含一個N。(0 < N<=10^6)
輸出

輸出包含兩行,第一行為最少分解的XY數的個數,第二行為從大到小排序過的被分解後的XY數。

最後一個數後不跟空格。
樣例輸入

9
32
樣例輸出

9
1 1 1 1 1 1 1 1 1
3
11 11 10

思維+模擬
可以發現數目最少就是:這個數上所有位中的最大值。對於其他小於最大值的位可以用0來填。只有最大值需要全部填1,具體操作看程式碼

#include<iostream>
#include<cstring>
#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
const int INF = (2e9) + 2;
int ans[7][10];
int bits[10];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        memset(bits,0,sizeof(bits));
        memset(ans,0,sizeof(ans));
        int col=0,row=0;
        int temp=n;
        while(temp)
        {
            col=max(col,temp%10);
            bits[++row] = temp%10;
            temp/=10;
        }
        printf("%d\n",col);
        int fir=1;
        while(n)
        {
            int tmp = n,cnt=0,cur=1;
            int res=0;
            while(tmp)
            {
                if(tmp%10)
                    res+=cur;
                cur*=10;
                tmp/=10;
            }
            n-=res;
            if(!fir)printf(" ");
            printf("%d",res);
            fir=0;
        }
        puts("");
    }
}