交作業來著ヽ( ゚▽゚)ノ

最後編輯:2016-11-11 建立:2016-11-04 歷史紀錄

 

嚼嚼快速排序

  • #include<iostream>
  • using namespace std;
  • int qq(int a[],int s,int f){
  • int l=s,r=f,t[f];
  • for(int i=s+1;i<=f;i++){
  • if(a[i]<a[s]){
  • t[l]=a[i];
  • l+=1;
  • }
  • else{
  • t[r]=a[i];
  • r-=1;
  • }
  • }
  • t[l]=a[s];
  • for(int i=s;i<=f;i++){
  • a[i]=t[i];
  • }
  • if(l-1-s>1)qq(a,s,l-1);
  • if(f-r>1)qq(a,l+1,f);
  • return 1;
  • }
  • int main(){
  • int n;
  • cout<<"要排幾個數?"<<endl;
  • cin>>n;
  • int c[n];
  • cout<<"哪些數?"<<endl;
  • for(int i=0;i<n;i++){
  • cin>>c[i];
  • }
  • qq(c,0,n-1);
  • for(int i=0;i<n;i++){
  • cout<<c[i]<<" ";
  • }
  • return 0;
  • }

二分搜尋

  • #include<iostream>
  • using namespace std;
  • 百千.IO
  • 嚼嚼void qq(int a[],int s,int f)
  • {
  • int l=s,r=f,t[f];
  • for(int i=s+1; i<=f; i++)
  • {
  • if(a[i]<a[s])
  • {
  • t[l]=a[i];
  • l+=1;
  • }
  • else
  • {
  • t[r]=a[i];
  • r-=1;
  • }
  • }
  • t[l]=a[s];
  • for(int i=s; i<=f; i++)
  • {
  • a[i]=t[i];
  • }
  • if(l-1-s>0)qq(a,s,l-1);
  • if(f-r>0)qq(a,l+1,f);
  • }
  • bool found(int m[],int b,int a,int y)
  • {
  • if(a<b) return false;
  • if(m[(a+b)/2]==y) return true;
  • if(m[(a+b)/2]>y) found(m,b,(a+b)/2-1,y);
  • else found(m,(a+b)/2+1,a,y);
  • }
  • int main()
  • {
  • int n,wyw;
  • cin>>n;
  • int c[n];
  • for(int i=0; i<n; i++)
  • {
  • cin>>c[i];
  • }
  • cout<<"歡迎來到商店,請問您想買些什麼?"<<endl<<"請輸入編號:";
  • cin>>wyw;
  • qq(c,0,n-1);
  • if(found(c,0,n,wyw))cout<<"系統公告:恭喜玩家交易完成,物品已收入背包.";
  • else cout<<"很抱歉,您要的東西已售空,去別家看看吧!";
  • return 0;

 

    百千.IO作業成績 100 分,不能再高了