程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> cf#179(div.2)

cf#179(div.2)

編輯:C++入門知識

[cpp]   /*    A 題,方法:搜索 。。。  題意:要求(0,0)到(x,y)要拐幾個彎!!       */   #include<iostream>   #include<cstdio>   #include<cmath>   #include<algorithm>   #include<string>   using namespace std;   #define manx 10000         int sum,xx,yy;         void dfs(int x,int y){       if(x>0 && y>0) {           if(yy==y && xx<x && xx>=(-x)) { cout<<sum<<endl; return ; } ////尼瑪啊,這裡等號標錯了,WA了許久!!!            sum++;            dfs(-x,y);        }       else if(x>0 && y<0) {            if(xx==x && yy<=(-y+1) && yy>y){ cout<<sum<<endl; return ; }           sum++;            dfs(x,-y+1);        }       else if(x<0 && y<0) {            if(yy==y && xx<=(-x+1) && xx>x){ cout<<sum<<endl; return ; }           sum++;            dfs(-x+1,y);        }       else if(x<0 && y>0) {            if(xx==x && yy>=(-y) && yy<y){ cout<<sum<<endl; return ; }           sum++;            dfs(x,-y);        }       else if(x==0 && y==0){            if(yy==y && xx<=(x+1) && xx>x){ cout<<sum<<endl; return ; }           sum++;            dfs(x+1,y);        }       else if(x==1 && y==0) {           if(xx==x && yy<=(y+1) && yy>y) { cout<<sum<<endl; return ; }                    sum++;             dfs(x,y+1);        }   }         int main(){           while(cin>>xx>>yy){           if(xx==0 && yy==0) { cout<<"0"<<endl; continue; }           sum=0;           dfs(0,0);       }   }        [cpp]   /*    B 題,方法:隊列或者模擬。。。  題意:求連續子段和不超過 t 的最大長度。。。      */    #include<iostream>   #include<cstdio>   #include<algorithm>   #include<cmath>   using namespace std;   #define manx 100009      int x[manx];   long long s[manx];      int main(){       int n,t;       while(cin>>n>>t){           long long k=1,max=0,p;           long long sum=0;           for(int i=1;i<=n;i++){              scanf("%d",&x[i]);              sum += x[i];              while(sum>t){                  p=i-k;                  if(max<p) max=p;                  sum -= x[k];                  k++;              }           }               if(sum<=t) {               p=n-k+1;               if(max<p) max=p;           }           cout<<max<<endl;       }   }      /*    4 5   3 1 2 1    4 6   3 1 1 4      */    

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved