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 -------
3
Keep up the good work!

Thanks for your submission!

代码:
WA了两次,第一次B>=10的话没有转变成对应的字母,第二次忘了换成文件 加油!
 1/*
 2ID:tbbd4261
 3LANG:C++
 4PROG:palsquare
 5*/

 6#include<iostream>
 7#include<algorithm>
 8#include<string>
 9#include<vector>
10#include<fstream>
11using namespace std;
12string  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=0break;}
22     reverse(str.begin(),str.end());
23     return str;
24}

25string 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}

36int 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

posted on 2010-05-20 11:18 田兵 阅读(138) 评论(0)  编辑 收藏 引用 所属分类: USACO


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2010年5月>
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

导航

统计

常用链接

留言簿(2)

随笔分类(65)

随笔档案(65)

文章档案(2)

ACM

搜索

积分与排名

最新随笔

最新评论

阅读排行榜