循环练习第1关
难度: 1
行数和每行*的个数,由用户输入。
参考:
#include <iostream>
using namespace std;
int main() {
int row = 0;
int list = 0;
cout<<"Please enter the line you want to enter: ";
cin>>row;
cout<<"Please enter the column you want to enter: ";
cin>>list;
for (int i = 1; i <= row; i++) {
for (int j = 1; j <= list; j++) {
cout<<"*";
}
cout<<endl;
}
return 0;
}
循环练习第2关
难度 1.5
参考:
#include <iostream>
using namespace std;
int main() {
int row = 0;
cout<<"Please enter the number of rows: ";
cin>>row;
for (int i = 0; i < row; i++) {
for (int j = 0; j < i+1; j++) {
cout<<"*";
}
cout<<endl;
}
return 0;
}
循环练习第3关
难度: 1.5
参考:
#include <iostream>
using namespace std;
int main() {
int row = 0;
cout<<"Please enter the number of rows: ";
cin>>row;
for (int i = 0; i < row; i++) {
for(int j = 0; j < row - i; j++) {
cout<<"*";
}
cout<<endl;
}
return 0;
}
循环练习第4关
难度系数2.0
参考:
#include <iostream>
using namespace std;
int main() {
int rows;
cout<<"Please enter the number of rows: ";
cin>>rows;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < rows - i - 1; j++) {
cout<<" ";
}
for (int k = 0; k < i * 2 + 1; k++) {
cout<<"*";
}
cout<<endl;
}
return 0;
}
循环练习第5关
难度:2.5
打印乘法口诀表
参考:
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 9; i++) {
for(int j = 1; j <= 9; j++) {
if (j <= i) {
cout<<j<<"*"<<i<<"="<<i*j<<" ";
}
}
cout<<endl;
}
return 0;
}
循环练习第6关
输出所有水仙花数
水仙花数: 3位数字, 各位的立方之和,等于这个数本身.
说明: 严格的说只有3位的整数, 才可能是水仙花数
参考:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int a, b, c = 0;
for (int i = 100; i <= 999; i++) {
a = i % 10;
b = (i / 10) % 10;
c = i / 100;
if (a * a * a + b * b * b + c * c * c == i) {
cout<<i<<endl;
}
}
return 0;
}
**
循环练习第7关
**
输出指定项的斐波那契数列.
1, 1, 2, 3, 5, 8, 13, 21, …
难度: 2.5
参考:
#include <iostream>
#include <iomanip>
#include <unistd.h>
using namespace std;
int main() {
int n = 0;
long long a = 1;
long long b = 1;
long long value;
cout<<"Please enter the number of Fibonacci Numbers: ";
cin>>n;
if (n <= 0) {
cout<<"We want a positive integer greater than zero."<<endl;
sleep(2);
return 1;
}
if (n == 1) {
cout<<"1"<<endl;
sleep(2);
return 0;
}
if (n == 2) {
cout<<"1 1"<<endl;
sleep(2);
return 0;
}
cout<<"1 1 ";
for (int i = 3; i <=n; i++) {
value = a + b;
a = b;
b = value;
cout<<value<<" ";
}
cout<<endl;
return 0;
}
循环练习第8关
输入一个10进制的正整数,把它转换为2进制输出。
6
110
参考:
#include <iostream>
#include <iomanip>
#include <unistd.h>
using namespace std;
int main() {
int ret[32] = {0};
int n;
int i;
cout<<"Please enter a positive integer: ";
cin>>n;
if (n < 0){
cout<<"Please enter a positive integer!!!"<<endl;
sleep(2);
return 1;
}
i = 0;
while (n != 0) {
ret[i] = n % 2;
n = n/2;
i++;
}
for (i--; i >= 0; i--) {
cout<<ret[i];
}
cout<<endl;
return 0;
}
循环练习第9关
输入一个2进制正数,把它转换为10进制输出。
参考:
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
int s = 0;
int p = 1;
cout<<"Please enter a binary number: ";
cin>>str;
for (int i = str.length() - 1; i >= 0; i--) {
int x = str[i] - '0';
s += x*p;
p = p * 2;
}
cout<<"s="<<s<<endl;
return 0;
}
循环练习第10关
输入一个字符串,然后把这个字符串逆转输出
123456789
987654321
参考:
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
int i;
int j;
char tmp;
cout<<"Please enter a string: "<<endl;
cin>>str;
i = 0;
j = str.length() - 1;
while (i < j) {
tmp = str[i];
str[i] = str[j];
str[j] = tmp;
i++;
j--;
}
cout<<str<<endl;
return 0;
}