/********** End **********/ }
void main() {
int m,s=0; m=252; s=getSum(m); cout<
void writeinfile() {
fstream myfile;
myfile.open(\myfile< 答案:---------------------- int s=0,n; while( m!=0) {n=m;s+=n;m=m/10;} return s; ---------------------- 第19题 (10.0分) 题号:636 /*------------------------------------------------------- 【程序设计】 --------------------------------------------------------- 题目:约瑟夫问题 这是17世纪的法国数学家加斯帕在《数目的游戏问题》中讲的一个故事:15个教徒和15 个 非教徒在深海上遇险,必须将一半的人投入海中,其余的人才能幸免于难,于是想了一个 办法:30个人围成一圆圈,从第一个人开始依次报数,每数到第九个人就将他扔入大海, 如此循环进行直到仅余15个人为止。问怎样排法,才能使每次投入大海的都是非教徒。 -------------------------------------------------- 注意:部分源程序给出如下。请勿改动主函数main和其它 函数中的任何内容,仅在函数的花括号中填入所编 写的若干语句。 -------------------------------------------------------*/ #include void output(int i); void circle(struct node link[] ); struct node { int nextp; /*指向下一个人的指针(下一个人的数组下标)*/ int no_out; /*是否被扔下海的标记。1:没有被扔下海。0:已被扔下海*/ }link[31]; /*30个人,0号元素没有使用*/ int main() { int i; for(i=1;i<=30;i++) /*初始化结构数组*/ { link[i].nextp=i+1; /*指针指向下一个人(数组元素下标)*/ link[i].no_out=1; /*标志置为1,表示人都在船上*/ } link[30].nextp=1; /*第30个人的指针指向第一个人以构成环*/ circle(link); for(i=1;i<=30;i++) /*输出排列结果*/ { cout<<(link[i].no_out?\教徒 \非教徒 \ output(link[i].no_out?1:0);/*0:表示被扔下海的非教徒, 1:表示在船上的教徒*/ } cout< return 0; } void circle(struct node link[] ) { /**********Program**********/ /**********Program**********/ } void output(int i) { ofstream myfile; myfile.open(\ myfile< ---------------------- ---------------------- 第20题 (10.0分) 题号:688 /*------------------------------------------------ 【程序设计】 -------------------------------------------------- 题目:函数compare比较两个长度为N的数组是否相等(即 两数组中下标相同的数组元素均相等)。请完成该 函数。 -------------------------------------------------- 注意:部分源程序给出如下。请勿改动主函数main和其它 函数中的任何内容,仅在注释形式标识Program和 End之间填入所编写的若干语句。 int i,j,k; j=30; /*j:指向已经处理完毕的数组元素,从link[i]指向的人开始计数*/ for(i=0;i<15;i++) /*i:已扔下海的人数计数器*/ { } for(k=0;;) /*k:决定哪个人被扔下海的计数器*/ if(k<9) { } else break; /*计数到15则停止计数*/ link[j].no_out=0; /*将标记置 0,表示该人已被扔下海*/ j=link[j].nextp; /*修改指针,取下一个人*/ k+=link[j].no_out; /*进行计数。因已扔下海的人计标记为0*/ ------------------------------------------------*/ #include using namespace std; void compare(int a[], int b[], int N) { bool equal = true; ofstream outfile; outfile.open(\ /**********Program**********/ /********** End **********/ if(equal) outfile << \ else outfile << \ outfile.close(); } void input(int a[], int b[]) { int index; ifstream input; input.open(\ for(index = 0; index < 200; index++) input >> a[index]; for(index = 0; index < 200; index++) input >> b[index]; } int main() { int a[200], b[200]; input(a, b); compare(a, b, 200); cout <<\ cin.ignore(cin.rdbuf()->in_avail() + 1); return 0; } 答案:for(int index = 0; index < 200; index++) 第21题 (10.0分) 题号:621 /*------------------------------------------------ 【程序设计】 -------------------------------------------------- 题目: 已知某文本文件 in.txt 中存放有若干整数,请将其按照 从小到大的顺序排列后存入另一文件 out.dat。 你需要完成三个函数 1. void read_in (node* & p_head, char* s_file) 负责从某个文件 s_file 中读入,并存在以 p_head 为头部指针的链表中; 2. void sort(node* p_head) 将链表内的元素按照从小到大的顺序排序; 3. void write_out(node* p_head, char* s_file) 将链表中的元素依次写入到 s_file 中 (注意,写入的元素以回车分割)。 { } if(a[index] != b[index]) { } equal = false; break;