码迷,mamicode.com
首页 > 其他好文 > 详细

实验1

时间:2019-03-18 22:29:54      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:using   switch   size   pac   +=   素数   nbsp   select   tor   

2-28

#include<iostream>
using namespace std;
int main(){
while(1){
cout<<"Menu: A(dd) D(elete) S(ort) Q(uit), Select one:";
char a;
cin>>a;
if(a==‘A‘){
cout<<"数据已增加"<<endl;
continue;
}
if(a==‘D‘){
cout<<"数据已删除"<<endl;
continue;
}
if(a==‘S‘){
cout<<"数据已排序"<<endl;
continue;
}
if(a==‘Q‘){
cout<<"已退出程序"<<endl;
break;
}
}

 

 

 

#include<iostream>
using namespace std;
int main()
{
char n;
cout<<"Menu: A(dd) D(elete) S(ort) Q(uit),Select one:"<<endl;
cin>>n;
while(n!=‘Q‘)
{
switch(n)
{
case ‘A‘:
{
cout<<"数据已经增加"<<endl;break;
}
case ‘D‘:
{
cout<<"数据已经删除"<<endl;break;
}
case ‘s‘:
{
cout<<"数据已经排序"<<endl;break;
}
}
cin>>n;
}
return 0;
}
}技术图片

 

2-29

用for循环

#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> nPrime;
nPrime.push_back(2);
bool prime = false;
for (int i = 3; i < 100; i += 2) {
prime = true;
for (int j = 0; j < nPrime.size(); j++)
if (i % nPrime[j] == 0) {
prime = false;
break;
}
if (prime)
nPrime.push_back(i);
}
cout << "0~100的素数 :\n";
for (int i = 0; i < nPrime.size(); i++)
cout << nPrime[i] <<" ";
return 0;
}

 

(while)
#include <iostream>
using namespace std;
int main () {
inti,j,n,m;
i=2;
while(i<101)
{
m=1,n=i/2;j=2;
while(j<=n)
{
if(i%j==0)
{
m=0;
break;
}
j++;
}
if(m)
cout<<i<<" ";
i++;
}
}

(.do....while)
#include<iostream.h>
void main()
{
inti,j,n,m;
i=2;
do
{
m=1;
n=i/2;
j=2;
do
{
if(i%j==0)
{
m=0;
break;
}
j++;
}
while(j<=n);
if(m)
cout<<i<<" ";
i++;
}
while(i<101);
}

技术图片

 

2-32

1#include<iostream>
using namespace std;
int main(){
int number;
cout<<"输入要猜测的数字:";
cin>>number;
int gnumber;
cout<<"输入你猜测的数字:";
cin>>gnumber;
int xnumber;
do{
if(gnumber>number){
cout<<"你猜的偏大了!重新输入:"<<endl;
cin>>xnumber;
gnumber=xnumber;
}
if(gnumber<number){
cout<<"你猜的偏小了!重新输入:"<<endl;
cin>>xnumber;
gnumber=xnumber;
}
}while(gnumber!=number);
cout<<"你猜对了!";

 

2

#include<iostream>
using namespace std;
int main(){
int number;
cout<<"输入要猜测的数字:";
cin>>number;
int gnumber;
cout<<"输入你猜测的数字:";
cin>>gnumber;
int xnumber;
while(gnumber!=number){
while(gnumber>number){
cout<<"你猜的偏大了!重新输入:"<<endl;
cin>>xnumber;
gnumber=xnumber;
}
while(gnumber<number){
cout<<"你猜的偏小了!重新输入:"<<endl;
cin>>xnumber;
gnumber=xnumber;
}
}
cout<<"你猜对了!";
}

}技术图片

 

2-34

#include<iostream>
using namespace std;
int main(){
cout<<"输入有几种颜色:";
int tnumber=0, number=0, i=0, s=0, x=0, k=0;
cin>>tnumber;
cout<<"输入取几种颜色:";
cin>>number;
int sm=tnumber;
int st=number;
while(st>1){
sm=sm*(tnumber-1);
tnumber--;
st--;
}
int xm=1;
for(i=1;i<number;i++){
xm=xm*(i+1);
}
k=sm/xm;
cout<<"有"<<k<<"种取法!";
} 技术图片

 

实验1

标签:using   switch   size   pac   +=   素数   nbsp   select   tor   

原文地址:https://www.cnblogs.com/jzgjzg/p/10555540.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!