- 在线时间
- 0 小时
- 最后登录
- 2005-4-22
- 注册时间
- 2005-4-22
- 听众数
- 2
- 收听数
- 0
- 能力
- 0 分
- 体力
- 72 点
- 威望
- 0 点
- 阅读权限
- 20
- 积分
- 26
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 7
- 主题
- 2
- 精华
- 0
- 分享
- 0
- 好友
- 0
升级   22.11% 该用户从未签到
 |
< >#include<iostream>: v3 I2 Z+ W5 L4 x$ V9 L- L
#include<fstream>
8 m+ g1 Q9 s6 A J% f; v#include"string.h") C, S7 T& G: R# U0 {, E/ o! x
//#include"time.h"
% k3 d3 }7 H9 H- L, r0 P! p% _using namespace std; s9 Y# L/ E# U$ H; }
ifstream in("input.txt");
2 H* l0 E# \* H2 O, J/ vofstream out("output.txt"); f+ P1 I8 g9 Q. T- r
class String : V9 _8 ?/ {, o4 W I: n! Z. Y# G" b
{: e8 \6 V( r* o, G4 }& _1 [4 H
public:# w4 F- Z0 O, o
String(char *s="");
2 z* m# ^. n. {6 N5 O. K* r+ V1 e String(const String& s);
. {1 ^2 T+ {4 _; U ~String() {delete[] str; delete[] pre;}$ ?: W0 Y+ K0 t
String& operator=(const String& s);
0 f6 h e1 \$ o int length()const {return size-1;}' }/ a0 J' j7 ^# `" i$ m
int get();
* ~$ z; P; I" R5 H+ ^9 f% k String& change(int *p,int n);6 r% e$ J9 ^! O' i8 L/ ^+ m, C
void get(int *p); B- A4 }3 Q# l- g
void display(){out<<str<<endl;}
) z' ] R! [/ Rprivate:5 H4 O8 S% [( ? ^/ E# d+ K
char *str;6 E! d# Q- ~, z+ I
int *pre;5 t, x4 x, l9 I" o. f0 x* u! [
int size;
; J/ T0 ^+ ]2 {9 V" C# ]};</P>
) E. P" q9 y* a8 A7 q# u9 ^. s< >String::String(char *s)) W$ H' H: l+ r. l/ x8 j3 {+ q" j0 a
{( N# V8 p H4 V6 x$ \
size=strlen(s)+1;7 r6 R5 w' D) t" N0 \
str=new char[size];
2 E, J V% E$ P/ K( u if(str==0) throw "error";6 _# P# [$ y+ e
strcpy(str,s);
3 @8 g: p+ l5 x; e0 s" o' q3 m" | pre=new int[size];
: }8 l, f3 x' x0 c( [ if(pre==0) throw "error";
! K( a h" X4 D" Q" I, o}</P>! o V* R/ o% z) K- |& }9 H
< >String::String(const String&s)5 i/ s& |: q- ~% k
{
* J/ o1 \, }. j; s! G6 E0 _! ^0 t size=s.size;9 T4 G" p9 g4 }/ G O; _) y
str=new char[size];
I; M6 d/ T- v* M# G if(str==0) throw "error"; t+ V6 n& g- V3 b; }- o& m5 E, C
strcpy(str,s.str);
- d! M- D8 Q" T4 g( B6 A pre=new int[size];
" G( c# ]% Z4 e) F if(pre==0) throw "error";0 Z: b2 O; D( e" H* e8 Q
}</P>
7 R) F' c' o' l: ]< >String& String: perator=(const String& s)9 s# A* g3 p: x7 T+ e h" A
{
9 c0 ]2 c! ^9 O8 ], i if(s.size!=size)! ^4 U* B5 d( G' S0 F. a- `
{; }5 l' u1 A9 r9 N! y1 D! {
delete[] str;" a3 s; n3 [1 ~/ H* l
str=new char[s.size];
$ J. K& S' n+ X# D if(str==0)) x3 [3 |# G$ e) g6 H. @
throw "error";2 s% Z$ W4 s, R
size=s.size;7 ?' i: J8 ?+ D$ E8 V. U
}9 |( ]% T' w8 M9 c+ A
strcpy(str,s.str);
2 e) M: d% y; V* y- p9 @ return *this;
; V1 |$ ]. j) ]1 j6 O* k}</P>
9 s3 ?! ?9 [& _% M< >String& String::change(int *p,int n)//将整型数组改成字符串% O# U8 {& F/ h. [; Z! A
{! y/ s( {: j4 n( ^
int i;
4 ~% z1 n2 P, P delete[] str;. w8 N" `. {2 _6 I4 y& k
str=new char[n+1];
1 [" O& N& ?/ P7 z, u for(i=0;i<n;i++)2 r* J' t3 l% L& X2 b
if(p>=0&&p<=9)
4 ^* q- [# {' ]% { str=p+48;) v! H% B5 D" V, A( G
else
( @! V5 Y2 }$ j" L* h switch(p)
& {3 {* C+ A) |% i, j2 T- ^/ W {
: U( R" R2 ]8 S, M) I6 {$ U1 q3 p case 10: str='A'; break;
- ^4 Q, e) P& p2 X+ r1 l case 11: str='B'; break;4 p2 M* c: a6 j7 E' N- \
case 12: str='C'; break;7 D- C: R G6 X
case 13: str='D'; break;( V$ @: a- m( C- W& P( J: U
case 14: str='E'; break;
4 j2 ]$ [6 H! o/ R case 15: str='F'; break;* W$ P& N3 B, @; Z
}
9 e$ Q' D' u1 p: D& g* C4 d- G9 L; p str[n]='\0';7 z: O8 c9 O: D4 Z
return *this;5 }; r. p& M% Z4 h9 }+ f
}
7 t, y0 n5 o) `0 |8 e+ U `/ L" w& yint String::get()//输入一个字符串
/ p4 ^4 s2 U+ w- [: a+ s{
; p `. n( _3 o- Z& n char tmp[40000];
' m7 S; v+ I- ?& R9 ? in>>tmp;
' X x, g( G8 |+ i6 R delete[] str;) {7 p/ H; l2 L( [: ?: c. d J
size=strlen(tmp)+1;
/ F0 E% _% [9 S str=new char[size];7 L+ j& b* p" g6 c9 {: N
if(str==0)
( g3 ?: t6 D' Q0 E8 j throw "error";1 ~9 E3 v% M9 A* Q9 Q& r
strcpy(str,tmp);
! t3 s7 B( u' b1 A return size-1;
z2 B8 n' J4 v3 W( i}</P>9 L/ b. ~7 ?# _1 u3 V
< >void String::get(int *p)//将字符串改成整型数组
# } T4 n& H0 i$ w* e5 s, o{
6 s, ^& n1 @7 T, Z( }: C- | int i,j;
9 k+ ^' {6 ~; p" H for(i=0,j=size-2;j>=0;i++,j--)" Y5 y- ^$ n p2 k- d
if(str[j]>='0'&&str[j]<='9')2 R1 a: b# s7 w% m2 }
p=str[j]-48;
- s- Q R. a: h# `' n else' ^ \) u$ i: m
{
9 a/ a% D" L" Z: _5 @, i' `9 r$ u: m switch(str[j])& s; C! ?* I7 k9 z; K) s4 x
{
6 j, n, p; u$ K/ f; o$ m8 K case 'A': p=10; break;/ X, h+ u& L) e- ?, a
case 'B': p=11; break;1 W6 @* c+ _7 G {3 {
case 'C': p=12; break;& w1 S' i6 M' r6 Y* O2 |
case 'D': p=13; break;# Y J- D( o, w/ T3 ~- ~
case 'E': p=14; break;- }/ H. A9 S9 q& B6 ^
case 'F': p=15; break;
" t ]/ n# v7 u! Y1 q } m8 b3 ~' Z x3 ~: D t! F" p
}
9 `% g. v0 a# t}</P>" B# m$ I5 A1 J
< >void add(int *p,int &m,int *c,int k)//将一个数同其倒置数相加: j; H# Q, l# L# Y: {4 h
{1 U1 b6 l) G1 \/ Y
int i,j,a=0;# u6 T. X$ V1 Y
for(j=m-1,i=0;j>=0 && i<m;j--,i++)
3 j" D) Y8 M0 W# M4 J$ I; }2 _ {
7 K, N# t4 t: x, w! w c=p+p[j]+a;
7 F) k0 c5 v1 H# |4 u* k$ J a=0;# @/ t+ l# Q" Q
if(c>=k)
# Z$ l; [! R. \* V {% ?1 P# s! H: ~- K" J
a=c/k;6 U3 o7 o! q' D
c=c%k;
8 N5 A' Y. s6 w }
5 O5 D: @ U1 p } . `6 E3 C. |7 |$ U; K
if(a!=0), _, {7 `& u: `+ i* v
{
' H: L$ [$ M6 c' R7 a8 {% p c=a;/ n5 l" R8 x; P" K/ L
m++;
, O" l7 K6 w: b/ l; i }( C9 h3 g8 I" p0 ?* V7 h
}</P> l. H3 Z$ Q$ Z' W) n
< >bool match(int *a,int n)//判断是否为回文数
* u* [1 n+ W( D# L* _8 T{
; S5 x$ p% I: u/ P; d3 w8 G int i,j,h=0;
! s3 I: o" @$ g for(i=0,j=n-1;i<=n/2 && j>=n/2;i++,j--)
' g; t& ~# \$ {1 ~* g, f2 ` {+ K4 g/ U1 J& [# E; C! b
if(a==a[j])9 ~) k/ x1 b0 L q( H* {
continue;
( F* o% U [* R) q" ?0 \ h=1;% Y. }& B+ A7 w; [# p1 d% n+ I
break;. i1 g; z2 D5 [# | C9 D/ E
}
8 V% Z5 {- _$ r, `( j6 Q, ~9 H' g8 } if(h==0)
% p! u$ C& K7 A- G, [) P return true;
5 [ S0 ^* K! g2 m* O else 8 C0 G6 J' C% J. X
return false;
. ~: C) L% @" E' m L! Y}
T/ J7 h" ~' [$ d, C! I; B+ q! A" h) o//clock_t start,finish;* H! x% R' x% f) V% g2 J" ]" ]/ I
int main(), J: B" Y, q& _6 q8 {2 I3 L
{//start=clock();
0 f2 b1 v% ]" N; ?. ^ if(in.fail())2 h$ F$ K# ^0 Q* D8 ~) L4 O
{$ _+ ]& t0 x7 [, O6 w
cout<<"the input.txt is not exist!";
; G2 X: ^; O- _' s$ y exit(1);
1 q6 p0 G, E W8 I- @% P }
( J$ p- w8 e) `: o# m" i String s,s1;4 l0 T" X. l2 m# }: k F+ m
int n,g,k,*a,*c,m,h=0;
) L8 q9 B8 P( P' R' B; V4 n in>>k>>g;
/ Q" j1 C3 |8 _5 B2 e% m- g s.get();$ B% m1 w% x* V7 |) P& n, d
0 Y8 [" P3 T/ M
n=s.length();
6 c1 F: ]2 P0 }" t2 ? m=n+g+1;
9 R5 v5 m- ~% L/ T" V a=new int[m];
' y O% j8 O6 O0 }! b: w$ F3 `% l- L c=new int[m];
5 i, Q3 \ ?. V s.get(a);
' l0 [ M1 z: Q0 ^4 m if(match(a,n))
6 w; {% _) a" w0 J {( Q0 l0 q) Y; e6 n5 T' \% k
out<<0<<endl;
$ ^9 b+ k1 Y$ n; }& \ s.display();
2 G; ?9 d5 J- y1 C! D( t return 1;
) f6 L5 I6 t* _# g+ S# _; m }, ?1 |5 ]- @/ F! f5 w6 `
do
% g$ K3 q9 \! ?8 \( a- g B+ D {; d2 L* f2 H% q" i$ C
if(h%2==0)
2 S L/ f6 P. M( w! S2 e( e add(a,n,c,k);# N5 K8 B) l; M t2 R1 Y
else$ H; a; W# x: l9 V7 M* f6 U. B
add(c,n,a,k);
/ N. w9 i% f$ B# f% ^8 A h++;; K! l/ g# o( g) E$ ~: O$ z0 C/ E4 V$ I
if(h>g): w$ V4 A# R6 ~, e6 D6 X& b* O
break;
; F a% c" B+ f2 H% q6 t5 Z }7 X. [2 @9 i4 T8 S
while(!match(a,n)&&!match(c,n));! I- R: W' \7 A7 Y
if(h>g)
8 H0 B) a% @2 V& U' m: l0 h) T# | out<<"No Solution!"<<endl;
3 h5 e( {* @! x1 w- n/ G else
) o( F& r8 U) D8 S8 B' K {' p" H K, N5 [, q
out<<h<<endl;
8 l8 r8 D% a& F- e; s if(h%2==0)5 H! g7 f$ `. V5 m/ Z: ^
s1.change(a,n);$ I+ M3 h( h; L+ S7 q
else' f* r0 S7 t* }# s
s1.change(c,n);) U) i# G2 J+ R! y/ L
s1.display();
/ a5 w5 s0 l$ _9 a$ j }
1 j% [- b% i; F5 f+ h6 z1 N delete[] a;
: M& W- L6 N5 e! _# a( V delete[] c;
" ?% q: i+ z8 v: t4 o// finish=clock();
_9 Y( s* f2 t% O// cout<<finish-start<<endl;; _$ p2 m. g& R( e
return 1;
8 R R/ o# H7 K% M5 k% D}</P> |
zan
|