C++——循环控制强化训练
循环练习第1关
难度: 1
行数和每行*的个数,由用户输入。
#include <iostream> #include <Windows.h> using namespace std; int main(void) { int rows; int cols; cout << "请输入行数: "; cin >> rows; cout << "请输入每行需要打印的列数: "; cin >> cols; for (int i=0; i<rows; i++) { for (int j=0; j<cols; j++){ cout << "*"; } cout << endl; } system("pause"); return 0; } |
循环练习第2关
难度 1.5
#include <iostream> #include <Windows.h> using namespace std; int main(void) { int rows; cout << "请输入行数: "; cin >> rows;
for (int i=0; i<rows; i++) { for (int j=0; j<i+1; j++){ cout << "*"; } cout << endl; } system("pause"); return 0; } |
循环练习第3关
难度: 1.5
#include <iostream> #include <Windows.h> using namespace std; int main(void) { int rows; cout << "请输入行数: "; cin >> rows;
for (int i=0; i<rows; i++) { for (int j=0; j<rows-i; j++){ cout << "*"; } cout << endl; } system("pause"); return 0; } |
循环练习第4关
难度系数2.0
*的个数 空格的个数
第1行: 1 7 (n-1)
第2行: 3 6 (n-2)
第3行: 5 3
第4行: 7 2
第 i 行 2*i-1 n-i
第 n行: 2*n-1
#include <iostream> #include <Windows.h> using namespace std; int main(void) { int rows; cout << "请输入行数: "; cin >> rows;
for (int i=0; i<rows; i++) { for (int j=0; j<rows-i-1; j++) { cout << " "; } for (int j=0; j<2*i+1; j++){ cout << "*"; } cout << endl; } system("pause"); return 0; } |
循环练习第5关
难度:2.5
打印乘法口诀表
#include <iostream> #include <Windows.h> #include<iomanip> using namespace std; int main(void) { for (int i=1; i<=9; i++) { for (int j=1; j<=i; j++) { //setw(2) 是下一个数据的输出宽度为2, //仅对下一个数据的输出有效, 即只有一次效果 // std::left 使数据在自己规定的宽度内左对齐,默认是右对齐, 持续有效 cout << i << "*" << j << "=" ; if (j==1) { cout << setw(1) << std::left << i*j << " "; } else { cout << setw(2) << std::left << i*j << " "; } } cout << endl; } system("pause"); return 0; } |
循环练习第6关
输出所有水仙花数
水仙花数: 3位数字, 各位的立方之和,等于这个数本身.
说明: 严格的说只有3位的整数, 才可能是水仙花数.
#include <iostream> #include <Windows.h> #include<iomanip> using namespace std; int main(void) { int a, b, c;
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; } } system("pause"); return 0; } |
循环练习第7关
难度: 2.5
输出指定项的斐波那契数列.
#include <iostream> #include <Windows.h> #include<iomanip> using namespace std; int main(void) { int n = 0; long long a = 1; long long b = 1; long long value; cout <<"请输入斐波那契数列的个数: " ; cin >> n; if (n <= 0) { cout << "要求是大于0的正数." <<endl; system("pause"); return 1; } if (n == 1) { cout << "1" <<endl; system("pause"); return 0; } if (n== 2) { cout << "1 1" <<endl; system("pause"); return 0; } cout << "1 1 "; for (int i=3; i<=n; i++) { value = a + b; // a 和 b 前进一位 a = b; b = value; cout << value << " "; }
cout << endl; system("pause"); return 0; } |
循环练习第8关
输入一个10进制的正整数,把它转换为2进制输出。
6
110
#include <iostream> #include <Windows.h> #include<iomanip> using namespace std; int main(void) { int ret[32] = {0}; int n; int i; cout << "请输入一个正整数: "; cin >> n; if (n<0) { cout << "需要输入一个正整数!" << endl; system("pause"); return 1; } i = 0; while (n != 0) { ret[i] = n % 2; n = n / 2; i++; } for (i--; i>=0; i--) { cout << ret[i]; } system("pause"); return 0; } |
循环练习第9关
输入一个2进制正数,把它转换为10进制输出。
#include <iostream> #include <Windows.h> #include <string> using namespace std; int main(void) { string str; int s = 0; int p = 1;
cout << "请输入一个二进制数: "; cin >> str; for (int i=str.length()-1; i>=0; i--) { int x = str[i] - '0'; s += x * p; p *= 2; } cout << "s=" << s << endl; system("pause"); return 0; } |
循环练习第10关
输入一个字符串,然后把这个字符串逆转输出
123456789
987654321
#include <iostream> #include <Windows.h> #include <string> using namespace std; int main(void) { string str; int i; int j; char tmp; cout << "请输入一个字符串: " << 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; system("pause"); return 0; } |
循环练习第11关
难度3.0
经典算法题: 千鸡百钱.
1000块钱, 要买100只鸡.
公鸡每只50块
母鸡每只30块
小鸡每3只10块
问:一共有多少种买法?
#include <iostream> #include <Windows.h> #include <string> using namespace std; /* 1000块钱, 要买100只鸡. 公鸡每只50块 母鸡每只30块 小鸡每3只10块 */ int main(void) { int cock_max = 1000/50; int hen_max = 1000/30; for (int i=1; i<=cock_max; i++) { for (int j=1; j<=hen_max; j++) { int k = 100 - i - j; //小鸡的个数 if (k%3 == 0 && i*50 + j*30 + k/3*10 == 1000) { cout <<"公鸡:" << i << " 母鸡:" << j << " 小鸡:" << k << endl; } } } system("pause"); return 0; } |
循环练习第12关
难度2.8
输入一个英文字符串(一句话),统计输入的单词个数.
#include <iostream> #include <Windows.h> #include <string> using namespace std; int main(void) { char line[256]; //'\0'就是0 int i = 0; // 访问字符串(字符数组)的下标 int count = 0; //单词计数 cout << "请输入一句话:"; gets_s(line, sizeof(line)); // 跳过前面的连续空格 while(line[i] == ' ') i++; while (line[i]) { // while(line[i] != '\0') '\0' 就是 0 // 跳过连续的多个非空格组合(就是单词!) while (line[i] && line[i] != ' ') i++; while(line[i] == ' ') i++; count++; } cout << "一共有" << count << "个单词" << endl; system("pause"); return 0; } |
循环练习第13关
难度: 3.0
输入一句话,然后把这个字符串以单词为单位,逆转输出。(腾讯笔试题)
比如将“Alice call Jack”转换为“Jack call Alice”,
实现速度最快,移动最少。
#include <iostream> #include <Windows.h> #include <string> using namespace std; int main(void) { char str[256]; int i = 0; cout << "请输入一句话(英文):"; gets_s(str, sizeof(str)); while (str[i]) { //跳过前面的空格 //该循环结束后,str[i]是下一个的单词的第一个字母 while (str[i] == ' ') { i++; } int j = i;//用来保存左边的值 while (str[j] != ' ' && str[j] != '\0') { j++; } //逆转这个单词 for (int k1 = i, k2 = j - 1; k1 < k2; k1++, k2--) { char tmp = str[k1]; str[k1] = str[k2]; str[k2] = tmp; } i = j; } //逆转这个单词 for (int k1 = 0, k2 =i-1; k1 < k2; k1++, k2--) { char tmp = str[k1]; str[k1] = str[k2]; str[k2] = tmp; } cout << str << endl; system("pause"); return 0; } |
posted on 2022-10-18 09:04 会飞的鱼-blog 阅读(12) 评论(0) 编辑 收藏 举报 来源
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现