• <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>

            天之道

            享受編程的樂趣。
            posts - 118, comments - 7, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

             Map是c++的一個標準容器,她提供了很好一對一的關系,在一些程序中建立一個map可以起到事半功倍的效果,總結了一些map基本簡單實用的操作!
            1. map最基本的構造函數;
               map<string , int >mapstring;         map<int ,string >mapint;
               map<sring, char>mapstring;         map< char ,string>mapchar;
               map<char ,int>mapchar;            map<int ,char >mapint;

            2. map添加數據;

               map<int ,string> maplive;  
               1.maplive.insert(pair<int,string>(102,"aclive"));
               2.maplive.insert(map<int,string>::value_type(321,"hai"));
               3, maplive[112]="April";//map中最簡單最常用的插入添加!
            3,map中元素的查找:

               find()函數返回一個迭代器指向鍵值為key的元素,如果沒找到就返回指向map尾部的迭代器。        

               map<int ,string >::iterator l_it;; 
               l_it=maplive.find(112);
               if(l_it==maplive.end())
                            cout<<"we do not find 112"<<endl;
               else cout<<"wo find 112"<<endl;
            4,map中元素的刪除:
               如果刪除112;
               map<int ,string >::iterator l_it;;
               l_it=maplive.find(112);
               if(l_it==maplive.end())
                    cout<<"we do not find 112"<<endl;
               else  maplive.erase(l_it);  //delete 112;
            5,map中 swap的用法:
              Map中的swap不是一個容器中的元素交換,而是兩個容器交換;
              For example:
              #include <map>
              #include <iostream>

              using namespace std;

              int main( )
              {
                  map <int, int> m1, m2, m3;
                  map <int, int>::iterator m1_Iter;

                  m1.insert ( pair <int, int>  ( 1, 10 ) );
                  m1.insert ( pair <int, int>  ( 2, 20 ) );
                  m1.insert ( pair <int, int>  ( 3, 30 ) );
                  m2.insert ( pair <int, int>  ( 10, 100 ) );
                  m2.insert ( pair <int, int>  ( 20, 200 ) );
                  m3.insert ( pair <int, int>  ( 30, 300 ) );

               cout << "The original map m1 is:";
               for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
                  cout << " " << m1_Iter->second;
                  cout   << "." << endl;

               // This is the member function version of swap
               //m2 is said to be the argument map; m1 the target map
               m1.swap( m2 );

               cout << "After swapping with m2, map m1 is:";
               for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
                  cout << " " << m1_Iter -> second;
                  cout  << "." << endl;
               cout << "After swapping with m2, map m2 is:";
               for ( m1_Iter = m2.begin( ); m1_Iter != m2.end( ); m1_Iter++ )
                  cout << " " << m1_Iter -> second;
                  cout  << "." << endl;
               // This is the specialized template version of swap
               swap( m1, m3 );

               cout << "After swapping with m3, map m1 is:";
               for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
                  cout << " " << m1_Iter -> second;
                  cout   << "." << endl;
            }

            6.map的sort問題:
              Map中的元素是自動按key升序排序,所以不能對map用sort函數:
              For example:
              #include <map>
              #include <iostream>

              using namespace std;

             int main( )
             {
               map <int, int> m1;
               map <int, int>::iterator m1_Iter;

               m1.insert ( pair <int, int>  ( 1, 20 ) );
               m1.insert ( pair <int, int>  ( 4, 40 ) );
               m1.insert ( pair <int, int>  ( 3, 60 ) );
               m1.insert ( pair <int, int>  ( 2, 50 ) );
               m1.insert ( pair <int, int>  ( 6, 40 ) );
               m1.insert ( pair <int, int>  ( 7, 30 ) );

               cout << "The original map m1 is:"<<endl;
               for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
                  cout <<  m1_Iter->first<<" "<<m1_Iter->second<<endl;
              
            }
              The original map m1 is:
              1 20
              2 50
              3 60
              4 40
              6 40
              7 30
              請按任意鍵繼續. . .

            #include <iostream>
            #include <string>
            #include <map>
            using namespace std;
            int main()
            {
                char str[50];
                int count=0;
                map <string,int> counter;
            map <string,int> ::iterator it;
                while(gets(str)!=NULL)
                {
                    counter[str]++;
                    count++;
                }
                for(it=counter.begin();it!=counter.end();it++)
                {
            cout<<it->first<<" "<<it->second<<endl;
                }
                return 0;
            }
            7,   map的基本操作函數:
                  C++ Maps是一種關聯式容器,包含“關鍵字/值”對
                  begin()          返回指向map頭部的迭代器
                  clear()         刪除所有元素
                  count()          返回指定元素出現的次數
                  empty()          如果map為空則返回true
                  end()            返回指向map末尾的迭代器
                  equal_range()    返回特殊條目的迭代器對
                  erase()          刪除一個元素
                  find()           查找一個元素
                  get_allocator()  返回map的配置器
                  insert()         插入元素
                  key_comp()       返回比較元素key的函數
                  lower_bound()    返回鍵值>=給定元素的第一個位置
                  max_size()       返回可以容納的最大元素個數
                  rbegin()         返回一個指向map尾部的逆向迭代器
                  rend()           返回一個指向map頭部的逆向迭代器
                  size()           返回map中元素的個數
                  swap()            交換兩個map
                  upper_bound()     返回鍵值>給定元素的第一個位置
                  value_comp()      返回比較元素value的函數

                                                                

            posted @ 2012-09-25 19:56 hoshelly 閱讀(342) | 評論 (0)編輯 收藏

            Totalsubmit: 790   Accepted: 47  
            Description

            一天,農夫喬伊像往常一樣來到了他的牧場,他突然對他的奶牛產奶量產生了興趣。
            他想知道產奶量處于中間的那頭奶牛的產奶量是多少,處于中間的意思是說,其中有一半牛的產奶量比它多,另一半牛的產奶量比它少。
                這個問題現在交由你來寫程序完成!

            Input

            僅包括一組測試數據,第一行一個正整數N(1<=N<=10,000),接下來N行,每行一個正整數不會超過10^6,第i+1行的數字代表第i頭牛的產奶量。
            Output

            產奶量處于中間的牛的產奶量。
            Sample Input

            5
            1
            2
            4
            5
            3
            Sample Output

            3

            #include<iostream>
            #include<algorithm>
            using namespace std;
            int compare(const void * a,const void * b)
            {
                return *(int*)a - *(int*)b;
            }
            int main()
            {
                int n,cow[10005];
                while(cin>>n)
                {
                    for(int i=1;i<=n;i++)
                    {
                        cin>>cow[i];
                    }
                    qsort(cow+1,n,sizeof(int),compare);
                    cout<<cow[(n+1)/2]<<endl;
                }
                return 0;
            }

            posted @ 2012-09-23 17:18 hoshelly 閱讀(1144) | 評論 (0)編輯 收藏

            給你兩個數a和b,你的任務是計算出1在a和b之間出現的次數,比如說,如果a=1024,b=1032,那么a和b之間的數就是:

            1024 1025 1026 1027 1028 1029 1030 1031 1032

            則有10個1出現在這些數中。

            Input

            輸入不會超過500行。每一行有兩個數a和b,a和b的范圍是0 < a, b < 100000000。輸入兩個0時程序結束,兩個0不作為輸入樣例。

            Output

            對于每一對輸入的a和b,輸出一個數,代表1出現的個數。

            Sample Input


            1 10
            44 497
            346 542
            1199 1748
            1496 1403
            1004 503
            1714 190
            1317 854
            1976 494
            1001 1960
            0 0

            Sample Output

            2
            185
            40
            666
            113
            105
            1133
            512
            1375
            1256


            #include<stdio.h>
            #include<string.h>
            int Find_OneNum(long a)
            {
                long i,b,count=0;
                while(a)
                {
                    b=a%10;
                    if(b == 1)
                    {
                        count++;
                    }
                    a=a/10;
                }
                return count;
            }
            int main()
            {
                long a,b,i,sum;
                while((scanf("%ld%ld",&a,&b) == 2),a)
                {
                    if(a>b)
                    {
                        int temp;
                        temp=a;
                        a=b;
                        b=temp;
                    }
                    sum=0;
                for(i=a;i<=b;i++)
                {
                    sum += Find_OneNum(i);
                }
                printf("%ld\n",sum);
                }
                return 0;
            }

            posted @ 2012-09-23 16:35 hoshelly 閱讀(242) | 評論 (0)編輯 收藏

            2.3x4+3.2x3+2x2+1.2x與5.6x5-2.3x4+3.4x3
            相加結果為:5.6x5+6.6x3 +2x2+1.2x
            首先考慮存儲結構,多項式中的每一項包括“系數”和“指數”兩項,且相加運算可能會改變系數和指數,故應采用鏈式存儲結構。在一個單鏈表結點中,存儲多項式一項的系數和指數。其次,考慮多項式的運算規則:對于兩個一元多項式中所有指數相同的項,對應系數相加,若和不為0,則構成“和多項式”中的一項;對于兩個一元多項式中所有指數不同的項,則分別復抄到“和多項式”中去。


            #include  <iostream>
            using  namespace  std;
                
                typedef struct{
                float  coef;   //系數
                int expn;        //指數
            }DataType;

            struct Node;
            typedef struct Node *PNode;
            struct Node{
                DataType info;
                PNode link;
            };
            typedef  struct  Node *LinkList;

            typedef  LinkList  polynomial;    //帶頭結點的單鏈表表示多項式

            int cmp(DataType a,DataType b){
                int flag;
                if(a.expn<b.expn) flag=-1;
                else if(a.expn==b.expn)    flag=0;
                else flag=1;
                return flag;
            }

            //建立多項式單鏈表
            void CreatPolyn(polynomial &P,int m){    //m為多項式項數
                polynomial r,s;
                P=new struct Node;
                r=P;
                for(int i=0;i<m;i++){
                    s=new struct Node;
                    cout<<"輸入系數和指數:";
                    cin>>s->info.coef>>s->info.expn;
                    r->link=s;
                    r=s;
            }
            r->link=NULL;
            }

            //多項式相加得到新的多項式
            polynomial AddPolyn(polynomial &pa,polynomial &pb){
                polynomial newp,p,q,s,r;
                float sum;
                p=pa->link;
                q=pb->link;
                newp=new struct Node;
                r=newp;
                while(p&&q){
                    switch(cmp(p->info,q->info)){    //比較兩個多項式的指數
                    case -1:        //多項式pa當前結點的指數值小
                        s=new struct Node;
                        s->info.coef=q->info.coef;
                        s->info.expn=q->info.expn;
                        r->link=s;
                        r=s;
                        q=q->link;
                        break;
                    case 0:        //兩個多項式指數值相等
                        sum=p->info.coef+q->info.coef;
                        if (sum!=0.0){
                            s=new struct Node;
                            s->info.coef=sum;
                            s->info.expn=p->info.expn;
                            r->link=s;
                            r=s;
                        }
                        p=p->link;
                        q=q->link;
                        break;
                    case 1:        //多項式pb當前結點的指數值小
                        s=new struct Node;
                        s->info.coef=p->info.coef;
                        s->info.expn=p->info.expn;
                        r->link=s;
                        r=s;
                        p=p->link;
                        break;
                    }//switch
                }//while

            //鏈接pa剩余結點
            while(p){
                s=new struct Node;
                s->info.coef=p->info.coef;
                s->info.expn=p->info.expn;
                r->link=s;
                r=s;
                p=p->link;
            }
            //鏈接pb剩余結點
            while(q){
                s=new struct Node;
                s->info.coef=q->info.coef;
                s->info.expn=q->info.expn;
                r->link=s;
                r=s;
                q=q->link;
            }
            r->link=NULL;
            return newp;
            }    
            //輸出多項式單鏈表
            void PrintPolyn(polynomial p){
                polynomial s;
                s=p->link;
                while(s){
                //輸出系數和指數
                cout<<s->info.coef<<"("<<s->info.expn<<")";
                s=s->link;
            }
            cout<<endl;
            }
                void main(){
                int m,n;
                polynomial p,q;
                cout<<"請輸入多項式pa的項數:";
                cin>>m;
                CreatPolyn(p,m);
                cout<<"請輸入多項式pb的項數:";
                cin>>n;
                CreatPolyn(q,n);
                PrintPolyn(p);
                PrintPolyn(q);
                PrintPolyn(AddPolyn(p,q));
            }





            posted @ 2012-09-23 15:48 hoshelly 閱讀(1157) | 評論 (1)編輯 收藏


            The number 151 is a prime palindrome because it is both a prime number and a palindrome (it is the same number when read forward as backward). Write a program that finds all prime palindromes in the range of two supplied numbers a and b (5 <= a < b <= 1000,000,000); both a and b are considered to be within the range .
            Input
            Line 1: Two integers, a and b
            Output
            The list of palindromic primes in numerical order, one per line.
            Sample Input
            5 500
            Sample Output
            5
            7
            11
            101
            131
            151
            181
            191
            313
            353
            373
            383


            #include<stdio.h>
            #include<math.h>
            #include<stdlib.h>
            int i;
            int Is_Prime(int a)
            {
                double t = a;
                for(i=2;i<=sqrt(t);i++)
                {
                    if(a % i == 0)
                        return 0;
                }
                    return 1;
            }
            int diglen(int c)
            {
                if(c/10 == 0)
                    return 1;
                int count=0;
                while(c)
                {
                    c=c/10;
                    count++;
                }
                return count;
            }

            void IntoStr(int a,char* str) //將數字轉換為字符串
            {
                int h= diglen(a)-1,i=0;
                while(h+1)
                {
                int k,c,d=1;
                for(k=0;k<h;k++)
                {
                    d=d*10;
                }

                c=a/d;//取本次數字的最高位
                str[i++] = c+48;
                a = a%d; //取余數
                h--;
                }
                str[i]='\0'; //最后末尾加字符串結束符
            }

            int Is_Pal(int b)
            {
                int len = diglen(b);
                int ok = 1,j;
                char* str = (char*)malloc(sizeof(char)*(len+1));
                IntoStr(b,str); //將數字轉換為字符數組,也可以用itoa函數或 sprintf函數
                for(i=0,j=len-1;i < len/2;i++,j--)
                {
                    if(str[i] != str[j])
                        ok = 0;
                    
                }
                free(str);
                if(ok == 1)
                    return 1;
                else
                    return 0;
            }

            int main()
            {
                int a,b,j;
                scanf("%d%d",&a,&b);
                for(j=a;j<=b;j++)
                {
                    if(Is_Prime(j) && Is_Pal(j))
                        printf("%d\n",j);
                }
                return 0;
            }

            posted @ 2012-09-22 17:45 hoshelly 閱讀(312) | 評論 (0)編輯 收藏

            卡片游戲

            桌上有一疊牌,從第一張牌開始從上往下依次編號1~n。當至少還剩兩張牌時進行如下操作:把第一張牌扔掉,然后把新的第一張牌放到整疊牌的最后。輸入n,輸出每次扔掉的牌,以及最后剩下的牌。

            樣例輸入:7
            樣例輸出:1 3 5 7 4 2 6

            代碼如下:

            #include<iostream>
            #include<queue>
            using namespace std;
            queue<int> q; //聲明隊列
            int main()    
            {
                int n;
                cin>>n;
                for(int i=0;i<n;i++) q.push(i+1);
                while(!q.empty())
                {
                    cout<<q.front()<<" ";
                    q.pop();
                    if(!q.empty()) //此處需要判斷此時隊列是否為空
                    {
                       q.push(q.front());
                       q.pop();
                    }
                }
                cout<<endl;
                return 0;
            }

            posted @ 2012-09-18 23:12 hoshelly 閱讀(14436) | 評論 (0)編輯 收藏

            #include<stdio.h>
            #include<stdlib.h>
            typedef struct STACKnode* link; //堆棧的鏈表實現
            struct STACKnode
            {
                int item;
                link next;
            };
            static link head;
            link NEW(int item,link next)
            {
                link x=(link)malloc(sizeof(STACKnode));
                x->item = item; //將新建立的結點添加到鏈表頭
                x->next = next; //此時head指向新建立的結點,此結點的next結點,即為原先是頭結點的結點(舊頭結點)
                return x;  //函數返回新建立的頭結點,此結點總是位于棧頂
            }

            void STACKinit()
            {
                head = NULL;
            }
            int STACKempty()  //置空棧
            {
                return head == NULL;
            }
            void STACKpush(int item)
            {
                head = NEW(item,head);
            }
            int STACKpop()
            {
                int item = head->item; //將頭結點上的值取出
                link t = head->next;
                free(head); //釋放此頭結點內存
                head = t; //head指向新的頭結點t
                return item; //返回舊的頭結點的值
            }

            int main()
            {
                int i;
                STACKinit();
                for(i=1;i<=10;i++)
                    STACKpush(i);
                for(i=1;i<=10;i++)
                    printf("%d ",STACKpop());
                printf("\n");
                return 0;
            }

            posted @ 2012-09-17 15:41 hoshelly 閱讀(304) | 評論 (0)編輯 收藏

            #include<stdio.h>
            #include<string.h>
            #include<stdlib.h>
            #define M 1000
            static int N; //N棧的大小
            static int *s;
            void STACKinit(int maxN) //建立一個動態數組s,相當于建立并初始化棧
            {
                s = (int *)malloc(maxN*sizeof(int));
                N=0;
            }
            int STACKempty()
            {
                return N==0;
            }
            void STACKpush(int item)
            {
                s[N++] = item;
            }
            int STACKpop()
            {
                return s[--N];
            }

            int main()  //程序作用:將中綴表達式轉為后綴表達式
            {
                char a[M];
                gets(a); //輸入中綴表達式
                STACKinit(N);
                int i,len=strlen(a);
                for(i=0;i<len;i++)
                {
                    if(a[i] == ')')
                        printf("%c ",STACKpop()); //遇到右括號彈出棧頂的運算符
                    if((a[i] == '+') || (a[i] == '*'))
                        STACKpush(a[i]);             //將運算符壓入棧
                    if((a[i] == '-') || (a[i] =='/'))
                        STACKpush(a[i]);
                    if((a[i] >= '0') && (a[i] <= '9')) //遇到數字則輸出
                        printf("%c ",a[i]);
                }
                return 0;
            }

            示例:

            posted @ 2012-09-17 13:06 hoshelly 閱讀(255) | 評論 (0)編輯 收藏

            #include<stdio.h>
            #include<string.h>
            #include<stdlib.h>
            #define M 1000
            static int N; //N棧的大小
            static int *s;
            void STACKinit(int maxN) //建立一個動態數組s,相當于建立并初始化棧
            {
                s = (int *)malloc(maxN*sizeof(int));
                N=0;
            }
            int STACKempty()
            {
                return N==0;
            }
            void STACKpush(int item)
            {
                s[N++] = item;
            }
            int STACKpop()
            {
                return s[--N];
            }

            int main()
            {
                char a[M];
                gets(a); //輸入后綴表達式,每個整數之后至少要有一個空格
                int i,len,ok;
                char k[15]={' ','0','1','2','3','4','5','6','7','8','9','+','-','/','*'};
                len=strlen(a);
                while(1)  //檢查輸入字符串的合法性(僅限于檢查輸入是否為包含數字和+ - * / 和 ' '的字符串)
                {
                   ok=0;
                   for(i=0;i<len;i++) 
                  {
                     for(int j=0;j<15;j++)
                     {
                        if(a[i] == k[j])
                        {
                            ok=1;
                        }
                     }
                     if(!ok)
                     {
                         printf("Input error! please input again:\n");
                         break;
                     }
                   }
                   if(!ok)
                   {
                      gets(a);
                      len=strlen(a);
                   }
                   else
                       break;
                }

                STACKinit(len); //初始化N=0
                for(i=0;i<len;i++)
                {
                    if(a[i] == '+')
                        STACKpush(STACKpop()+STACKpop());
                    if(a[i] == '*')
                        STACKpush(STACKpop()*STACKpop());
                    if(a[i] == '-')
                    {
                        b = STACKpop();
                        c = STACKpop();
                        STACKpush(c-b);
                    }

                    if(a[i] == '/')
                    {
                        b = STACKpop();
                        c = STACKpop();
                        STACKpush(c/b);
                    }

                        
                    if((a[i] >= '0') && (a[i] <= '9')) //如果遇到數字,則先壓入0進棧
                        STACKpush(0);
                    while((a[i] >='0') && (a[i] <='9'))
                        STACKpush(10*STACKpop()+(a[i++]-'0'));//再將“字符數字”轉換為數字
                }

                printf("%d \n",STACKpop());
                return 0;
            }

            示例:

            posted @ 2012-09-17 12:03 hoshelly 閱讀(1424) | 評論 (0)編輯 收藏

            二分搜索算法的使用前提條件是表中的數已經是有序的,如果還沒排好序,則不能用二分搜索算法。


            int search(int a[],int v,int l,int r) //在數組a[]內查找v,l為左下標,r為右下標
            {
                while (r>=l)
                {
                    int m=(l+r)/2; //每次取數組的一半,此處要加1
                    if(v == a[m]) //如果查找到v,則返回下標m
                        return m;
                    if(v < a[m])  //如果v小于此時的數,則右下標變成r=m-1,去掉數組中一半的數,反之亦然
                        r=m-1;
                    else 
                        l=m+1;
                }
                return -1;
            }

            posted @ 2012-09-16 00:05 hoshelly 閱讀(628) | 評論 (0)編輯 收藏

            僅列出標題
            共12頁: 1 2 3 4 5 6 7 8 9 Last 
            久久精品国产第一区二区三区| 国产日韩久久久精品影院首页| 久久婷婷久久一区二区三区| 性高湖久久久久久久久| 亚洲国产成人精品女人久久久 | 国内精品久久久久久不卡影院| 日韩人妻无码一区二区三区久久| 久久午夜夜伦鲁鲁片免费无码影视 | 亚洲国产另类久久久精品小说| 综合久久给合久久狠狠狠97色| 色婷婷噜噜久久国产精品12p| 亚洲精品无码专区久久同性男| 久久亚洲精品国产精品婷婷| 77777亚洲午夜久久多喷| 无码人妻久久久一区二区三区| 亚洲精品乱码久久久久久按摩| 亚洲成色WWW久久网站| 国产午夜福利精品久久2021 | 久久成人18免费网站| 久久人人爽人人爽人人片AV东京热 | 久久青青草原亚洲av无码app| 精品久久久久久国产| 久久无码一区二区三区少妇| 久久无码AV中文出轨人妻| 一本色道久久88精品综合| 久久成人精品视频| 久久亚洲中文字幕精品一区| 中文字幕乱码久久午夜| AAA级久久久精品无码区| 中文字幕久久精品| 99精品久久精品一区二区| 久久免费国产精品| 91精品国产9l久久久久| 亚洲国产成人乱码精品女人久久久不卡| 久久亚洲熟女cc98cm| 国产精品综合久久第一页| 亚洲国产另类久久久精品| 久久久久一本毛久久久| 久久er99热精品一区二区| 模特私拍国产精品久久| 国产精品久久久久久一区二区三区|