接上篇:

 

 

 
  
  1. struct tank 
  2.     double dis; 
  3.     double price; 
  4.      
  5.     bool operator < (const tank &t) 
  6.     { return dis < t.dis; } 
  7. }tanks[101]; 
  8.  
  9. int main117()               //贪心解决加油站问题,到达目的花费最少 
  10.     double tankCap,distance,disUnit; 
  11.     int n,i,j; 
  12.     bool flag = true
  13.      
  14.     while(cin>>tankCap>>distance>>disUnit>>n) 
  15.     { 
  16.         flag = true
  17.         for(i=0; i<n; ++i) 
  18.             cin>>tanks[i].price>>tanks[i].dis; 
  19.         tanks[n].price = 0; tanks[n].dis = distance; 
  20.         sort(tanks,tanks+n); 
  21.  
  22. //for(i=0; i<=n; ++i)        
  23. //  cout<<tanks[i].price<<"  "<<tanks[i].dis<<endl; 
  24.  
  25.         double curOil = 0, oilMoney = 0; 
  26.         double Dis = disUnit * tankCap; //满油的状态能跑多远 
  27.         for(i=0; i<n; )                                                  //里面包含三个循环进行查找,其中前两个循环都是找比当前站油价低的站,最后一个找当前站后面的最低油价的站 
  28.         {// cout<<i<<endl; 
  29.             if(tanks[i+1].dis - tanks[i].dis > Dis) 
  30.             { flag = false; cout<<"The maximum travel distance is: "<<tanks[i].dis+Dis<<endl; break; } 
  31.  
  32.             double min_price = tanks[i].price; 
  33.             int min_pos = i; 
  34.             for(j=i+1; j<=n && tanks[j].dis <= tanks[i].dis + curOil * disUnit; ++j)  //先查找当前的油量之内的距离内的最小油价,必须是当前油量之内的 
  35.             { 
  36.                 if(min_price > tanks[j].price) 
  37.                 { 
  38.                     min_price = tanks[j].price; 
  39.                     min_pos = j; 
  40.                 } 
  41.             } 
  42.              
  43.             if(i != min_pos)   //表示找到 
  44.             {                
  45.                 curOil -= (tanks[min_pos].dis-tanks[i].dis)/disUnit; 
  46.                 i = min_pos; 
  47.     //          cout<<"one one: "<<curOil<<"   "<<i<<endl; system("pause"); 
  48.                 continue
  49.             } 
  50.  
  51.             min_price = tanks[i].price;  
  52.             min_pos = i; 
  53.             for(j=1+i; j<=n && tanks[j].dis <= tanks[i].dis + Dis; ++j)   //此步查找距离当前站最近的并且油价比当前站还低 
  54.             { 
  55.                 if(min_price > tanks[j].price) 
  56.                 { 
  57.                     min_pos = j; 
  58.                     break
  59.                 } 
  60.             } 
  61.             if(min_pos != i)   //表示找到 
  62.             { 
  63.                 double oilGap = (tanks[min_pos].dis-tanks[i].dis)/disUnit - curOil; //表示行驶到min_pos点处,需要再加多少油 
  64.                 curOil = 0; 
  65.                 oilMoney += (oilGap*tanks[i].price); 
  66.                 i = min_pos; 
  67.     //          cout<<"two two: "<<curOil<<"   "<<i<<endl; system("pause"); 
  68.                 continue
  69.             } 
  70.  
  71.             min_price = 0XFFFFFFF; 
  72.             min_pos = i; 
  73.             for(j=1+i; j<=n && tanks[j].dis <= Dis +tanks[i].dis; ++j) //此处查找满油状态下不算本站,油价最低的站并且一定可以找到 
  74.             { 
  75.                 if(min_price > tanks[j].price) 
  76.                 { 
  77.                     min_price = tanks[j].price; 
  78.                     min_pos = j; 
  79.                 } 
  80.             } 
  81.             oilMoney += (tankCap-curOil)*tanks[i].price; 
  82.             curOil  = tankCap - (tanks[min_pos].dis-tanks[i].dis)/disUnit; 
  83.             i = min_pos; 
  84. //          cout<<"three three: "<<curOil<<"   "<<i<<endl; system("pause"); 
  85.         } 
  86.         if(flag) cout<<fixed<<setprecision(2)<<oilMoney<<endl; 
  87.     } 
  88.  
  89.     return 0; 
  90.  
  91. string getPre(int n) 
  92. {    
  93.     int iarr[16]; 
  94.     int index = 0; 
  95.     int i = 0; 
  96.     while(n) 
  97.     { 
  98.         if((0X00000001 & n) != 0) //表示此处是1 
  99.             iarr[index++] = i; 
  100.         ++i; 
  101.         n>>=1; 
  102.     //  cout<<n<<endl; 
  103.     } 
  104.     string s ; 
  105.     for(i=index-1; i>=0; --i) 
  106.     { 
  107.         if(iarr[i] == 2) 
  108.         { 
  109.             if(i == 0) s += "2(2)"
  110.             else s += "2(2)+"
  111.     //      cout<<iarr[i]<<"   "<<s<<endl;   
  112.     //      system("pause"); 
  113.         }else if(iarr[i] == 1) 
  114.         { 
  115.             if(i == 0) s += "2"
  116.             else s += "2+"
  117.     //      cout<<iarr[i]<<"   "<<s<<endl;   
  118.     //      system("pause"); 
  119.         }else if(iarr[i] == 0) 
  120.         { 
  121.             if(i == 0) s += "2(0)"
  122.             else s += "2(0)+"
  123.     //      cout<<iarr[i]<<"   "<<s<<endl;   
  124.     //      system("pause"); 
  125.         } 
  126.         else {  
  127.             if( i == 0) s += ("2("+getPre(iarr[i])+")"); 
  128.             else s += ("2("+getPre(iarr[i])+")+"); 
  129.     //      cout<<iarr[i]<<"   "<<s<<endl;   
  130.     //      system("pause"); 
  131.         } 
  132.     } 
  133.     return s ; 
  134.  
  135.  
  136. int main119() 
  137.     int n; 
  138.     while(cin>>n) 
  139.     { 
  140.         string s = getPre(n); 
  141.         cout<<s <<endl; 
  142.     } 
  143.     return 0; 
  144. /* 
  145. int main120() 
  146. { 
  147. //  char tree[26][26]; 
  148.     vector<string> tree[26]; 
  149.     char str[80]; 
  150.     bool root[26]; 
  151.     memset(root,true,sizeof(root)); 
  152.     while(gets(str)) 
  153.     { 
  154.         int i=0; 
  155.         char parent = '*'; 
  156.         while(str[i] != 0) 
  157.         { 
  158.             if(isalpha(str[i])) 
  159.             { 
  160.                 if(parent == '*') 
  161.                     parent = str[i]; 
  162.                 else 
  163.                 { 
  164.                     tree[parent-'a'].push_back(str[i]);  
  165.                     parent = str[i]; 
  166.                     root[str[i]-'a'] = false; 
  167.                 } 
  168.             } 
  169.             ++i; 
  170.         } 
  171.     } 
  172.     return 0; 
  173. } 
  174. */ 
  175. void deleteNode(BTree3 *&root,int key) 
  176.     BTree3 *f = NULL, *p = root; 
  177.  
  178.     while(p!=NULL && p->data!=key) 
  179.     { 
  180.         f = p; 
  181.         if(p->data > key) 
  182.             p = p->lchild; 
  183.         else p = p->rchild; 
  184.     } 
  185.      
  186.     if(p!=NULL)  //表示在排序二叉树中找到了值为key的节点,若找不到则一定是到达了NULL节点 
  187.     {               //如果f为空则表示找到的是根节点 
  188.             if(key == 4) 
  189.             { 
  190.                 if(p->lchild != NULL) cout<<"left : "<<p->lchild->data; 
  191.                 if(p->rchild != NULL) cout<<"right : "<<p->rchild->data<<endl;  
  192.             } 
  193.  
  194.         if(p->lchild == NULL && p->rchild == NULL)     //节点是叶节点 
  195.         { 
  196.             if(f) 
  197.             { 
  198.                 if(f->lchild == p) 
  199.                     f->lchild = NULL; 
  200.                 else f->rchild = NULL; 
  201.                 free(p); 
  202.                 p = NULL; 
  203.             }else 
  204.             { 
  205.                 free(p); 
  206.                 root = NULL;     
  207.             }            
  208.         }else if(p->lchild == NULL)          //节点的左节点为空 
  209.         { 
  210.             if(key == 4) 
  211.                 cout<<"左节点为空"<<endl; 
  212.             if(f) 
  213.             { 
  214.                 if(f->lchild == p) 
  215.                     f->lchild = p->rchild; 
  216.                 else if(f->rchild == p) 
  217.                     f->rchild = p->rchild; 
  218.                 free(p); 
  219.             }else 
  220.             {                
  221.                 BTree3 *tmp = p;     
  222.                 p = p->rchild; 
  223.                 root = p; 
  224.                 free(tmp); 
  225.             } 
  226.         }else if(p->rchild == NULL)     //节点的右节点为空 
  227.         {  
  228.             if(f) 
  229.             { 
  230.                 if(f->lchild == p) 
  231.                     f->lchild = p->lchild; 
  232.                 else 
  233.                     f->rchild = p->lchild; 
  234.                 free(p); 
  235.             }else 
  236.             { 
  237.                 BTree3 *tmp = p; 
  238.                 p = p->lchild; 
  239.                 root = p; 
  240.                 free(tmp); 
  241.             } 
  242.              
  243.  
  244.         }else            //节点的左右节点都不为空,   因为这种方法是把找到的节点的值进行替换,所以不需要改变节点的左右孩子指针 
  245.         { 
  246.             BTree3 *pp = p->lchild; 
  247.             BTree3 *ff = p; 
  248.              
  249.             while(pp->rchild != NULL)  //此处找其左子树的最右节点 
  250.             { 
  251.                 ff = pp; 
  252.                 pp = pp->rchild; 
  253.             } 
  254.  
  255.             if(ff == p)  //表示p的左子树的第一个节点 
  256.             { 
  257.                 p->data = pp->data; 
  258.                 p->lchild = pp->lchild; 
  259.                 free(pp); 
  260.             }else 
  261.             { 
  262.                 p->data = pp->data; 
  263.                 ff->rchild = pp->lchild; 
  264.                 free(pp); 
  265.             } 
  266.         } 
  267.     } 
  268.  
  269. void Insert(BTree3 *&root,int key) 
  270.     if(root == NULL) 
  271.     { 
  272.         root = (BTree3 *)malloc(sizeof(BTree3)); 
  273.         root->data = key; 
  274.         root->lchild = root->rchild = NULL;   
  275.     }else if(root->data > key) 
  276.         Insert(root->lchild,key); 
  277.     else Insert(root->rchild,key); 
  278.  
  279. void  main121() 
  280.     int n; 
  281.     BTree3 *root = NULL; 
  282.     while(cin>>n) 
  283.     { 
  284.         for(int i=0; i<n; ++i) 
  285.         { 
  286.             int tmp; 
  287.             cin>>tmp; 
  288.             Insert(root,tmp); 
  289.         } 
  290.          
  291.         postOrder3(root); 
  292.         while(cin>>n && n>=0) 
  293.         { 
  294.             deleteNode(root,n); 
  295.             postOrder3(root); 
  296.         } 
  297.     } 
  298. void main123()   //A^B 二分法求幂 
  299.     int A,B; 
  300.     while(cin>>A>>B && A && B) 
  301.     { 
  302.         int AA = A; 
  303.         A = 1; 
  304.         while(B) 
  305.         { 
  306.             if(0X00000001 & B)  //表示最低位是1 
  307.             { 
  308.                 A *= AA; 
  309.                 A %= 1000; 
  310.             } 
  311.             AA *= AA; 
  312.             AA %= 10000; 
  313.             B>>=1; 
  314.         } 
  315.         cout<<A<<endl; 
  316.     } 
  317.  
  318. void judgeAVL(BTree3 *root,bool &balance,int &h)  //判断是否为平衡二叉树 
  319.     if(root == NULL) { balance = true; h = 0; return; } 
  320.     if(root->lchild == NULL && root->rchild == NULL) 
  321.     { balance = true;  h = 1; return; } 
  322.     int hl=0,hr=0; 
  323.     bool bl=true,br=true
  324.     judgeAVL(root->lchild,bl,hl); 
  325.     judgeAVL(root->rchild,br,hr); 
  326.     h = 1+(hl>hr?hl:hr); 
  327.     if(abs(hl-hr) < 2) 
  328.         balance = bl & br; 
  329.     else balance = false
  330.  
  331.  
  332. int root3[101]; 
  333. int findRoot3(int x) 
  334. {    
  335.     if(root3[x] == -1) return x; 
  336.     else 
  337.     { 
  338.         int tmp = findRoot3(root3[x]); 
  339.         root3[x] = tmp; 
  340.         return tmp; 
  341.     } 
  342.  
  343. void main124() 
  344.     int n,m; 
  345.     while(cin>>n>>m) 
  346.     { 
  347.         for(int i=1; i<=n; ++i) 
  348.             root3[i] = -1; 
  349.         for(i=1; i<=m; ++i) 
  350.         { 
  351.             int x,y; 
  352.             cin>>x>>y; 
  353.             x = findRoot3(x); 
  354.             y = findRoot3(y); 
  355.             if(x != y) 
  356.                 root3[y] = x; 
  357.         } 
  358.         int cnt = -1; 
  359.         for(i=1; i<=n; ++i) 
  360.             if(root3[i] == -1) 
  361.                 ++cnt; 
  362.         cout<<cnt<<endl; 
  363.     } 
  364.  
  365. int edges[101][101]; 
  366.  
  367. void main125() 
  368.     int n,m; 
  369.     while(cin>>n>>m) 
  370.     { 
  371.         if(n==0 && m==0) break
  372.         int i,j,k; 
  373.         for(i=0; i<101; ++i) 
  374.             for(j=0; j<101; ++j) 
  375.             { 
  376.                 edges[i][j] = -1;   //-1表示不可达             
  377.             } 
  378.         for(i=1; i<=m; ++i) 
  379.         { 
  380.             int a,b,c; 
  381.             cin>>a>>b>>c; 
  382.             edges[a][b] = c; 
  383.             edges[b][a] = c; 
  384.         } 
  385.   
  386.         for(k=1; k<=n; ++k) 
  387.         { 
  388.             for(i=1; i<=n; ++i) 
  389.             { 
  390.                 for(j=1; j<=n; ++j) 
  391.                     if(edges[i][k] == -1 || edges[k][j] == -1) 
  392.                         continue
  393.                     if(edges[i][j] == -1 || edges[i][j] > edges[i][k]+edges[k][j]) 
  394.                         edges[i][j] = edges[i][k] + edges[k][j]; 
  395.             }        
  396.         } 
  397.         cout<<edges[1][n]<<endl; 
  398.     } 
  399. }