Summer day 1

今天上午沒什麼事,佈置了三道輸入輸出水題,一個月的複習之後成功對字符串讀入陌生。
第三題老是TLE,暴力之後圖簡單想減枝過,然而GG,後推導公式一層循環過了。如下。
HDU-2058

Problem Description
Given a sequence 1,2,3,……N, your job is to calculate all the possible sub-sequences that the sum of the sub-sequence is M.

Input
Input contains multiple test cases. each case contains two integers N, M( 1 <= N, M <= 1000000000).input ends with N = M = 0.

Output
For each test case, print all the possible sub-sequence that its sum is M.The format is show in the sample below.print a blank line after each test case.

Sample Input
20 10
50 30
0 0

Sample Output
[1,4]
[10,10]

[4,8]
[6,9]
[9,11]
[30,30]

暴力不過就推公式,另所求區間爲[ i, i+len ],則可用m和len來推出i,判斷i是否爲整。
滿足輸出,len的枚舉從大開始。

#include<cstdio>
#include<cmath>

int main()
{
    int n, m;
    while(scanf("%d%d",&n, &m)!=EOF)
    {
        if(n+m == 0)
            break;
        int len = sqrt(2*m)+5;
        if(len>n+1)
            len = n+1;
        for(; len>=0; len--)
        {
            double temp =double(m)/(len+1)-double(len)/2;
            if(temp>0 && temp+len<=n && temp == floor(temp))
            {
                int ans = floor(temp);
                printf("[%d,%d]\n",ans, ans+len);
            }
        }
        printf("\n");
    }
    return 0;
}

接下來定時賽,兩道較爲複雜的模擬,黑白棋和象棋(Uva220,Uva201)均沒寫出,其中黑白棋源碼L和M有問題,代碼冗長不貼了。

接下來練習一些水題。

Uva1399
有關字母表映射,題目提取出來後知,只需要重複次數相同的字母數目也相同即可,也即分別用cnt1,cnt2 記錄string的各字母出現次數,將cnt數組排序比較即可。如下。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int main()
{
    char a[105], b[105];
    while(scanf("%s%s",a,b)!=EOF)
    {
        int cnt1[26], cnt2[26];
        memset(cnt1,0,sizeof(cnt1));
        memset(cnt2,0,sizeof(cnt2));
        int len = strlen(a);      
        for(char i = 'A'; i<='Z'; i++)
        {
            for(int j = 0; j<len; j++)
            {
                if(i == a[j])
                    cnt1[i-'A']++;
            }
        }

        for(char i = 'A'; i<='Z'; i++)
        {
            for(int j = 0; j<len; j++)
            {
                if(i == b[j])
                    cnt2[i-'A']++;
            }
        }
        sort(cnt1, cnt1+26);
        sort(cnt2, cnt2+26);

        int flag = 1;
        for(int i = 0; i<26; i++)
        {
            if(cnt1[i] == cnt2[i])
                continue;
            else
            {
                flag = 0;
                break;
            }
        }
        if(flag)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

以及吊死鬼題目

Uva489
思路:用table記錄答案各字母出現次數,將猜的內容進行比對即可,此題有坑:重複猜不算錯,即猜過a之後再猜a不算加一筆。如下。並不需要那些複雜的狀態記錄。

#include<cstdio>
#include<cstring>

int main()
{
    int table[27], rnd;
    char a[26], b[26];
    while(scanf("%d",&rnd) == 1 && rnd != -1)
    {
        scanf("%s%s",a,b);
        memset(table, 0, sizeof(table));
        int len = strlen(a);
        for(int i = 0; i<len; i++)
        {
            table[a[i]-'a']++;
        }
        int cnt = 0;
        for(int i = 0; i<strlen(b); i++)
        {
            if(table[b[i]-'a'] == 0)
            {
                cnt++;
                table[b[i]-'a'] = -1;
            }
            if(table[b[i]-'a'] > 0)
            {
                len -= table[b[i]-'a'];
                table[b[i]-'a'] = -1;
            }
            if(cnt >= 7)
            {
                printf("Round %d\nYou lose.\n",rnd);
                goto bkpt;
            }
            if(len == 0)
            {
                printf("Round %d\nYou win.\n",rnd);
                goto bkpt;
            }
        }      
            printf("Round %d\nYou chickened out.\n",rnd);
        bkpt:;
    }
    return 0;
}

今天並不順,因爲有些生疏了,幾天的通宵之後智商也有所衰減。
PS 寫了黑白棋200+line,發現智商真的捉急。
以上。

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