• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            newplan

            阿基米德在洗澡時發現浮力原理,高興得來不及穿上褲子,跑到街上大喊:Eureka(我找到了)。
            posts - 39, comments - 26, trackbacks - 0, articles - 4
              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理



            /*
             STL       map應用 
             
            * Greedy   部分背包問題 
             
            * newplan  開發時間:08.5.13  
            */
            /*--------INCLUDES----------*/ 
            #include 
            <cstdlib>
            #include 
            <iostream>
            #include 
            <map>
            #include 
            <fstream> 
            #include 
            <iomanip>
            /*--------INCLUDES----------*/ 

            /*---------MACROS-----------*/
            #define INPUTFILE  
            "bag.txt"
            /*---------MACROS-----------*/

            /*----------STD-------------*/
            using std::ifstream;
            using std::cout;
            using std::endl;
            using std::map;
            using std::greater;
            using std::ios;
            using std::setw;
            /*----------STD-------------*/

            /*-------GLOBAL VAL---------*/
            ifstream  Fin;
            int n;
            int W;
            int totalValue;
            /*-------GLOBAL VAL---------*/

            /*---------MAIN-------------*/
            int main(int argc, char *argv[])
            {  
             
                map
            <int,int,greater<int> > goods;
                
                Fin.open(INPUTFILE);
             
                
            int value;
                
                
            int weight;
                
                Fin
            >>W;
                
                Fin
            >>n;
                
                
            int i;
                
            for(i=0;i<n;i++)
                {
                   Fin
            >>value;
                   Fin
            >>weight;
                   goods[value]
            =weight;
                }

                
            for(map<int,int>::iterator it = goods.begin();it!=goods.end();it++)
                {
                 cout
            <<setiosflags(ios::left)<<"value:"<<setw(4)<<it->first
                 
            <<" weight:"<<setw(4)<<it->second<<endl;
                }
                
                
            for(map<int,int>::iterator it = goods.begin();it!=goods.end();it++)
                {
                  
            if(W-it->second>=0)
                  {
                     W
            -=it->second;
                     totalValue
            +=it->first*it->second;
                     cout
            <<"w="<<W<<" ";
                  }
                  
            else 
                  {
                     totalValue
            +=W*it->first;
                     cout
            <<"totalValue:"<<totalValue<<endl;
                     break;  
                  }
                  
                }
                
                system(
            "PAUSE");
                return EXIT_SUCCESS;
            }
            /*---------MAIN-------------*/
            BAG.TXT
            100   10
            3   43
            5   22
            6    4
            4   67
            2    3
            45  2
            4   2
            42  24
            41  4
            34  55

            posted @ 2008-05-13 17:17 山泉彎延 閱讀(428) | 評論 (0)編輯 收藏

            vc  :
              CMD:
              1. vcvars.bat    
              2. cl  /FAs  ***.cpp  注意要將VC98/bin下面的文件VCVARS.BAT帶到 所要編譯的CPP文件所在的文件夾之下


            borlandc:
              1.設置環境變量右擊我的電腦進入高級環境進行系統:path的設置:加入*\Borland\BCC55\BIN;
              2.在*\BIN文件夾下添加兩個文件bcc32.cfg 文件內容為:
              -I "*\Borland\BCC55\INCLUDES"
              -L "*\BORLAND\BCC55\LIBS"
              另外的一個文件名為:ilink32.cfg:
              -L "*\BORLAND\BCC55\LIBS"
               3.CMD:
               BCC32  -S *CPP/C

            gcc/g++:gcc for c file  and g++ for cpp 只要安裝了DEVCPP就可以使用g++/gcc
               gcc  -S *c
               g++   -S *cpp
                
                  

            posted @ 2008-05-11 16:22 山泉彎延 閱讀(533) | 評論 (0)編輯 收藏

            #include <functional>
            #include 
            <vector> 
            #include 
            <algorithm>
            #include 
            <iostream>


            using namespace std;

            class out_times_x 
            {
            private:
              
            int multiplier;
              
            public:
                     
              out_times_x(
            const int& k) : multiplier(k) { }
              
              void operator()(
            const int& x) { cout << x * multiplier << " " << endl; }
              
            };




            int main ()
            {
              
            int sequence[5= {1,2,3,4,5};  
              
              vector
            <int>  v(sequence+0, sequence+5);
              
              out_times_x f2(
            2);
              
              for_each(v.begin(),v.end(),f2);   
            // Apply function

              system(
            "pause");
              
              return 
            0;
            }

            posted @ 2008-05-11 11:47 山泉彎延 閱讀(4291) | 評論 (0)編輯 收藏

             win32:DEV C++ 格式:
              #include 
            <iostream>
              using namespace std;
              

                    
            int a=1;/*a,b 應當都是全局的*/
                    
            int b=10;

              
            int main(int argc,char **argv)
              {
                     cout
            <<sizeof(int)<<endl;
                     
                     
            /*AT&T 規范 Not Intel*/
                     __asm(
            "mov _b,%eax");//mov 的左邊是源,右邊是目標 
                     
                     __asm(
            "mov %eax,_a");

                     cout
            <<a<<endl;
                     
                     cout
            <<b<<endl;
                     
                     cout
            <<"a+b="<<a+b<<endl;
                     
                     getchar();
                     
                     return(a
            -b);
              }
            win32:VC6.0格式:
            #include 
            <iostream>
            using namespace std;
            int main(int argc,char **argv)
            {
                    
            /*int的長度和eax的長度一樣都是4個字節*/
                    cout
            <<sizeof(int)<<endl;
                    
            int a=100;
                    
            int b=10;
                    
            /*Intel 規范*/
                     __asm{
                      mov eax,a;
            //eax 是目標,a 是源
                      add eax,b;
                      mov a,eax;
                          }
                     cout
            <<a<<endl;
                     cout
            <<"a+b="<<a<<endl;
                     return(a
            -b);
            }

            posted @ 2008-05-10 17:54 山泉彎延 閱讀(531) | 評論 (0)編輯 收藏

            #include <iostream>
            #include 
            <queue>//有priority_queue
            using namespace std;  
            class cl
            {
                  
            public:
                         
            int i;
                         
            };
            bool operator
            >(const cl&a, const cl & b)
            {
                 return a.i 
            < b.i;    
            }
            bool operator
            <(const cl&a,const cl &b)
            {
                 return a.i
            >b.i;
            }
            int main()
            {
                
                
                priority_queue
            <cl,vector<cl>,greater<vector<cl>::value_type> > q;
                cl a;
                
            while(cin>>a.i)
                {
                    q.push(a);
                }
                
            while(!q.empty())
                {
                    cout
            <<q.top().i<<endl;
                    q.pop();
                }
                system(
            "PAUSE");
                return 
            1;
            }

            posted @ 2008-05-07 15:26 山泉彎延 閱讀(317) | 評論 (0)編輯 收藏

            /*
            Romberg Algorithm 
            開發者:newplan
            開發日期:
            08.05.07 
            */


            /*=======================================*/
            /*INCLUDES*/ 
            #include           
            <cstdlib>
            #include           
            <iostream>
            #include           
            <cmath>
            /*=======================================*/
            /*MACROS  USED IN THIS FILE*/
            #define            MAX       
            20   
            #define            PRECISION 
            0.000008
            /*=======================================*/
            /*DECLARE NAMES IN STL NAMESAPCE */
            using  std::cout;
            using  std::endl;

            /*=======================================*/
            /*CLASS FUNC (FUNCTION OBJECT): THE ORIGINAL FUNCTION WE WANT TO INTEGRAL*/
            class  func{
                   
            public:
                          func(
            double x=1.0):exp(x){}
                          
            double operator()(const double& dnum)const{return pow(dnum,exp);}
                   
            private:
                          
            double exp;
                   };
            /*=======================================*/
            /*CLASS ECHELONFUNC (FUNCTION OBJECT)梯形法的遞推公式*/
            class  echelonFunc{
                   
            public:
                          echelonFunc(
            double begining,double ending,func & myfunc);
                          
            double operator()();
                   
            private:
                           
            double    h;
                           
            int       n;
                           
            double    T;
                           func      myfunc;
                           
            double    begining;
                           
            double    ending ;
                 };
            /*=======================================*/
            echelonFunc::echelonFunc(
            double begining,double ending,func & myfunc)
                          {
                           this
            ->begining=begining;
                           this
            ->ending=ending;
                           this
            ->h=ending-begining;
                           this
            ->n=0;
                           this
            ->T=0;
                           this
            ->myfunc=myfunc;//FUCNTION 
                          }
            /*------------------------------*/
            /* INCREASE FUNCTION 遞推函數*/
            double echelonFunc::operator()()
                          {   
            if(this->n==0)
                              {
                               this
            ->T=h*0.5*(myfunc(this->begining)+myfunc(this->ending));
                               this
            ->n=1;
                               return this
            ->T;
                              }
                              
            double len=0.5*h;
                              
            double sum=0;
                              
            int k=0;
                              
            for(k=0;k<this->n;k++)
                                  {
                                   sum
            +=myfunc(len);
                                   
            len=len+h;
                                  }
                              this
            ->T=0.5*this->T+0.5*h*sum;
                              this
            ->h/=2;
                              this
            ->n*=2;
                              return this
            ->T;
                          }
            /*=======================================*/
            /*THE MAIN CLASS IN THIS PROGRAM*/ 
            class  Romberg{
                   
            public:
                          Romberg(
            double begining,double ending,double exp);
                          ~Romberg();
                   
            private:
                          void RombergCPU();
            /*THE MOST IMPORTANT FUNCTION IN THIS PROGRAM*/
                          echelonFunc 
            *echol; 
                          
            double T[MAX][MAX];/*STO THE ROMBERG TABLE*/       
                   };
            /*------------------------------*/
            Romberg::Romberg(
            double begining ,double ending ,double paraexp)
            {
                func     myfunc(paraexp);
                echol 
            =  new echelonFunc(begining,ending,myfunc);
                RombergCPU();
            }
            /*------------------------------*/
            Romberg::~Romberg()
            {
                 delete echol;
            }
            /*------------------------------*/
            void Romberg::RombergCPU()
            {   clock_t Start; 
            //TIME STARAT
                clock_t 
            End;   //TIME END
                
            double *p[MAX];//WE USE THIS POINTER ARRAY TO ACCELERATE ALGOTITHM
                
            double **q;
                
            int i,j;
                Start 
            = clock();//TIME START FROM HERE
                
            for(i = 0,q = p; q < p+MAX; q++,i++)
                    
            *q= &T[i][0];
                
            double a,b,pows; 
                cout
            <<"-----------------------Romberg Algorithm---------------------"<<endl;
                
            *p[0]=(*echol)();
                cout
            <<"  "<<*p[0]<<endl;
                p[
            0]++;
                
            do{
                   
            *p[0]=(*echol)();
                    cout
            <<"  "<<*p[0];
                    p[
            0]++;
                    
            for(i=1;;i++)
                              {
                                pows
            =pow(4.0,double(i));
                                a
            =pows/(pows-1);
                                b
            =1/(pows-1);
                                
            *p[i]=a*(*(p[i-1]-1))-b*(*(p[i-1]-2));//ROMBERG ALGORITHM
                                cout
            <<"  "<<*p[i];
                                
            if(p[i]==&T[i][0])
                                  {
                                  p[i]
            ++
                                  break;
                                  } 
                                 p[i]
            ++;
                               }
                               cout
            <<endl;//fabs(T[i][0]-T[i-1][0])
                   }
            while(fabs(T[i][0]-T[i-1][0])>PRECISION);
                
                
            End = clock();//TIME END HERE
                
                cout
            <<"-------------------------------------------------------------"<<
                endl
            <<"  TIME SPEND:"<<(double)(End-Start)/CLOCKS_PER_SEC<<endl; 

            /*=======================================*/
            /*MAIN FUNCTION*/
            int main(int argc, char *argv[])
            {  
               Romberg  romberg(
            0,1,1.5);//ROMBERG API :BEGIN(0END(1EXP(1.5)
               system(
            "PAUSE");
               return EXIT_SUCCESS;
            }

            posted @ 2008-05-07 15:05 山泉彎延 閱讀(1488) | 評論 (1)編輯 收藏

            /*
             *用來測試STL hash_map 
             *簡單例子2008.5.5
            */
            #include  
            <cstdlib>
            #include  
            <iostream>
            #include  
            <string>
            #include  
            <hash_map.h>/*因為hash_map暫不為CPP標準所以沒辦法寫為<hash_map>*/
            /*-------------------------------------------*/
            using  std::cout;
            using  std::endl;
            using  std::string;
            /*-------------------------------------------*/
            /*函數類
             *作為hash_map的hash函數 
             *string沒有默認的hash函數 
             
            */ 
            class str_hash{
                  
            public:
                   size_t 
            operator()(const string& str) const
                    {
                            unsigned 
            long __h = 0;
                            
            for (size_t i = 0 ; i < str.size() ; i ++)
                            __h 
            = 5*__h + str[i];
                            
            return size_t(__h);
                    }
            };
            /*-------------------------------------------*/
            /*函數類 
             *作為hash_map的比較函數 )
             *(查找的時候不同的key往往可能對用到相同的hash值
            */ 
            class str_compare
            {
                  
            public:
                         
            bool operator()(const string& str1,const string& str2)const
                         {
            return   str1==str2;}
            };
            /*-------------------------------------------*/
            int 
            main(
            int argc, char *argv[])
            {  
                hash_map
            <string,string,str_hash,str_compare>  myhash;
                
                myhash[
            "google"]="newplan";
               
                myhash[
            "baidu"]="zhaoziming";
               
                
            if(myhash.find("google")!=myhash.end())
                  cout
            <<myhash["google"]<<endl;
                
                system(
            "PAUSE");
                
                
            return EXIT_SUCCESS;
            }
            /*-------------------------------------------*/

            posted @ 2008-05-05 17:17 山泉彎延 閱讀(14544) | 評論 (6)編輯 收藏

            #include <stdio.h>
            #include <ctype.h>

            int lookahead;
            void error()
             {
                 printf("synatax error\n"); 
              exit(1);
             }
             void match(int t)
             {
                 if(lookahead==t)
                   lookahead=getchar();
              else
                 error();
              }
             
              void term()
              {
                   if(isdigit(lookahead))
                      {
                     putchar(lookahead);
                     match(lookahead);
                      }
                      else error();
               }
               void exptr()
               {
                 term();
                 while(1)
                 {
                 if(lookahead=='+')
                 {
                       match('+');term();putchar('+');
                      
                   }
                   else if(lookahead=='-')
                 {
                       match('-');term();putchar('-');
                      
                   }
                   else  break;
               }
                }
            int main(int argc, char *argv[])
            {
              lookahead=getchar();
              exptr();
              putchar('\n');
              system("PAUSE"); 
              return 0;
            }

            posted @ 2008-03-26 09:40 山泉彎延 閱讀(1182) | 評論 (1)編輯 收藏

            #include "apue.h"
            #include 
            "semaphore.h"
            #include 
            "pthread.h"

            #define N 
            5
            static 
            int necs;
            static sem_t  
            *forks;

            void 
            *
            takeFork(
            int i)
            {
            if(i==N-1)
                {
                 sem_wait(
            &forks[0]);
                 sem_wait(
            &forks[i]);
                }
               
            else
                 {
                 sem_wait(
            &forks[i]);
                 sem_wait(
            &forks[i+1]);
                 }
            }
            void 
            *
            putFork(
            int i)
            {
            if(i==N-1)
                {
                 sem_post(
            &forks[0]);
                 sem_post(
            &forks[i]);
                }
            else
                {
                sem_post(
            &forks[i]);
                sem_post(
            &forks[i+1]);
               }
            }
            void 
            thinking(
            int i,int necs)
            {
                printf(
            "pholosopher %d  is thinking\n",i);
                sleep(necs);

            }
            void
            eating(
            int i,int necs)
            {
                printf(
            "pholosopher %d  is eating\n",i);
                sleep(necs);
            }
            void 
            *
            philosopher(void 
            *n)
            {
                
            int i=(int )n;
                
            while(1)
                {
                 thinking(i,necs);
                 takeFork(i);
                 eating(i,necs);
                 putFork(i);
                }
            }
            //============================main function
            int
            main(
            int argc,char *argv[])
            {
               pthread_t tid;
              
               
            if(argc==1)
                      necs
            =2;
               
            else if(argc ==2)
                      necs
            =atoi(argv[1]);
               
            else return 1;
              
               forks
            =(sem_t*)malloc(N*sizeof(sem_t));
               
               
            int i;
               
            for(i=0;  i<N;  i++)
               sem_init(forks
            +i,0,1);

               
            int status;
               
            for(i=0;i<N;i++)
                {
                  status
            =pthread_create(&tid,NULL,philosopher,(void*)i);

                  
            if(status<0)
                  err_sys(
            "create error!");  
                } 
                pthread_join(tid ,
            NULL);
                return 
            0;  
            }

            posted @ 2008-01-04 00:14 山泉彎延 閱讀(345) | 評論 (0)編輯 收藏

            #include "apue.h"
            #include 
            "lock.h"
            #include 
            "lock.c"

            /*define some important variable*/
            /*=======================================*/
            pid_t  pid;

            static char 
            *forks[5]={
            "fork0""fork1""fork2" ,"fork3" ,"fork4"
            };

            static 
            int necs;

            #define N 
            5

            /*======================================*/
            void takeFork(
            int i)
            {
            if(i==N-1)
              {
                 lock(forks[
            0]);
                 lock(forks[i]);
              }
            else
            {
            lock(forks[i]);
            lock(forks[i
            +1]);
            }
            }


            void putFork(
            int i)
            {
               
            if(i==N-1)
                {
                unlock(forks[
            0]);
                unlock(forks[i]);
                }
              
            else
               {
               unlock(forks[i]);
               unlock(forks[i
            +1]);

               }
            }
            int
            thinking(
            int i,int necs){
            printf(
            "pholosopher %d is thinking\n",i);
            return sleep(necs);
            }
            int 
            eating(
            int i,int necs)
            {printf(
            "pholosopher %d is eating\n",i);
            return sleep(necs);
            }
            void philosopher(
            int i)
            {
            while(1)
            {
            thinking(i,necs);
            takeFork(i);
            eating(i,necs);
            putFork(i);

            }
            }
            int
             main(
            int argc ,char *argv[])
            {
             
            if(argc=1)
                  necs
            =2;
            else if(argc==2)
                 necs
            =atoi(argv[1]);
            else  return 0;
             
            int i;   
            for(i=0;i<N;i++)
            {
             pid
            =fork();
             
            if(pid ==0)
                 philosopher(i);
            }
            return 
            0;
            }

            posted @ 2008-01-04 00:13 山泉彎延 閱讀(281) | 評論 (0)編輯 收藏

            僅列出標題
            共4頁: 1 2 3 4 
            精品国际久久久久999波多野| 久久精品国产亚洲精品| 久久夜色精品国产网站| 少妇被又大又粗又爽毛片久久黑人 | 国产三级观看久久| 99久久精品费精品国产一区二区 | 色诱久久久久综合网ywww| 色偷偷88欧美精品久久久| 一本色道久久88加勒比—综合| 99久久婷婷国产综合亚洲| 怡红院日本一道日本久久| 久久久99精品成人片中文字幕| 亚洲国产香蕉人人爽成AV片久久| 狠狠人妻久久久久久综合蜜桃| 国产精品欧美久久久久天天影视 | 9191精品国产免费久久| 亚洲精品乱码久久久久66| 欧美成a人片免费看久久| 久久男人Av资源网站无码软件| 久久精品国产亚洲Aⅴ香蕉| 久久国产免费观看精品| 久久国产亚洲精品无码| 精品伊人久久大线蕉色首页| 久久精品视频一| 午夜精品久久久久9999高清| 久久久WWW成人免费毛片| 国产三级观看久久| 欧美与黑人午夜性猛交久久久| 超级碰久久免费公开视频| 久久久精品一区二区三区| 国产精品久久成人影院| 少妇久久久久久久久久| 久久久久久亚洲AV无码专区| 欧美亚洲国产精品久久久久| 亚洲国产精品久久久久网站| 国产精品九九久久精品女同亚洲欧美日韩综合区 | 国产精品九九九久久九九| 久久久久亚洲av无码专区 | 久久久无码精品亚洲日韩京东传媒| 国产精品日韩深夜福利久久| 久久伊人中文无码|