码迷,mamicode.com
首页 > 其他好文 > 详细

USACO dualpal

时间:2014-05-13 06:10:09      阅读:312      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   c   int   

/*
ID:kevin_s1
PROG:dualpal
LANG:C++
*/

#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>

using namespace std;

int N,S;
vector<int> result;

bool isPalindromic(string num){
	bool flag = true;
	int len = num.length();
	for(int i = 0; i <= len/2; i++){
		if(num[i] != num[len - i - 1]){
			flag = false;
		}
	}
	if(num[0] == ‘0‘ || num[len - 1] == ‘0‘)
		flag = false;
	return flag;
}

string trans(int num,int base){
	string str;
	while(num > 0){
		str += num % base + ‘0‘;
		num = num / base;
	}
	reverse(str.begin(),str.end());
	return str;
}

int main(){
	
	freopen("dualpal.in","r",stdin);
	freopen("dualpal.out","w",stdout);
	cin>>N>>S;
	int index = 0;
	for(int i = S + 1; index < N; i++){
		int x = i;
		int count = 0;
		for(int base = 2; base <= 10; base++){
			string num = trans(x,base);
			if(isPalindromic(num)){
				count++;
			}
		}
		if(count >= 2){
			index++;
			result.push_back(x);
		}
	}
	vector<int>::iterator iter = result.begin();
	for(;iter != result.end();iter++){
		cout<<*iter<<endl;
	}
	return 0;
}



提交结果:

USER: Kevin Samuel [kevin_s1]
TASK: dualpal
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 3504 KB]
   Test 2: TEST OK [0.000 secs, 3504 KB]
   Test 3: TEST OK [0.027 secs, 3504 KB]
   Test 4: TEST OK [0.000 secs, 3504 KB]
   Test 5: TEST OK [0.000 secs, 3504 KB]
   Test 6: TEST OK [0.014 secs, 3504 KB]
   Test 7: TEST OK [0.003 secs, 3504 KB]

All tests OK.

YOUR PROGRAM (‘dualpal‘) WORKED FIRST TIME! That‘s fantastic -- and a rare thing. Please accept these special automated congratulations.

Here are the test data inputs:

------- test 1 ----
5 1
------- test 2 ----
9 10
------- test 3 ----
15 9900
------- test 4 ----
10 90
------- test 5 ----
12 125
------- test 6 ----
12 1900
------- test 7 ----
8 500
Keep up the good work!
Thanks for your submission!

USACO dualpal,布布扣,bubuko.com

USACO dualpal

标签:style   blog   class   code   c   int   

原文地址:http://blog.csdn.net/kevin_samuel/article/details/25574009

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!