2-28
(1)用if else 语句
#include<iostream> using namespace std; int main(){ char alphabet; while (true) { cout << "Menu : A(dd) D(elete) S(ort) Q(uit) , Select one:" << endl; cin >> alphabet ; if(alphabet=='A') { cout << "Data has added" << endl; } else if(alphabet=='D') { cout << "Data has deleted" << endl; } else if(alphabet=='S') { cout << "Data has sorted" << endl; } else if(alphabet=='Q') break; else cout << "import error,please import another one" << endl; } return 0; }
这里用while(true)多组输入,在题目的基础上还多加了一个输入错误的提示。代码运行结果如下:
(2)用switch
switch语句相对于if语句简便了很多
#include<iostream> using namespace std; int main(){ char alphabet; while(true) { cout << "Menu: A(dd) D(elete) S(ort) Q(uit),Select one :"<<endl; cin >> alphabet; if(alphabet=='Q') break; switch(alphabet){ case 'A': cout << "Data has been added" << endl; break; case 'D': cout << "Data has been deleted" << endl; break; case 'S': cout << "Data has been sorted" << endl; break; default : cout << "import error,please import another one" <<endl; } } return 0; }
和第一问没有多大区别,运行时间比上一个要快,运行结果如下:
2-29 用穷举法找出1-100的质数
(1)用while
思路是i%j(j<i),如果i=j,就是质数
代码如下:
#include<iostream> using namespace std; int main(){ int i=2; while(i<=100){ int j=2; while(j<i){ if(i%j==0) break; j++; } if(j==i) cout << i << endl; i++; } return 0; }
运行结果如下:
(2)用do while
由于排列的不够美观,所以这次把结果的排列优化了一遍,代码如下:
#include<iostream> #include<iomanip> using namespace std; int main(){ int i=2; int a=0; while(i<=100){ int j=2; while(j<i){ if(i%j==0) break; j++; } if(j==i) { a++; cout << setw(3) << i; if(a%5==0) { cout << endl; } } i++; } return 0; }
运行结果如下:
(3)用for循环:
代码如下:
#include<iostream> #include<iomanip> using namespace std; int main(){ int i; int a=0; for( i=2; i<=100; i++ ) { int j=2; for( ; j<i; j++) { if(i%j==0) break; } if(i==j) { a++; cout << setw(3) << i; if(a%5==0) cout << endl; } } return 0; }
运行结果如下:
从结果来看,3个结果for循环用的时间最少。
2-32 猜数字
(1)用while
#include<iostream> using namespace std; const int GUESSNUMBER=28; int main(){ cout << " Please guess a number from 1 to 100 :" << endl; int num; while(true){ cin >> num; if(num==GUESSNUMBER) { cout << "Congratulations!You are right!" <<endl; break; } if(num>GUESSNUMBER) { cout << "Bigger than num" << endl; } if(num<GUESSNUMBER) { cout << "smaller than num" << endl; } } return 0; }
第一次是定义了一个数字
运行结果如下:
(2)第二次我运用了rand函数,在上一题的基础上优化了一下:
#include<iostream> #include<cstdlib> using namespace std; int main(){ cout << " Please guess a number from 1 to 100 :" << endl; int num; int GUESSNUMBER; GUESSNUMBER = 1+(rand()%100); while(true){ cin >> num; if(num==GUESSNUMBER) { cout << "Congratulations!You are right!" <<endl; break; } if(num>GUESSNUMBER) { cout << "Bigger than num" << endl; } if(num<GUESSNUMBER) { cout << "smaller than num" << endl; } } return 0; }
运行结果如下:
2-34
(1)不考虑顺序。我是运用了for循环。如果前面比后面的大,就break,代码如下:
#include<iostream> using namespace std; int main(){ int total=0; enum Colour { red=1 , yellow , blue , white , black }; int i,j,k; for( i=1; i<=3; i++) { for(j=2;j<=4;j++) { if(i>=j) continue; for(k=3;k<=5;k++) { if(j>=k) continue; total++; } } } cout << "total = " <<total << endl; return 0; }
运行结果如下:
(2)我考虑了顺序,也是用for循环
代码如下:
#include<iostream> #include<cstring> using namespace std; int main(){ enum Colour { red=0 , yellow , blue , white , black }; char *s[]={ "red", "yellow" , "blue" , "white" , "black"}; int i,j,k; int total=0; for( i=0; i<=4; i++) { for(j=0;j<=4;j++) { if(i==j) continue; for( k=0; k<=4; k++) { if(j==k||i==k) continue; total++; cout <<s[i]<<' '<<s[j]<<' '<<s[k]<<endl; } } } cout << total << endl; return 0; }
运行结果如下: