题目来源:
C++语言程序设计 郑莉 董渊 何江舟 编著 清华大学出版社
软件:codeblocks
2-28.代码:
#include <iostream>
using namespace std;
int main()
{
cout << "Menu: A(dd) D(elete) S(ort) Q(uit), Select one:" << endl;
char cv;
do
{
cin>>cv;
if(cv=='Q')break;
else
{
switch(cv)
{
case 'A':cout<< " 数据已经增加。 " <<endl;continue;
case 'D':cout<< " 数据已经删除。 " <<endl;continue;
case 'S':cout<< " 数据已经排序。 " <<endl;continue;
}
}
}while(cv!='Q');
return 0;
}
运行结果:
2-29.代码:
for语句实现:
#include <iostream>
#include <math.h>
using namespace std;
bool ccv(int n)//找出质数:for 语句
{
int p=1;
for( int i=2;i<=sqrt(n);i++)
{
if(n%i==0)p=0;
}
return p;
}
int main()
{
int t=100;
cin>>t;
cout << "The prime number range 1 to "<< t <<" is " <<endl;
for(int m=2;m<=t;++m)
{
if(ccv(m))cout<< " " << m <<endl;
}
return 0;
}
while语句实现:
#include <iostream>
#include <math.h>
using namespace std;
bool ccv(int n)//找出质数:while 语句
{
int p=1;
int i=2;
while(i<=sqrt(n))
{
if(n%i==0)p=0;
i++;
}
return p;
}
int main()
{
int t=100;
cin>>t;
cout << "The prime number range 1 to "<< t <<" is " <<endl;
int m=2;
while(m<=t)
{
if(ccv(m))cout<< " " << m <<endl;
++m;
}
return 0;
}
do_while语句实现:
#include <iostream>
#include <math.h>
using namespace std;
bool ccv(int n)//找出质数:do_while 语句
{
int p=1;
int i=2;
do
{
if(n%i==0&&n!=2)p=0;
i++;
}while(i<=sqrt(n));
return p;
}
int main()
{
int t=100;
cin>>t;
cout << "The prime number range 1 to "<< t <<" is " <<endl;
int m=2;
do
{
if(ccv(m))cout<< " " << m <<endl;
m++;
}while(m<=t);
return 0;
}
运行结果:
当然,这三个程序均可以实现求1至任意数(整型取值范围内)之间的质数
2-32.代码:这个是1~10000之间的,因为1~100太好猜了~~~
while语句实现:
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
srand((unsigned)time(nullptr));
int i=rand()%10000;
int m=0;
cout<< " The computer just set a number. "
<<"Now,please guess what the number the computer set."<<endl;
cin>>m;
while(m!=i)
{
if(m<i)cout<<"Oh,your number is smaller."<<endl;
else cout<<"Oh,your number is bigger." <<endl;
cout<<"Please try again."<<endl;
cin>>m;
}
if(m==i)
cout << "Congratulation! You get the number." << endl;
return 0;
}
do_while语句实现:
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
srand((unsigned)time(nullptr));
int i=rand()%10000;
int m;
cout<< " The computer just set a number. "
<<"Now,please guess what the number the computer set."<<endl;
do
{
cin>>m;
if(m<i) cout<<"Oh,your number is smaller.Please try again."<<endl;
else if(m>i)cout<<"Oh,your number is bigger.Please try again." <<endl;
}while(m!=i);
if(m==i)
cout << "Congratulation! You get the number." << endl;
return 0;
}
运行结果:
额。。。我猜的次数有点多
当然,上述程序还可以再优化一下,可以#define N 你想输入的数,这样就便于改动数值。
还有,可以再加几行,使得程序可以反映出输入的次数并配以相应的语句,这里不再赘述。
2-34.代码:这个主要取决于对题目的理解,理解得简单,程序也简单;往复杂里想,就实实在在的一道排列组合题了,还是超难的那种。。。下面这个算是中等吧
附题目:口袋中有红、黄、蓝、白、黑 5 种颜色的球若干。每次从口袋中取出 3 个不同颜色的球,问有多少种取法?
#include <iostream>
#include <iomanip>
using namespace std;
enum color{red,yellow,blue,white,black};
int main()
{
int k=0;
color sdx;
for(int i=red;i<=black;i++)
{
for(int j=red;j<=black;j++)
{
if(i!=j)
{
for(int p=red;p<=black;p++)
{
if(i!=p&&j!=p)
{
k++;
cout<<" Number: "<<k;
for(int cx=0;cx<3;cx++)
{
switch(cx)
{
case 0:sdx=color(i);break;
case 1:sdx=color(j);break;
case 2:sdx=color(p);break;
}
switch(sdx)
{
case 0:cout<<setw(12)<<" red";continue;
case 1:cout<<setw(12)<<" yellow";continue;
case 2:cout<<setw(12)<<" blue";continue;
case 3:cout<<setw(12)<<" white";continue;
case 4:cout<<setw(12)<<" black";continue;
}
}
cout<<endl;
}
}
}
}
}
cout<<" Total: "<<k<<endl;
return 0;
}
注:里面的break和continue其实起一样的效果。
运行结果:
有点多,没有办法全部截屏
差不多就是这样了。
PS:本人学生一枚,初学C++,若有不足之处,还请各位大佬指正。
//不知道为什么,之前上传的图片全都不见了,只好重新截屏。。。