0%

c++练习五

1. POJ1028

题目

  • Description
    Standard web browsers contain features to move backward and forward among the pages recently visited. One way to implement these features is to use two stacks to keep track of the pages that can be reached by moving backward and forward. In this problem, you are asked to implement this.
    The following commands need to be supported:
    BACK: Push the current page on the top of the forward stack. Pop the page from the top of the backward stack, making it the new current page. If the backward stack is empty, the command is ignored.
    FORWARD: Push the current page on the top of the backward stack. Pop the page from the top of the forward stack, making it the new current page. If the forward stack is empty, the command is ignored.
    VISIT : Push the current page on the top of the backward stack, and make the URL specified the new current page. The forward stack is emptied.
    QUIT: Quit the browser.
    Assume that the browser initially loads the web page at the URL http://www.acm.org/

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include<iostream>
#include<string>
#define MAX 1024
using namespace std;
int judgement(string str)
{
string str1;
if(str=="BACK") return 1;
else
if(str=="FORWARD") return 2;
else
if(str=="VISIT") return 3;
else return 0;
}
void web()
{
string str1[MAX],str2[MAX],str[MAX],index,temp;
int n=0,m=0,store=0;
str1[0]="http://www.acm.org/";
cin>>index;
while(index!="QUIT")
{
if(judgement(index)==3)
{
m=0;
n++;
cin>>index;
str[store]=index;
str1[n]=index;
store++;
}
if(judgement(index)==1)
{
str2[m]=str1[n];
m++;
n--;
if(n<0) //对于 str1数组溢出情况的处理
{
str[store]="Ignored";
store++;
m--;
n++;
str1[0]="http://www.acm.org/";
}
else
{
str[store]=str1[n];
store++;
}
}
if(judgement(index)==2)
{
n++;
m--;
if(m<0)//对于 str2数组溢出情况的处理
{ m++;
n--;
str[store]="Ignored";
store++;
}
else
{
str1[n]=str2[m];
str[store]=str1[n];
store++;
}
}
cin>>index;
}

for(int i=0;i<store;i++)
cout<<str[i]<<endl;

}
int main()
{
web();
}

POJ1013

使用枚举法:
题目

  • Description
    Sally Jones has a dozen Voyageur silver dollars. However, only eleven of the coins are true silver dollars; one coin is counterfeit even though its color and size make it indistinguishable from the real silver dollars. The counterfeit coin has a different weight from the other coins but Sally does not know if it is heavier or lighter than the real coins.
    Happily, Sally has a friend who loans her a very accurate balance scale. The friend will permit Sally three weighings to find the counterfeit coin. For instance, if Sally weighs two coins against each other and the scales balance then she knows these two coins are true. Now if Sally weighs
    one of the true coins against a third coin and the scales do not balance then Sally knows the third coin is counterfeit and she can tell whether it is light or heavy depending on whether the balance on which it is placed goes up or down, respectively.
    By choosing her weighings carefully, Sally is able to ensure that she will find the counterfeit coin with exactly three weighings.
  • Input
    The first line of input is an integer n (n > 0) specifying the number of cases to follow. Each case consists of three lines of input, one for each weighing. Sally has identified each of the coins with the letters A–L. Information on a weighing will be given by two strings of letters and then one of the words up'',down’’, or ``even’’. The first string of letters will represent the coins on the left balance; the second string, the coins on the right balance. (Sally will always place the same number of coins on the right balance as on the left balance.) The word in the third position will tell whether the right side of the balance goes up, down, or remains even.
  • Output
    For each case, the output will identify the counterfeit coin by its letter and tell whether it is heavy or light. The solution will always be uniquely determined.
  • Sample Input
    1
    ABCD EFGH even
    ABCI EFJK up
    ABIJ EFGH even
    Sample Output
    K is the counterfeit coin and it is light.

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <cstring>
using namespace std;
char Left[3][7]; //天平左边硬币
char Right[3][7]; //天平右边硬币
char result[3][7]; //称量结果
bool IsFake(char c,bool light);//light为真表示假设假币为轻,否则表示假设假币为重
int main()
{
int t;
cin >> t;
while(t--)
{
for(int i = 0;i < 3; ++i) cin >> Left[i] >> Right[i] >> result[i];
for(char c='A'; c<='L';c++)
{
if( IsFake(c,true) )//假设c这个硬币为假硬币而且它比真硬币轻
{
cout << c << " is the counterfeit coin and it is light.\n";
break;
}
else if( IsFake(c,false) )//假设c这个硬币为假硬币而且它比真硬币重
{
cout << c << " is the counterfeit coin and it is heavy.\n";
break;
}
}
}
return 0;
}
bool IsFake(char c,bool light)//light 为真表示假设假币为轻,否则表示假设假币为重
{
for(int i = 0;i < 4; i++)
{
char * pLeft,*pRight; //指向天平两边的字符串
if(light)
{
pLeft = Left[i];
pRight = Right[i];
}
else
{
pLeft = Right[i];
pRight = Left[i];
}

switch(result[i][0])
{
case 'u':
if (strchr(pRight,c) == NULL) return false;
break;
case 'e':
if(strchr(pLeft,c) || strchr(pRight,c)) return false;
break;
case 'd':
if ( strchr(pLeft,c) == NULL) return false;
break;
}
}
return true;
}