0%

C++练习二(poj练习)

1. 练习一:poj.org的1001题

题目:

  • Description
    Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many computer systems. This problem requires that you write a program to compute the exact value of Rn where R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25.
  • Input
    The input will consist of a set of pairs of values for R and n. The R value will occupy columns 1 through 6, and the n value will be in columns 8 and 9.
  • Output
    The output will consist of one line for each line of input giving the exact value of R^n. Leading zeros should be suppressed in the output. Insignificant trailing zeros must not be printed. Don’t print the decimal point if the result is an integer.
  • Sample Input

95.123 12
0.4321 20
5.1234 15
6.7592 9
98.999 10
1.0100 12

  • Sample Output
    548815620517731830194541.899025343415715973535967221869852721
    .00000005148554641076956121994511276767154838481760200726351203835429763013462401
    43992025569.928573701266488041146654993318703707511666295476720493953024

29448126.764121021618164430206909037173276672
90429072743629540498.107596019456651774561044010001
1.126825030131969720661201
代码:

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
#include<iostream>
#include<string>
#include<cmath>
#include<sstream>
using namespace std;
int main()
{
string ch,s;
double R,result;
int n;

while(cin>>R>>n)
{
result=pow(R,n);
if(result<1)
{
s=to_string(result);
cout<<s.substr(1,s.length()-1)<<endl;
}
else
{
s=to_string(result);
cout<<s<<endl;
}

if((ch=cin.get())=="EOF")
break;
}

}

参考

  1. https://blog.csdn.net/happyjacob/article/details/81211713
  2. https://blog.csdn.net/Imagirl1/article/details/82707672

问题:
可能是使用了to_string导致精确度不够,而to_string不通用(只在增加c++11才能用),目前本人还不能解决。


2. 练习二的poj.org的1002题

问题:

  • Description
    Businesses like to have memorable telephone numbers. One way to make a telephone number memorable is to have it spell a memorable word or phrase. For example, you can call the University of Waterloo by dialing the memorable TUT-GLOP. Sometimes only part of the number is used to spell a word. When you get back to your hotel tonight you can order a pizza from Gino’s by dialing 310-GINO. Another way to make a telephone number memorable is to group the digits in a memorable way. You could order your pizza from Pizza Hut by calling their ``three tens’’ number 3-10-10-10.

The standard form of a telephone number is seven decimal digits with a hyphen between the third and fourth digits (e.g. 888-1200). The keypad of a phone supplies the mapping of letters to numbers, as follows:

A, B, and C map to 2
D, E, and F map to 3
G, H, and I map to 4
J, K, and L map to 5
M, N, and O map to 6
P, R, and S map to 7
T, U, and V map to 8
W, X, and Y map to 9

There is no mapping for Q or Z. Hyphens are not dialed, and can be added and removed as necessary. The standard form of TUT-GLOP is 888-4567, the standard form of 310-GINO is 310-4466, and the standard form of 3-10-10-10 is 310-1010.

Two telephone numbers are equivalent if they have the same standard form. (They dial the same number.)

Your company is compiling a directory of telephone numbers from local businesses. As part of the quality control process you want to check that no two (or more) businesses in the directory have the same telephone number.

  • Input
    The input will consist of one case. The first line of the input specifies the number of telephone numbers in the directory (up to 100,000) as a positive integer alone on the line. The remaining lines list the telephone numbers in the directory, with each number alone on a line. Each telephone number consists of a string composed of decimal digits, uppercase letters (excluding Q and Z) and hyphens. Exactly seven of the characters in the string will be digits or letters.
  • Output
    Generate a line of output for each telephone number that appears more than once in any form. The line should give the telephone number in standard form, followed by a space, followed by the number of times the telephone number appears in the directory. Arrange the output lines by telephone number in ascending lexicographical order. If there are no duplicates in the input print the line:

No duplicates.

  • Sample Input
    12
    4873279
    ITS-EASY
    888-4567
    3-10-10-10
    888-GLOP
    TUT-GLOP
    967-11-11
    310-GINO
    F101010
    888-1200
  • 4-8-7-3-2-7-9-
    487-3279
  • Sample Output
    310-1010 2
    487-3279 4
    888-4567 3

代码:

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <string>
using namespace std;
int String(string str)
{
int value=0;
for(size_t i=0;i<str.size();i++)
{
if((str[i]>='0'&&str[i]<='9')||(str[i]>='A'&&str[i]<='Y'&&str[i]!='Q'))
{
if(str[i]>='0'&&str[i]<='9')
{
value*=10;
value += str[i] - '0';
}
if(str[i]>='A'&&str[i]<='P')
{

value*=10;
value+=((str[i]+1)/3)%10;
}
if(str[i]>='R'&&str[i]<='Y')
{
value*=10;
value+=((str[i])/3)%10;
}
}
}
return value;
}
void order(int s[],int n)
{
int t=0;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(s[i]>s[j])
{
t=s[i];
s[i]=s[j];
s[j]=t;
}
}
}
}


void print(int valu)
{
char s[8];
for(int i=7;i>=0;i--)
{
if(i==3)
s[i]='-';
else
{
s[i]=valu%10+'0';
valu/=10;
}

}
for(int i=0;i<8;i++)
cout<<s[i];
}


int main()
{
int n,store[100],flag=0;
string s;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>s;
store[i]=String(s);
}
order(store,n);
for(int i=0;i<n;i++)
{

for(int j=i+1;j<n;j++)
{
if(store[i]==store[j])
flag+=1;
}
if(flag>=1)
{

print(store[i]);
cout<<" "<<flag+1<<endl;
for(int k=i+1;k<n;k++)
{
if(store[k]==store[i])
{

for(int y=k;y<n;y++)
store[y]=store[y+1];
n=n-1;
k=k-1;
}

}
flag=0;
}
}


return 0;


}

应该大概可能解决了吧。运行时间有点长;