CodeForces #300 B Quasi Binary

來源:互聯網
上載者:User

標籤:

大致題意:給定一個整數,比如32,32=11+11+10。。。給定一個整數n,它可以由0 和1 組成的整數的和組成,求最少需要幾個這樣0 和 1的整數。比如32至少需要3個。

分析:把輸入的這個整數當成字串處理。比如s=“32”  s[0]=‘ 3 ‘ ,s[1]=‘ 2 ‘;

那麼,我們做一個二維數組。a[][];  用來儲存這個字串s  

例如“ 32 ” 儲存為 

1 1

1 1

1 0 

例如3451 儲存為:

1 1 1 1

1 1 1 0

1 1 1 0

0 1 1 0

0 0 1 0

我們只需要把這個二維數組按行輸出並忽略前置0 即可。

代碼如下:

#include <cstdio>#include <cstring>#include <iostream>#define max(a,b) a>b?a:busing namespace std;int a[10][10];int main(){    string s;    cin>>s;    int len=s.size();    int i,j,k,cou=0;    for(i=0;i<len;i++)    {        cou=max(cou,s[i]-‘0‘);        for(j=0;j<s[i]-‘0‘;j++)            a[j][i]=1;    }//把輸入的n按位儲存入二維數組a[][];    //for(i=0;i<cou;i++)    //{    //    for(j=0;j<len;j++)    //        printf("%d ",a[i][j]);     //   printf("\n");   // }    printf("%d\n",cou);    for(i=0;i<cou;i++)    {        int flag=0;        for(j=0;j<len-1;j++)            if(a[i][j]==1||flag==1)//忽略前置0 把數組a內的元素按行輸出。            {                flag=1;                printf("%d",a[i][j]);            }            printf("%d",a[i][j]);        printf(" ");    }    return 0;}

很多優秀的處理辦法不一定拘束於數字,也可以把數字當做字串來處理,降低思考難度。

CodeForces #300 B Quasi Binary

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.