41504199-第三次实验作业2016

  • 格式:doc
  • 大小:524.50 KB
  • 文档页数:20

第三次实验内容:循环结构程序设计 实验内容1 实验课本第四次实验部分 练习题一 程序运行结果

思考题及问题 ① 将程序中的“sum=0; ”语句去掉可以吗? 不可以,否则m为随机值

② 将while循环语句改为do-while循环语句。 程序如下: #include using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) { int n,sum; n=1; sum=0; do { sum=sum+n; n=n+2; } while (n<=99); cout<<"sum="

② 方法二:用辗转相除法求最大公约数。

思考题及问题 ① 将 if (m < n) { r=m; m=n; n=r; } 去掉,分别按m的值小于n的值情况输入数据,程序运行结果如下:

② 分别输入m或n的值为负数,程序运行结果如下: ③ 为什么在用辗转相除法求最大公约数时,输出结果是m,而不是r。 这里的r为m和n的余数,最后的计算结果为0;m为最后一次的计算的约数,所以最后输出m;

④ 试将辗转相除法中的do-while循环语句,改为while循环语句实现。程序如下: #include #include using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) { int x,y; int m,n,r,g; cout<<"输入2个数x,y:"; cin>>x>>y; m=abs(x); n=abs(y); if(m{r=m; m=n; n=r; } while (r!=0) { r=m%n; m=n; n=r; } cout<<"辗转相除法求最大公约数:"<

g=abs(x)*abs(y)/m; cout<<"最小公倍数:"

程序运行结果 思考题及问题 ① 更改初值,程序运行结果: ② 如果输出图形向右平移25个字符位置,程序应该如何实现?运行结果如下: ③ 如果需将输出的两个图形并排打印,程序应如何实现?以输出图①和图②为例,应输出以下图形,程序代码如下:

#include #include using namespace std; int main(int argc, char** argv) { int i,j,k; for(i=1;i<=9;i++) { for(j=1;j<=i;j++) cout<<" "; for(j=19-2*i;j>0;j--) cout<<"*"; for(k=-4;k<=i;k++) cout<<" "; for(k=10-i;k>0;k--) cout<<"*"; cout<<"\n"; } cout<<"\n";

for(i=1;i<=9;i++) { for(j=10-i;j>0;j--) cout<<" "; for(j=1;j<=i;j++) cout<<2*j-1; if(i<=5) {for(j=15-i;j>0;j--) cout<<" "; for(j=1;j<=2*i-1;j++) cout<<(char)('A'+i-1); cout<<"\n"; } else { cout<<" "; for(j=9-2*(i-5);j>0;j--) cout<<(char)('E'-i+5); cout<<'\n'; } } cout<<"\n";

return 0; }

程序设计自测练习 1.自测练习一 编写一个程序,统计输出各位数之和为9,且能被5整除的五位数的个数。 #include #include using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) { int i,s,j,p; for(i=10000,s;i<100000;i+=5) { for(j=1,p=10000,s=0;j<=4;j++) { s=s+(i/p%10); p/=10; } if(s==9) cout<} return 0; } 提示:  遍历所有的五位数,对每一个五位数判定是否符合“输出各位数之和为9,且能被

5整除”。  设n为一个五位数,则其个位数为n%10,十位数为n/10%10,百位数为

n/100%10……

2.自测练习二 编写一个程序,求s=1+(1+2)+(1+2+3)+„+(1+2+3+„+n)的值。

提示:  思路一:用双重循环,外层循环重复n次,内层循环求(1+2+3+…+i),将结果累加到s中。  思路二:用单层循环,通过累加变量p,每次循环都将p累加一个i,则

#include #include using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) { int i,n,s,p,j; cout<<"请输入一个整数"; cin>>n; for(i=1,s=0,p;i<=n;i++) { for(j=1,s=0;j<=i;j++) { p=p+j; } s+=p; } cout<<"sum="

4.自测练习四 编写一个程序,求出1~1000之间的所有完全数。所谓完全数是指:该数的各因子之和正好等于该数本身,如6=1+2+3。 提示:  遍历1~1000之间的所有整数,对每一个整数i测试其是否为完全数。  对整数i而言,若i%j=0,则j为i 的因子。对从1~sqrt(i)的每一个j进行测试其是否为i的因子。

#include #include using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) { int i,n,s,p,j; for(i=4,s=0,p;i<=8;i+=2) { for(j=1,p=1;j<=i;j++) { p=p*j; } s+=p; } cout<<"sum="

)12()12()2(75653431222222nnn,要求:精度为10-5,并输出n的大小

 采用while循环,若前后两次计算的π的近似值的绝对值小于10-5,则结束循环。

 求绝对值用fabs函数(需include )

#include #include using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) { int i,n,p,j; for(i=1,p;i<=1000;i++) { for(j=1,p=0;j{ if(i%j==0) p=p+j ; } if(p==i) cout<} return 0; }