📄 subject_66237.htm
字号:
<p>
序号:66237 发表者:蓝欣 发表日期:2003-12-23 12:56:28
<br>主题:林建华兄,能进来看看吗?
<br>内容:我想把本区搞的程序设计竞赛中的那个三角形问题用函数重载和友元函数输入输出来编写,可以吗?<BR><BR>希望帮忙!<BR>#include "stdafx.h"<BR>#include <iostream><BR>using namespace std;<BR><BR>const int STACK_SIZE = 100;<BR>const int MAX_SIZE = 1000;<BR>int N; //总层数<BR><BR>typedef struct{<BR> int layer; //结点所在的层(根是第1层)<BR> int index; //该结点是该层的第index个结点<BR> int sum;<BR>}TreeNode;<BR><BR>///////////////////////////////////////////////栈类模板定义<BR>template <class T><BR>class Stack<BR>{<BR> T *base;<BR> T *top;<BR> int size;<BR>public:<BR> Stack()<BR> {<BR> base = new T[STACK_SIZE];<BR> top = base;<BR> size = 0;<BR> }<BR> ~Stack()<BR> {<BR> delete []base;<BR> }<BR> int Push(T e);<BR> int Pop(T &e);<BR> int GetTop(T &e);<BR> int StackEmpty();<BR>};<BR>template <class T><BR>int Stack<T>::Push(T e) //元素e推入栈<BR>{<BR> if (size > STACK_SIZE)<BR> {<BR> exit(0);<BR> }<BR> *top++ = e;<BR> size++;<BR> return 1;<BR>}<BR><BR>template <class T><BR>int Stack<T>::Pop(T &e) //弹出栈顶元素,栈顶元素存入e中<BR>{<BR> if (size == 0)<BR> {<BR> return 0;<BR> }<BR> else<BR> {<BR> e = *--top;<BR> size--;<BR> return 1;<BR> }<BR>}<BR><BR>template <class T><BR>int Stack<T>::GetTop(T &e) //取栈顶元素,存入e中<BR>{<BR> if (size == 0)<BR> {<BR> return 0;<BR> }<BR> else<BR> {<BR> e = *(top - 1);<BR> return 1;<BR> }<BR>}<BR><BR>template <class T><BR>int Stack<T>::StackEmpty() //判断栈是否为空<BR>{<BR> if (size == 0)<BR> {<BR> return 1;<BR> }<BR> else<BR> {<BR> return 0;<BR> }<BR>}<BR>////////////////////////////////////////////////////////////栈类模板定义结束<BR><BR>int Sum1Ton(int n)//求1+2+... ...+n<BR>{<BR> int i,sum = 0;<BR> for (i = 1; i < n + 1; i++)<BR> {<BR> sum += i;<BR> }<BR> return sum;<BR>}<BR>TreeNode LChild(TreeNode x) //返回结点x的“左孩子”<BR>{<BR> TreeNode t;<BR> t.layer = x.layer + 1;<BR> t.index = x.index;<BR> return t;<BR>}<BR><BR>TreeNode RChild(TreeNode x) //返回结点x的“右孩子”<BR>{<BR> TreeNode t;<BR> t.layer = x.layer + 1;<BR> t.index = x.index + 1;<BR> return t;<BR>}<BR> <BR>int Address(TreeNode x) //求x结点在数字三角形的存储数组中的下标<BR>{<BR> if (x.layer > 0)<BR> {<BR> return Sum1Ton(x.layer -1) + x.index;<BR> }<BR> else<BR> {<BR> return -1;<BR> }<BR>}<BR><BR><BR>int main(int argc, char* argv[])<BR>{<BR> int result = 0; //存放最后结果<BR> int temp;<BR> cout << "input total layers:";<BR> cin >> N; //输入总层数<BR><BR> int CurrentSize;<BR> CurrentSize = Sum1Ton(N); //求出元素个数<BR><BR> int Triangle[MAX_SIZE+1]; //用这个数组存放数字三角形,从1 开始,逐层存放<BR> <BR> for (int i = 0; i < MAX_SIZE+1; i++)<BR> { <BR> Triangle[i] = 0;<BR> }<BR> <BR> for (int j = 1; j < CurrentSize + 1; j++) //输入数字三角形的各元素<BR> {<BR> cout << "input elem:[" << i << "]=";<BR> cin >> Triangle[i];<BR> }<BR><BR> TreeNode tn;<BR> tn.layer = 0;<BR> tn.index = 0;<BR> tn.sum = 0;<BR><BR> Stack<TreeNode> S;<BR> S.Push(tn);<BR><BR> while (!S.StackEmpty()) //先序遍历这棵“二叉树”,求出最大值<BR> {<BR> S.Pop(tn);<BR> if (tn.layer == N)<BR> {<BR> if (result < tn.sum)<BR> {<BR> result = tn.sum; //tn.sum存放从根遍历到该结点所走的“某条路径”上数的总和<BR> }<BR> }<BR> else<BR> { <BR> temp = tn.sum;<BR> tn = RChild(tn);<BR> tn.sum = temp + Triangle[Address(tn)];<BR> while (tn.layer < N +1)<BR> {<BR> temp = tn.sum;<BR> S.Push(tn);<BR> if (tn.layer == N)<BR> {<BR> break;<BR> }<BR> tn = LChild(tn); <BR> tn.sum = temp + Triangle[Address(tn)];<BR> }<BR> }<BR> }//while<BR> cout << "The result is:" << result << endl;<BR><BR> return 0;<BR>}<BR>
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:林建华 回复日期:2003-12-23 13:27:11
<br>内容:输入参数的时候由问题<BR> for (int j = 1; j < CurrentSize + 1; j++) //输入数字三角形的各元素<BR> {<BR> cout << "input elem:[" << j << "]=";<BR> cin >> Triangle[j];<BR> }<BR>其他没看出什么问题,那他的测试用例测试了一下也没有问题
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:蓝欣 回复日期:2003-12-23 13:39:02
<br>内容:能不能帮我重新写一个能在TC++3.0下运行调试的啊,这个是在VC下能调试,但在TC++3.0下不能运行啊,要求用到函数重载和友元函数输入输出!<BR><BR>小弟感激不尽!
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:林建华 回复日期:2003-12-23 13:45:02
<br>内容:你用的类和模板,要改成c要改很多东西的,为什么要再tc下运行啊?
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:蓝欣 回复日期:2003-12-23 13:58:58
<br>内容:还是用C++用啊,也要用类的,只是要求在TC++环境下运行调试,我们老师要求我们调试给他看,可是机房里只有TC++环境,没有VC和其他环境啊
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:林建华 回复日期:2003-12-23 14:02:34
<br>内容:tc++啊,我没有用过,不是支持c++的吗,那就应该支持函数重载和友元函数啊
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:蓝欣 回复日期:2003-12-23 14:21:16
<br>内容:是的啊,是支持C++啊,但是它不支持很多库函数啊!<BR><BR>像#include "stdafx.h"这个它就不支持啊
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:林建华 回复日期:2003-12-23 14:37:42
<br>内容:...那是预编译文件,把那句话去掉好了,我编译你的代码的时候就把那句话去掉了
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:蓝欣 回复日期:2003-12-23 14:48:37
<br>内容:是吗?<BR><BR>那我去掉能在TC++下运行调试吗?
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -