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;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

会飞的鱼-blog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值