接上篇:
- struct tank
- {
- double dis;
- double price;
- bool operator < (const tank &t)
- { return dis < t.dis; }
- }tanks[101];
- int main117() //贪心解决加油站问题,到达目的花费最少
- {
- double tankCap,distance,disUnit;
- int n,i,j;
- bool flag = true;
- while(cin>>tankCap>>distance>>disUnit>>n)
- {
- flag = true;
- for(i=0; i<n; ++i)
- cin>>tanks[i].price>>tanks[i].dis;
- tanks[n].price = 0; tanks[n].dis = distance;
- sort(tanks,tanks+n);
- //for(i=0; i<=n; ++i)
- // cout<<tanks[i].price<<" "<<tanks[i].dis<<endl;
- double curOil = 0, oilMoney = 0;
- double Dis = disUnit * tankCap; //满油的状态能跑多远
- for(i=0; i<n; ) //里面包含三个循环进行查找,其中前两个循环都是找比当前站油价低的站,最后一个找当前站后面的最低油价的站
- {// cout<<i<<endl;
- if(tanks[i+1].dis - tanks[i].dis > Dis)
- { flag = false; cout<<"The maximum travel distance is: "<<tanks[i].dis+Dis<<endl; break; }
- double min_price = tanks[i].price;
- int min_pos = i;
- for(j=i+1; j<=n && tanks[j].dis <= tanks[i].dis + curOil * disUnit; ++j) //先查找当前的油量之内的距离内的最小油价,必须是当前油量之内的
- {
- if(min_price > tanks[j].price)
- {
- min_price = tanks[j].price;
- min_pos = j;
- }
- }
- if(i != min_pos) //表示找到
- {
- curOil -= (tanks[min_pos].dis-tanks[i].dis)/disUnit;
- i = min_pos;
- // cout<<"one one: "<<curOil<<" "<<i<<endl; system("pause");
- continue;
- }
- min_price = tanks[i].price;
- min_pos = i;
- for(j=1+i; j<=n && tanks[j].dis <= tanks[i].dis + Dis; ++j) //此步查找距离当前站最近的并且油价比当前站还低
- {
- if(min_price > tanks[j].price)
- {
- min_pos = j;
- break;
- }
- }
- if(min_pos != i) //表示找到
- {
- double oilGap = (tanks[min_pos].dis-tanks[i].dis)/disUnit - curOil; //表示行驶到min_pos点处,需要再加多少油
- curOil = 0;
- oilMoney += (oilGap*tanks[i].price);
- i = min_pos;
- // cout<<"two two: "<<curOil<<" "<<i<<endl; system("pause");
- continue;
- }
- min_price = 0XFFFFFFF;
- min_pos = i;
- for(j=1+i; j<=n && tanks[j].dis <= Dis +tanks[i].dis; ++j) //此处查找满油状态下不算本站,油价最低的站并且一定可以找到
- {
- if(min_price > tanks[j].price)
- {
- min_price = tanks[j].price;
- min_pos = j;
- }
- }
- oilMoney += (tankCap-curOil)*tanks[i].price;
- curOil = tankCap - (tanks[min_pos].dis-tanks[i].dis)/disUnit;
- i = min_pos;
- // cout<<"three three: "<<curOil<<" "<<i<<endl; system("pause");
- }
- if(flag) cout<<fixed<<setprecision(2)<<oilMoney<<endl;
- }
- return 0;
- }
- string getPre(int n)
- {
- int iarr[16];
- int index = 0;
- int i = 0;
- while(n)
- {
- if((0X00000001 & n) != 0) //表示此处是1
- iarr[index++] = i;
- ++i;
- n>>=1;
- // cout<<n<<endl;
- }
- string s ;
- for(i=index-1; i>=0; --i)
- {
- if(iarr[i] == 2)
- {
- if(i == 0) s += "2(2)";
- else s += "2(2)+";
- // cout<<iarr[i]<<" "<<s<<endl;
- // system("pause");
- }else if(iarr[i] == 1)
- {
- if(i == 0) s += "2";
- else s += "2+";
- // cout<<iarr[i]<<" "<<s<<endl;
- // system("pause");
- }else if(iarr[i] == 0)
- {
- if(i == 0) s += "2(0)";
- else s += "2(0)+";
- // cout<<iarr[i]<<" "<<s<<endl;
- // system("pause");
- }
- else {
- if( i == 0) s += ("2("+getPre(iarr[i])+")");
- else s += ("2("+getPre(iarr[i])+")+");
- // cout<<iarr[i]<<" "<<s<<endl;
- // system("pause");
- }
- }
- return s ;
- }
- int main119()
- {
- int n;
- while(cin>>n)
- {
- string s = getPre(n);
- cout<<s <<endl;
- }
- return 0;
- }
- /*
- int main120()
- {
- // char tree[26][26];
- vector<string> tree[26];
- char str[80];
- bool root[26];
- memset(root,true,sizeof(root));
- while(gets(str))
- {
- int i=0;
- char parent = '*';
- while(str[i] != 0)
- {
- if(isalpha(str[i]))
- {
- if(parent == '*')
- parent = str[i];
- else
- {
- tree[parent-'a'].push_back(str[i]);
- parent = str[i];
- root[str[i]-'a'] = false;
- }
- }
- ++i;
- }
- }
- return 0;
- }
- */
- void deleteNode(BTree3 *&root,int key)
- {
- BTree3 *f = NULL, *p = root;
- while(p!=NULL && p->data!=key)
- {
- f = p;
- if(p->data > key)
- p = p->lchild;
- else p = p->rchild;
- }
- if(p!=NULL) //表示在排序二叉树中找到了值为key的节点,若找不到则一定是到达了NULL节点
- { //如果f为空则表示找到的是根节点
- if(key == 4)
- {
- if(p->lchild != NULL) cout<<"left : "<<p->lchild->data;
- if(p->rchild != NULL) cout<<"right : "<<p->rchild->data<<endl;
- }
- if(p->lchild == NULL && p->rchild == NULL) //节点是叶节点
- {
- if(f)
- {
- if(f->lchild == p)
- f->lchild = NULL;
- else f->rchild = NULL;
- free(p);
- p = NULL;
- }else
- {
- free(p);
- root = NULL;
- }
- }else if(p->lchild == NULL) //节点的左节点为空
- {
- if(key == 4)
- cout<<"左节点为空"<<endl;
- if(f)
- {
- if(f->lchild == p)
- f->lchild = p->rchild;
- else if(f->rchild == p)
- f->rchild = p->rchild;
- free(p);
- }else
- {
- BTree3 *tmp = p;
- p = p->rchild;
- root = p;
- free(tmp);
- }
- }else if(p->rchild == NULL) //节点的右节点为空
- {
- if(f)
- {
- if(f->lchild == p)
- f->lchild = p->lchild;
- else
- f->rchild = p->lchild;
- free(p);
- }else
- {
- BTree3 *tmp = p;
- p = p->lchild;
- root = p;
- free(tmp);
- }
- }else //节点的左右节点都不为空, 因为这种方法是把找到的节点的值进行替换,所以不需要改变节点的左右孩子指针
- {
- BTree3 *pp = p->lchild;
- BTree3 *ff = p;
- while(pp->rchild != NULL) //此处找其左子树的最右节点
- {
- ff = pp;
- pp = pp->rchild;
- }
- if(ff == p) //表示p的左子树的第一个节点
- {
- p->data = pp->data;
- p->lchild = pp->lchild;
- free(pp);
- }else
- {
- p->data = pp->data;
- ff->rchild = pp->lchild;
- free(pp);
- }
- }
- }
- }
- void Insert(BTree3 *&root,int key)
- {
- if(root == NULL)
- {
- root = (BTree3 *)malloc(sizeof(BTree3));
- root->data = key;
- root->lchild = root->rchild = NULL;
- }else if(root->data > key)
- Insert(root->lchild,key);
- else Insert(root->rchild,key);
- }
- void main121()
- {
- int n;
- BTree3 *root = NULL;
- while(cin>>n)
- {
- for(int i=0; i<n; ++i)
- {
- int tmp;
- cin>>tmp;
- Insert(root,tmp);
- }
- postOrder3(root);
- while(cin>>n && n>=0)
- {
- deleteNode(root,n);
- postOrder3(root);
- }
- }
- }
- void main123() //A^B 二分法求幂
- {
- int A,B;
- while(cin>>A>>B && A && B)
- {
- int AA = A;
- A = 1;
- while(B)
- {
- if(0X00000001 & B) //表示最低位是1
- {
- A *= AA;
- A %= 1000;
- }
- AA *= AA;
- AA %= 10000;
- B>>=1;
- }
- cout<<A<<endl;
- }
- }
- void judgeAVL(BTree3 *root,bool &balance,int &h) //判断是否为平衡二叉树
- {
- if(root == NULL) { balance = true; h = 0; return; }
- if(root->lchild == NULL && root->rchild == NULL)
- { balance = true; h = 1; return; }
- int hl=0,hr=0;
- bool bl=true,br=true;
- judgeAVL(root->lchild,bl,hl);
- judgeAVL(root->rchild,br,hr);
- h = 1+(hl>hr?hl:hr);
- if(abs(hl-hr) < 2)
- balance = bl & br;
- else balance = false;
- }
- int root3[101];
- int findRoot3(int x)
- {
- if(root3[x] == -1) return x;
- else
- {
- int tmp = findRoot3(root3[x]);
- root3[x] = tmp;
- return tmp;
- }
- }
- void main124()
- {
- int n,m;
- while(cin>>n>>m)
- {
- for(int i=1; i<=n; ++i)
- root3[i] = -1;
- for(i=1; i<=m; ++i)
- {
- int x,y;
- cin>>x>>y;
- x = findRoot3(x);
- y = findRoot3(y);
- if(x != y)
- root3[y] = x;
- }
- int cnt = -1;
- for(i=1; i<=n; ++i)
- if(root3[i] == -1)
- ++cnt;
- cout<<cnt<<endl;
- }
- }
- int edges[101][101];
- void main125()
- {
- int n,m;
- while(cin>>n>>m)
- {
- if(n==0 && m==0) break;
- int i,j,k;
- for(i=0; i<101; ++i)
- for(j=0; j<101; ++j)
- {
- edges[i][j] = -1; //-1表示不可达
- }
- for(i=1; i<=m; ++i)
- {
- int a,b,c;
- cin>>a>>b>>c;
- edges[a][b] = c;
- edges[b][a] = c;
- }
- for(k=1; k<=n; ++k)
- {
- for(i=1; i<=n; ++i)
- {
- for(j=1; j<=n; ++j)
- if(edges[i][k] == -1 || edges[k][j] == -1)
- continue;
- if(edges[i][j] == -1 || edges[i][j] > edges[i][k]+edges[k][j])
- edges[i][j] = edges[i][k] + edges[k][j];
- }
- }
- cout<<edges[1][n]<<endl;
- }
- }