USACO chapter 1 section 1.2 Palindromic Squares
USER: tianbing tianbing [tbbd4261] TASK: palsquare LANG: C++ Compiling... Compile: OK Executing... Test 1: TEST OK [0.000 secs, 2928 KB] Test 2: TEST OK [0.000 secs, 2928 KB] Test 3: TEST OK [0.011 secs, 2928 KB] Test 4: TEST OK [0.022 secs, 2928 KB] Test 5: TEST OK [0.011 secs, 2928 KB] Test 6: TEST OK [0.011 secs, 2928 KB] Test 7: TEST OK [0.011 secs, 2928 KB] Test 8: TEST OK [0.011 secs, 2928 KB] All tests OK.Your program ('palsquare') produced all correct answers! This is your submission #3 for this problem. Congratulations!
Here are the test data inputs:
------- test 1 ------- 10 ------- test 2 ------- 2 ------- test 3 ------- 5 ------- test 4 ------- 11 ------- test 5 ------- 15 ------- test 6 ------- 18 ------- test 7 ------- 20 ------- test 8 ------- 3Keep up the good work!
代碼:
WA了兩次,第一次B>=10的話沒有轉變成對應的字母,第二次忘了換成文件 加油!
1
/**//*
2
ID:tbbd4261
3
LANG:C++
4
PROG:palsquare
5
*/
6
#include<iostream>
7
#include<algorithm>
8
#include<string>
9
#include<vector>
10
#include<fstream>
11
using namespace std;
12
string pal(int n,int base,bool &flag)
13

{
14
string str="";
15
while(n)
16
{
17
str+=(n%base>=10)?n%base-10+'A':n%base+'0';
18
n=n/base;
19
}
20
for(int i=0,j=str.size()-1; i<str.size()/2;i++,j--)
21
if(str[i]!=str[j])
{ flag=0; break;}
22
reverse(str.begin(),str.end());
23
return str;
24
}
25
string turn(int n,int base)
26

{
27
string str="";
28
while(n)
29
{
30
str+=(n%base>=10)?n%base-10+'A':n%base+'0';
31
n=n/base;
32
}
33
reverse(str.begin(),str.end());
34
return str;
35
}
36
int main()
37

{
38
ifstream fin("palsquare.in");
39
ofstream fout("palsquare.out");
40
int base,i; string str;
41
fin>>base;
42
for(i=1; i<=300; i++)
43
{
44
bool flag=1;
45
string s1=turn(i,base);
46
string s2=pal(i*i,base,flag);
47
if(flag)
{ fout<<s1<<' '<<s2<<endl; }
48
}
49
50
//system("pause");
51
return 0;
52
}
53


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

posted on 2010-05-20 11:18 田兵 閱讀(158) 評論(0) 編輯 收藏 引用 所屬分類: USACO