改写要求1:用单链表实现
#include <cstdlib> #include <iostream> using namespace std; struct LinkNode { int data; LinkNode *next; }; class PALINDROME { int low,up; int a[100]; int count; public: PALINDROME(int t1,int t2); int IsPalin(int x); LinkNode* IsPalinAndStore(); void OutputResults(LinkNode* Head); }; PALINDROME::PALINDROME(int t1,int t2) { count=0; low=t1; up=t2; } LinkNode* PALINDROME::IsPalinAndStore() { LinkNode* Head=new LinkNode; Head->next=NULL; LinkNode* p=Head; for(int i=low;i<=up;i++) { int x=i*i; if(IsPalin(x)) { LinkNode* newLinkNode=new LinkNode; newLinkNode->next=NULL; newLinkNode->data=i; p->next=newLinkNode; p=newLinkNode; } } return Head; } void PALINDROME::OutputResults(LinkNode* Head) { LinkNode* p=Head->next; cout<<"count="<<count<<endl; cout<<"x"<<' '<<"x*x"<<endl; while(p) { cout<<p->data<<' '<<p->data*p->data<<endl; p=p->next; } } int PALINDROME::IsPalin(int x) { int i=0,j,n; int a[100]; while(x) { a[i]=x%10; x=x/10; i++; } n=i; for(i=0,j=n-1;i<=j;i++,j--) if(a[i]!=a[j]) return 0; return 1; } int main(int argc, char *argv[]) { LinkNode* Head=new LinkNode; PALINDROME p(100,1000); Head=p.IsPalinAndStore(); p.OutputResults(Head); system("PAUSE"); return EXIT_SUCCESS; }