hdu,hduacm

來源:互聯網
上載者:User

hdu,hduacm

題意:NBA比賽,雙方共N次進球(N<=100000),無論哪方,進一個球(得分只可能為1,2,3),就記錄一次(記兩隊分數差的絕對值),問最後兩隊的比分有多少種。

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=4972

——>>知道最後的比分差k,怎麼求得比分呢?設分數較低的一方的最後分數為x,則另一方的最後分數為x + k,設雙方最後總分和為S,則x  = (S - k) / 2,可得雙方的比分。。所以,只要知道最後雙方的總分和,就可以確定雙方比分。。於是,問題轉化成求最後雙方總分和有多少種。

知道了雙分總分和,可以求出x : x + k,還有x + k : x,因此,如果x != x +k 即 k ! = 0時,最後結果應乘2。。

狀態:dp[i]表示前 i 次進球後雙方總分和的種數

狀態轉移方程:dp[i] = dp[i - 1] + 1(當(diff[i] == 1 && diff[i - 1] == 2) || (diff[i] == 2 && diff[i - 1] == 1) 時)

邊界:dp[0] = 1, diff[0] = 0

註:上一次比分差為2,現要達比分差為1,則敗者進1分或者進3分,此時可產生2種總分和。。上一次比分差為1,當前比分差為2同理。。

提交G++使用abs(int)會CE(查了一下MinGW裡面的cmath,abs確實沒有提供 int 參數),C++不會(翻了下MSDN,提供的參數是有 int 的)。。一直對整數求絕對值用abs,用浮點數求絕對值用fabs的我,好囧。。

#include <cstdio>#include <cmath>using std::abs;int main(){    int T, N, diff, kase = 0;    scanf("%d", &T);    while (T--)    {        int dp = 1, last = 0;        bool ok = true;        scanf("%d", &N);        while (N--)        {            scanf("%d", &diff);            if (!ok) continue;            if (abs(diff - last) > 3 || (diff == last && diff != 1))            {                ok = false;            }            else            {                if ((diff == 2 && last == 1) || (diff == 1 && last == 2))                {                    dp++;                }                last = diff;            }        }        if (diff != 0)        {            dp <<= 1;        }        printf("Case #%d: ", ++kase);        ok ? printf("%d\n", dp) : puts("0");    }    return 0;}



hdu acm 2032

我發我的ac代碼給你看吧,肯定是輸出時多了什麼空格什麼的,好好看看吧,加油
#include<stdio.h>
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int a[31][31];
int i,j,k;
for(i=1;i<=30;i++)
a[i][1]=a[i][i]=1;
if(n==1)printf("1\n");
if(n>=2)printf("1\n1 1\n");
for(i=3;i<=n;i++)
{
printf("1 ");
for(j=2;j<i;j++)
{
a[i][j]=a[i-1][j]+a[i-1][j-1];
printf("%d ",a[i][j]);
}
printf("%d\n",a[i][i]);
}
printf("\n");
}
}
 
HDU 2000號題

看了代碼,就是輸入3個char字元,從小到大 輸出他。
char字元也是整型數字!
看下MSDN,中scanf的格式要與後面的參數類型和數目對應,否則結果未定義。
其格式和printf一樣的,請看MSDN
scanf("%c%c%c",&a,&b,&c) 3個%C對應&a,&b,&c沒問題的!
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.