apple
世上本无事,庸人自扰之!
C++博客
::
首页
::
新随笔
::
联系
::
聚合
::
管理
::
10 随笔 :: 0 文章 :: 4 评论 :: 0 Trackbacks
<
2008年12月
>
日
一
二
三
四
五
六
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
常用链接
我的随笔
我的评论
我参与的随笔
留言簿
给我留言
查看公开留言
查看私人留言
随笔分类
C++(7)
(rss)
stl learning(2)
(rss)
VC++
(rss)
随笔档案
2008年9月 (5)
2008年8月 (5)
搜索
最新评论
1. re: 简单链栈 执行为什么会有这样有问题呢?
析构函数不对 top==NULL的时候你还delete了
--沈臻豪(foxtail)
2. re: 循环移位
不要用递归啦 用循环做
--沈臻豪(foxtail)
3. re: 简单顺序表
@YG
呵呵,谢谢,更改过来啦,
--明王不动
4. re: 奇怪 Get()函数怎么调用不到呢?
你的length都没有赋值。
--YG
阅读排行榜
1. 简单顺序表(92)
2. 回文数字的判断 (81)
3. 简单链队列(77)
4. 循环移位(74)
5. 简单题::斐波那契数列(59)
评论排行榜
1. 简单顺序表(2)
2. 简单链栈 执行为什么会有这样有问题呢?(1)
3. 循环移位(1)
4. 简单题::斐波那契数列(0)
5. 简单链队列(0)
2008年9月7日
#
简单题::斐波那契数列
计算出1/1 + 2/1 + 3/2 + 5/3 + 8/5 + 13/8 + .....一直到第n项的和,
每项的分子分母是由斐波那契数列中取相邻的两个得到,计算结果保留三位小数
#include
<
iostream
>
#include
<
iomanip
>
using
namespace
std;
int
main()
{
int
n;
double
t;
while
(cin
>>
n)
{
double
a
=
1
;
double
b
=
1
;
double
sum
=
0
;
for
(
int
i
=
1
;i
<=
n;i
++
)
{
sum
+=
a
/
b;
t
=
a;
a
+=
b;
b
=
t;
}
cout
<<
setiosflags(ios::
fixed
)
<<
setprecision(
3
);
cout
<<
sum
<<
endl;
}
return
0
;
}
posted @
2008-09-07 15:29
明王不动 阅读(59) |
评论 (0)
|
编辑
收藏
循环移位
#include
<
iostream
>
#include
<
string
>
using
namespace
std;
const
int
Max
=
100
;
char
*
sreverse(
char
*
s,
int
a,
int
b)
{
char
e;
char
c;
if
(b
>
a)
{
c
=
s[a];
e
=
s[b];
s[a]
=
e;
s[b]
=
c;
a
++
;
b
--
;
sreverse(s,a,b);
}
return
s;
}
int
main()
{
int
i;
char
*
a
=
new
char
[Max];
char
*
p;
cin
>>
a;
cin
>>
i;
if
(i
==
0
) cout
<<
a
<<
endl;
else
if
(i
<
0
)
{
i
=-
i;
sreverse(a,
0
,i
-
1
);
sreverse(a,i,strlen(a)
-
1
);
p
=
sreverse(a,
0
,strlen(a)
-
1
);
cout
<<
p
<<
endl;
}
else
{ sreverse(a,strlen(a)
-
i,strlen(a)
-
1
);
sreverse(a,
0
,strlen(a)
-
i
-
1
);
p
=
sreverse(a,
0
,strlen(a)
-
1
);
cout
<<
p
<<
endl;
}
return
0
;
}
能不能进行优化啊,超时!!!
posted @
2008-09-07 12:54
明王不动 阅读(74) |
评论 (1)
|
编辑
收藏
2008年9月6日
#
回文数字的判断
#include
<
iostream
>
using
namespace
std;
int
Rev(
int
n)
{
char
ch;
if
(n
>
10000
) ch
=
'
a
'
;
else
if
(n
>
1000
) ch
=
'
b
'
;
else
if
(n
>
100
) ch
=
'
c
'
;
else
if
(n
>
10
) ch
=
'
d
'
;
else
if
(n
>
0
) ch
=
'
e
'
;
else
return
1
;
switch
(ch)
{
case
'
a
'
:
if
((n
/
10000
)
==
(n
%
10
)
&&
((n
%
10000
)
/
1000
)
==
((n
%
100
)
/
10
))
return
1
;
else
return
0
;
break
;
case
'
b
'
:
if
((n
/
1000
)
==
(n
%
10
)
&&
((n
%
1000
)
/
100
)
==
((n
%
100
)
/
10
))
return
1
;
else
return
0
;
break
;
case
'
c
'
:
if
((n
/
100
)
==
(n
%
10
))
return
1
;
else
return
0
;
break
;
case
'
d
'
:
if
((n
/
10
)
==
(n
%
10
))
return
1
;
else
return
0
;
break
;
case
'
e
'
:
return
0
;
}
return
0
;
}
int
main()
{ cout
<<
Rev(
654
);
cout
<<
Rev(
12321
);
cout
<<
Rev(
322
);
return
0
;
}
posted @
2008-09-06 09:26
明王不动 阅读(81) |
评论 (0)
|
编辑
收藏
2008年9月5日
#
简单链队列
#include
<
iostream
>
using
namespace
std;
struct
Node
{
int
data;
Node
*
next;
}
;
class
Linkqueue
{
public
:
Linkqueue();
~
Linkqueue();
void
Enqueue(
int
x);
int
Dequeue();
int
Getqueue();
private
:
Node
*
front;
Node
*
rear;
}
;
Linkqueue::Linkqueue()
{
Node
*
s;
s
=
new
Node;
s
->
next
=
NULL;
front
=
rear
=
s;
}
Linkqueue::
~
Linkqueue()
{
Node
*
p;
p
=
new
Node;
while
(front
!=
rear)
{
p
=
front
->
next;
p
->
next
=
front
->
next;
delete front;
front
=
p;
}
}
void
Linkqueue::Enqueue(
int
x)
{
Node
*
s;
s
=
new
Node;
s
->
data
=
x;
s
->
next
=
NULL;
rear
->
next
=
s;
rear
=
s;
}
int
Linkqueue::Dequeue()
{
if
(front
==
rear)
throw
"
error
"
;
Node
*
p;
p
=
new
Node;
p
=
front
->
next;
int
x
=
p
->
data;
front
->
next
=
p
->
next;
if
(p
->
next
=
NULL) rear
=
front;
delete p;
return
x;
}
int
Linkqueue::Getqueue()
{
return
front
->
next
->
data;
}
void
main()
{
Linkqueue q;
q.Enqueue(
2
);
cout
<<
q.Getqueue()
<<
endl;
q.Enqueue(
3
);
q.Enqueue(
4
);
cout
<<
q.Dequeue()
<<
endl;
cout
<<
q.Getqueue();
}
posted @
2008-09-05 23:16
明王不动 阅读(77) |
评论 (0)
|
编辑
收藏
2008年9月1日
#
简单链栈 执行为什么会有这样有问题呢?
#include
<
iostream
>
using
namespace
std;
struct
Node
{
int
data;
Node
*
next;
}
;
class
Linkstack
{
public
:
Linkstack();
~
Linkstack();
void
Push(
int
x);
void
Gettop();
void
Pop();
private
:
Node
*
top;
}
;
Linkstack::Linkstack()
{
top
=
NULL;
}
Linkstack::
~
Linkstack()
{
while
(top)
{ Node
*
p;
p
=
top
->
next;
delete top;
top
=
p;
}
}
void
Linkstack::Push(
int
x)
{
Node
*
s;
s
=
new
Node;
s
->
data
=
x;
s
->
next
=
top;
top
=
s;
}
void
Linkstack::Pop()
{
delete top;
}
void
Linkstack::Gettop()
{
cout
<<
top
->
data;
}
void
main()
{
Linkstack s;
s.Push(
3
);
s.Push(
2
);
s.Pop();
s.Push(
1
);
s.Gettop();
}
为什么会出现这样的情况呢,执行结果是没错的!
posted @
2008-09-01 22:53
明王不动 阅读(41) |
评论 (1)
|
编辑
收藏
2008年8月31日
#
简单顺序栈
#include
<
iostream
>
using
namespace
std;
const
int
MAX
=
100
;
class
Seqstack
{
public
:
int
Gettop();
void
Push(
int
x);
Seqstack();
private
:
int
top;
int
data[MAX];
}
;
Seqstack::Seqstack()
{
top
=-
1
;
}
void
Seqstack::Push(
int
x)
{
cout
<<
"
enter a number:
"
<<
endl;
cin
>>
x;
data[
++
top]
=
x;
cout
<<
top
<<
endl;
}
int
Seqstack::Gettop()
{
int
x;
x
=
data[top];
cout
<<
x;
return
0
;
}
void
main()
{
Seqstack s;
s.Push(
5
);
s.Push(
4
);
s.Gettop();
}
posted @
2008-08-31 11:02
明王不动 阅读(37) |
评论 (0)
|
编辑
收藏
2008年8月30日
#
简单单链表
#include
<
iostream
>
using
namespace
std;
struct
Node
{
int
data;
Node
*
next;
}
;
class
Linklist
{
public
:
Linklist(
int
a[],
int
n);
int
Get(
int
x);
private
:
Node
*
first;
}
;
Linklist::Linklist(
int
a[],
int
n)
{
first
=
new
Node;
first
->
next
=
NULL;
//
初始化空链表
Node
*
s;
for
(
int
i
=
0
;i
<
n;i
++
)
{
s
=
new
Node;
s
->
data
=
a[i];
s
->
next
=
first
->
next;
first
->
next
=
s;
}
}
int
Linklist::Get(
int
x)
{
Node
*
s;
s
=
new
Node;
s
=
first
->
next;
int
j
=
1
;
while
(s
->
data
!=
x)
{ s
=
s
->
next;
j
++
;
}
if
(s)
return
j;
}
void
main()
{
int
a[]
=