• 全部
• 问答

# 浙大1078又WA

jp1984 2004-08-21 08:59:41

#include<iostream>
#include<vector>
using namespace std;
void convert(int n,int base,vector<int>& v)
{
int r=n%base;
int q=n/base;
v.push_back(r);
while(q>0)
{
r=q%base;
q=q/base;
v.push_back(r);
}
}
bool isPalindrome(vector<int>& v)
{
int size=v.size();
int i,j=size-1;
if(size%2==0)
{
for(i=0;i<size/2;i++)
if(v[i]==v[j]&&j>=(size/2))
j--;
else break;
if(i==size/2)
return true;
else
return false;

}
else
{
for(i=0;i<(int)(size/2);i++)
if(v[i]==v[j]&&j>(int)(size/2))
j--;
else break;
if(i==j)
return true;
else return false;
}
}
void print(vector<int>& v)
{
int i;
for(i=0;i<v.size();i++)
cout<<v[i]<<" ";
cout<<endl;
}
int main()
{
int n;
vector<int> v,V;
int i;
int count=2;
while(in>>n){

for(i=2;i<17;i++)
{
convert(n,i,v);
if(isPalindrome(v))
V.push_back(i);
v.erase(v.begin(),v.end());
}
if(!V.empty())
{cout<<"Number "<<n<<" is palindrome in basis ";
print(V); }
else
{ cout<<"Number "<<n<<" is not a palindrome"; cout<<endl;}
V.erase(V.begin(),V.end());

}
return 0;
}

...全文
174 点赞 收藏 5

5 条回复

jp1984 2004-09-13

plpl574 2004-09-12

jp1984 2004-08-23

Sahni 2004-08-23

Number 17 is palindrom in basis 2 4 16

Number 19 is not palindrom

if (!IsPal)
cout<<"Number "<<number<<" is not palindrom";

if (!IsPal)
cout<<"Number "<<number<<" is not a palindrom";

Sahni 2004-08-23

#include <iostream>
using namespace std;

typedef unsigned char int8;

bool IsPalindrom(int number, int basis)
{
int8 a[16];
int i = 0;
int j = 0;
int k;
int Len;

while (i < 16) {
if (number) {
a[i++] = number % basis;
number /= basis;
if (!number) Len = i;
continue;
}
a[i++] = 0;
}

k = Len/2;

while (j < k) {
if (a[j] != a[Len-1-j]) return false;
j++;
}
return true;
}

int main()
{
int number;
bool IsPal;
while(cin>>number) {
if (!number) break;

IsPal = false;

for (int i = 2; i <= 16; i++) {
if (IsPalindrom(number, i)) {
if(!IsPal) {
IsPal = true;
cout<<"Number "<<number<<" is palindrom in basis";
}
cout<<" "<<i;
}
}

if (!IsPal)
cout<<"Number "<<number<<" is not palindrom";

cout<<endl;
}
return 0;
}

3.2w+

2004-08-21 08:59