筆試程序題專項----清華2016年912

#include<iostream>
using namespace std;
int a[8]= {
    1,1,2,1,1,1,2,1
};

main() {
    int n=8;
    int s=3;
    int curv,curn,needn,imax=0;
    bool isok=true;
    for(int i=0; i<n; i++)
    {
        if(i==0||a[i]!=curv)  //第一次或者數值變化的時候修正目標
        {

            curv=a[i];
            curn=1;
            if(a[i]!=0&&s%a[i]==0)
            {
                needn=s/a[i];
                isok=true;
            }
            else if(a[i]==0&&s==0)
            {
                isok=true;
            } else
            {
                isok=false; //如果這樣,永遠無法達到目標
            }
        }
        else if(a[i]==curv&&isok==false)
        {

        }
        else if(a[i]==curv&&isok==true)  //又一次累計了,看看累計得夠不夠
        {
            if(a[i]!=0)
            {
                curn++;
                if(curn==needn)
                {
                    imax=max(imax,needn);
                }
            }
            else if(a[i]==0&&s==0)
            {
                curn++;
                imax=max(imax,curn);
            }
        }
        cout<<isok<<" "<<curn<<" "<<needn<<endl;
    }
    cout<<imax<<endl;
}

 

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