C++——循环控制强化训练

简介: C++——循环控制强化训练

循环练习第1关

难度: 1


fe299dbb4ff6409ca80906cb27e518fb.png

行数和每行*的个数,由用户输入。

#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


f821ed8110f54665b63751fbc3527bba.png

#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


8bdcf2d56b6e447a9f5ac9cc298cc6e5.png


#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        

9f14d1899a7e4c56b68ea30c78eebb60.png


#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

打印乘法口诀表

f07dc32e57014b06ae0df7028108c8a1.png


#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进制输出。

56c236a2171d4e30a0a8fb3a282ef784.png

#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;
}


相关文章
|
21天前
|
设计模式 测试技术 编译器
C++项目中打破循环依赖的锁链:实用方法大全(一)
C++项目中打破循环依赖的锁链:实用方法大全
135 0
|
21天前
|
机器学习/深度学习 C++
C/C++基础知识——数组、循环
C/C++基础知识——数组、循环
51 0
C/C++基础知识——数组、循环
|
21天前
|
缓存 编译器 数据处理
【C/C++ 性能优化】循环展开在C++中的艺术:提升性能的策略与实践
【C/C++ 性能优化】循环展开在C++中的艺术:提升性能的策略与实践
80 0
|
9天前
|
算法 C++
c++循环
c++循环
14 0
|
14天前
|
C语言 C++
【C语言/C++】牛客网刷题训练-12
【C语言/C++】牛客网刷题训练-12
|
15天前
|
C++
C++ 循环
C++ 循环
20 0
|
21天前
|
安全 编译器 程序员
【C++入门】内联函数、auto与基于范围的for循环
【C++入门】内联函数、auto与基于范围的for循环
|
21天前
|
C++ Python
C++教学——从入门到精通 10.循环
学习编程建议先Python后C++,以避免C++思维影响。课程涵盖for、while和do while循环。for循环示例:`for(int i=0;i&lt;n;i++)`,用于计算114514天后的金币总数(1145140个)。死循环通过`for(int i=0;;i++)`实现,用`break`退出。while循环格式`while(条件)`,同样可解决金币问题。do while循环特点是先执行后判断,结构为`do{...}while(条件)`。
25 2
|
21天前
|
C++
C++ While 和 For 循环:流程控制全解析
本文介绍了C++中的`switch`语句和循环结构。`switch`语句根据表达式的值执行匹配的代码块,可以使用`break`终止执行并跳出`switch`。`default`关键字用于处理没有匹配`case`的情况。接着,文章讲述了三种类型的循环:`while`循环在条件满足时执行代码,`do/while`至少执行一次代码再检查条件,`for`循环适用于已知循环次数的情况。`for`循环包含初始化、条件和递增三个部分。此外,还提到了嵌套循环和C++11引入的`foreach`循环,用于遍历数组元素。最后,鼓励读者关注微信公众号`Let us Coding`获取更多内容。
27 0
|
21天前
|
设计模式 敏捷开发 持续交付
C++项目中打破循环依赖的锁链:实用方法大全(三)
C++项目中打破循环依赖的锁链:实用方法大全
74 0