锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
]]>
鍏朵腑閫氶厤絎?* 琛ㄧず鍙互鍖歸厤0涓垨澶氫釜浠繪剰鐨勫瓧絎︼紱
鍏朵腑閫氶厤絎?? 琛ㄧず鍙互鍖歸厤0涓垨1涓換鎰忓瓧絎︺?/p>
濡傛灉涓嶆兂瀵歸氶厤鐨勬ā寮忚繘琛岄澶勭悊錛屽彲浠ヤ嬌鐢ㄥ涓嬮掑綊鐨剋ildcard鍖歸厤錛屽彧涓嶈繃榪欑鍖歸厤鏂瑰紡姣旇緝浣庢晥銆傚叾瀹烇紝浣嶅茍琛岀畻娉?bit-parallel)綆楁硶鏄鐞嗛氶厤絎﹀尮閰嶇殑涓縐嶆湁鏁堟妧鏈紝鎴戜滑灝嗗彟鏂囦粙緇嶃?/p>

/**//*
recursive wildcard matching
* : match zero or more characters;
? : match zero or one character.
return value:
1 : match success.
0 : no match.
*/
int wildcard(const char * pat, const char * str)

{
while(*str && *pat) 

{
if(*pat=='?')
{
if(wildcard(pat+1, str)) return 1;
str++;
pat++;
}
else if(*pat=='*')
{
while(*pat=='*' || *pat=='?') pat++;
if(!*pat) return 1;
while(*str)
{
if(wildcard(pat, str)) return 1;
str++;
}
return 0;
}
else
{
if(*pat!=*str) return 0;
str++;
pat++;
}
}
if(*str!=0) return 0;
while(*pat=='*' || *pat=='?') pat++;
return !*pat;
}
璁$畻鏈澶у叕綰︽暟鐨勭畻娉曟槸Euclid綆楁硶錛屽嵆鎵璋撶殑“杈楄漿鐩擱櫎娉?#8221;錛?/p>
int gcd( int a, int b)

{
return (b == 0 ) ? a : gcd(b, a % b);
}
璇ョ畻娉曠殑鏃墮棿澶嶆潅搴︿負O(log(max(a, b)))錛屽洜鑰屾槸鐩稿綋蹇殑銆?/p>
鏍規嵁鍒濈瓑鏁拌鐨勭煡璇嗭細瀵逛簬浠繪剰鐨刟鍜宐錛実cd(a, b) * lcm(a, b)=a*b銆傛嵁姝ゅ緢瀹規槗寰楀埌璁$畻鏈灝忓叕鍊嶆暟鐨勬柟娉曪細
int lcm(int a, int b)

{
return (a*b)/gcd(a, b);
}
鍙﹀涓涓綆條cm鐨勬柟娉曟潵鑷猲ewsmth鐨刾rogramming鐗堬紝瀹冨彧鐢ㄥ姞鍑忔硶鍗沖彲瀹屾垚璁$畻錛?
int LCM(int a, int b)

{
int x=a, y=b;
int u=b, v=a;
while(x!=y)

{
if(x>y)
{
x-=y;
v+=u;
}
else
{
y-=x;
u+=v;
}
}
return (u+v)/2;
}
Create a class FactorialGCD with method factGCD which takes an int a and an int b as parameters and returns the GCD of a! (the factorial of a) and b.
聽
#include聽
<
utility
>
#include聽
<
iostream
>
using
聽
namespace
聽std;
class
聽FactorialGCD
{
public
:
聽
int
聽factGCD(
int
聽a,聽
int
聽b)聽
聽
{
聽聽unsigned聽
int
聽gcd
=
1
;
聽聽
while
(b
>
1
)
聽聽
{
聽聽聽unsigned聽
int
聽p;
聽聽聽
for
(p
=
2
;聽p
*
p
<=
(unsigned聽
int
)b聽
&&
聽b
%
p
!=
0
;聽p
++
)聽;
聽聽聽聽聽聽聽聽聽聽聽聽
if
(b
%
p
!=
0
)聽p
=
b;
聽聽聽
int
聽n
=
0
;
聽聽聽
for
(聽;聽b
%
p
==
0
;聽b
/=
p)聽n
++
;
聽聽聽
int
聽m
=
0
;
聽聽聽
for
(unsigned聽
int
聽j
=
a;聽j
>=
p;聽)聽m
+=
(j
/=
p);
聽聽聽
for
(
int
聽i
=
0
;聽i
<
min(n,聽m);聽i
++
)聽gcd
*=
p;
聽聽}
聽聽
return
聽gcd;
聽}
}
;
int
聽main()
{
聽FactorialGCD聽test;
聽cout聽
<<
聽test.factGCD(
5
,聽
20
)聽
<<
聽endl;
聽cout聽
<<
聽test.factGCD(
7
,聽
5040
)聽
<<
聽endl;
聽cout聽
<<
聽test.factGCD(
0
,聽
2425
)聽
<<
聽endl;
聽cout聽
<<
聽test.factGCD(
667024
,聽
1
)聽
<<
聽endl;
聽cout聽
<<
聽test.factGCD(
4
,聽
40
)聽
<<
聽endl;
聽cout聽
<<
聽test.factGCD(
2097711064
,聽
2147483646
)聽
<<
聽endl;
聽
return
聽
1
;
}
#include聽
<
iostream
>
using
聽
namespace
聽std;
#include聽
<
string
.h
>
void
聽BaseConv1(
char
聽
*
聽s,聽
int
聽a,聽
char
聽
*
聽t,聽
int
聽b)
{
聽聽聽聽
char
聽
*
聽q
=
t;
聽聽聽聽
while
(
*
s)
聽聽聽聽
{
聽聽聽聽聽聽聽聽
int
聽r
=
0
;
聽聽聽聽聽聽聽聽
for
(
char
聽
*
聽p
=
s;聽
*
p;聽p
++
)
聽聽聽聽聽聽聽聽
{
聽聽聽聽聽聽聽聽聽聽聽聽r
=
a
*
r
+*
p
-
'
0
'
;
聽聽聽聽聽聽聽聽聽聽聽聽
*
p
=
r
/
b
+
'
0
'
;
聽聽聽聽聽聽聽聽聽聽聽聽r
%=
b;
聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽
*
q
++=
r
+
'
0
'
;聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽
while
(
*
s
==
'
0
'
)聽s
++
;
聽聽聽聽}
聽聽聽聽
聽聽聽聽
*
q
=
'
\0
'
;
聽聽聽聽
for
(
char
聽
*
聽u
=
t,聽
*
聽v
=
q
-
1
;聽u
<
v;聽u
++
,聽v
--
)
聽聽聽聽
{
聽聽聽聽聽聽聽聽
char
聽c
=*
u;
聽聽聽聽聽聽聽聽
*
u
=*
v;
聽聽聽聽聽聽聽聽
*
v
=
c;
聽聽聽聽}
}
void
聽BaseConv2(
const
聽
char
聽
*
聽s,聽
int
聽a,聽
char
聽
*
聽t,聽
int
聽b)
{
聽聽聽聽
char
聽
*
聽p
=
t;
聽聽聽聽
for
(
*
p
=
'
\0
'
;聽
*
s;聽s
++
)
聽聽聽聽
{
聽聽聽聽聽聽聽聽
int
聽x
=*
s
-
'
0
'
;
聽聽聽聽聽聽聽聽
for
(p
=
t;聽
*
p;聽p
++
)
聽聽聽聽聽聽聽聽
{
聽聽聽聽聽聽聽聽聽聽聽聽x
=
a
*
(
*
p
-
'
0
'
)
+
x;
聽聽聽聽聽聽聽聽聽聽聽聽
*
p
=
x
%
b
+
'
0
'
;
聽聽聽聽聽聽聽聽聽聽聽聽x
/=
b;
聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽
while
(x)
聽聽聽聽聽聽聽聽
{
聽聽聽聽聽聽聽聽聽聽聽聽
*
p
++=
x
%
b
+
'
0
'
;
聽聽聽聽聽聽聽聽聽聽聽聽x
/=
b;
聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽
*
p
=
'
\0
'
;
聽聽聽聽}
聽聽聽聽
for
(
char
聽
*
聽u
=
t,聽
*
聽v
=
p
-
1
;聽u
<
v;聽u
++
,聽v
--
)
聽聽聽聽
{
聽聽聽聽聽聽聽聽
char
聽c
=*
u;
聽聽聽聽聽聽聽聽
*
u
=*
v;
聽聽聽聽聽聽聽聽
*
v
=
c;
聽聽聽聽}
}
int
聽main()
{
聽聽聽聽
char
聽s[]
=
"
987654321014612555422121454541212229852142104542345678901
"
;
聽聽聽聽
char
聽t[
4096
];
聽聽聽聽cout聽
<<
聽
"
s=
"
聽
<<
聽s聽
<<
聽endl;
聽聽聽聽BaseConv2(s,聽
10
,聽t,聽
8
);
聽聽聽聽cout聽
<<
聽
"
t=
"
聽
<<
聽t聽
<<
聽endl;
聽聽聽聽cout聽
<<
聽
"
s=
"
聽
<<
聽s聽
<<
聽endl;
聽聽聽聽BaseConv1(s,聽
10
,聽t,聽
8
);
聽聽聽聽cout聽
<<
聽
"
t=
"
聽
<<
聽t聽
<<
聽endl;
聽聽聽聽
return
聽
1
;
}