循环练习大闯关

第1关

题目:用户输入行数和列数,在控制台打印出对应行数和列数的*

#include <iostream>

using namespace std;

// 用户输入行数和列数,在控制台打印出对应行数和列数的*
int main()
{
    int rows = 0;
    int cols = 0;

    cout << "请输入行数:";
    cin >> rows;
    cout << "请输入列数:";
    cin >> cols;

    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            cout << "*";
        }
        cout << endl;
    }

    return 0;
}

image

第2关

#include <iostream>

using namespace std;

/*
输入行数:5
在控制台输出:
*
**
***
****
*****

*/

int main()
{
    int rows = 0;
    
    cout << "请输入行号:";
    cin >> rows;
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < i + 1; j++)
        {
            cout << "*";
        }
        cout << endl;
    }

    return 0;
}

image

第3关

#include <iostream>

using namespace std;
/*
输入行数:5
*****
****
***
**
* 

*/
int main()
{
    int rows = 0;

    cout << "请输入行号:";
    cin >> rows;
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < rows - i; j++)
        {
            cout << "*";
        }
        cout << endl;
    }

    return 0;
}

image

第4关

#include <iostream>

using namespace std;

/*
输入行号:5
    *
   ***
  *****
 *******
*********

*/
int main()
{
    int rows = 0;

    cout << "请输入行号:";
    cin >> rows;
    for (int i = 1; i <= rows; i++)
    {
        // 打印空格
        for (int j = 0; j < rows - i; j++)
        {
            cout << " ";
        }
        // 打印*
        for (int j = 0; j < 2 * i - 1; j++)
        {
            cout << "*";
        }

        cout << endl;
    }

    return 0;
}

image

第5关

题目:打印九九乘法表

#include <iostream>
#include <iomanip> 
using namespace std;

// 打印九九乘法表
int main()
{
    for (int i = 1; i <= 9; i++)
    {
        for (int j = 1; j <= i; j++)
        {
            if (j == 1)
            {
                cout << j << "x" << i << "=" << i * j << " ";
            }
            else
            {
                cout << j << "x" << i << "=" << setw(2) << left << i * j << " ";
            }

        }
        cout << endl;
    }

    return 0;
}

image

第6关

输出所有水仙花数
水仙花数: 3位数字, 各位的立方之和,等于这个数本身.
说明: 严格的说只有3位的整数, 才可能是水仙花数.

#include <iostream>

using namespace std;

// 输出所有水仙花数
int main()
{
    int a, b, c;  

    for (int i = 100; i <= 999; i++)
    {
        a = i % 10;         //个位
        b = i / 10 % 10;    //十位
        c = i / 100 % 10;   //百位
        if (a * a * a + b * b * b + c * c * c == i)
        {
            cout << i << endl;
        }
    }

    return 0;
}

image

第7关

题目:输出指定个数的斐波那契数列

#include <iostream>

using namespace std;
/*
输出指定项的斐波那契数列.
1, 1, 2, 3, 5, 8, 13, 21, ..
*/
int main()
{
    int count = 0;

    cout << "请输入斐波那契数列的个数:";
    cin >> count;
    if (count <= 0)
    {
        return 1;
    }
    if (count == 1)
    {
        cout << "1" << endl;
        return 0;
    }
    if (count == 2)
    {
        cout << "1 1" << endl;
        return 0;
    }

    cout << "1 1 ";
    int a = 1;
    int b = 1;
    int c = 0;
    for (int i = 2; i < count; i++)
    {
        c = a + b;
        a = b; 
        b = c;
        cout << c << " ";
    }
    cout << endl;

    return 0;
}

image

第8关

题目:输入一个10进制的正整数,把它转换为2进制输出。

#include <iostream>
#include <string.h>
using namespace std;

// 输入一个10进制的正整数,把它转换为2进制输出
int main()
{
    int num = 0;
    int ret[32] = { 0 };

    cout << "请输入一个数:";
    cin >> num;

    int index = 0;
    while (num)
    {
        ret[index] = num % 2;
        num /= 2;
        index++;
    }

    for (index--; index >= 0; index--)
    {
        cout << ret[index];
    }
    cout << endl;

    return 0;
}

image

第9关

题目:输入一个2进制正数,把它转换为10进制输出

#include <iostream>
#include <string>
using namespace std;

// 输入一个2进制正数,把它转换为10进制输出
int main()
{
    string str;   // 用来存放二进制数
    int p = 1;    // 用来计算二进制每一位应乘的系数  
    int num = 0;  // 用来存放转换后的十进制数

    // 二进制数不能直接输入,这里用字符串进行接收
    cout << "请输入一个二进制正数:";
    cin >> str;

    for (int i = str.length() - 1; i >= 0; i--)
    {
        str[i] = str[i] - '0';   // 把字符'1','2','3'...转换成对应的数字
        num += str[i] * p;
        p *= 2;
    }
    cout << "十进制:" << num << endl;
    cout << "十六进制:0x" << hex << num << endl;

    return 0;
}

image

第10关

题目:输入一个字符串,然后把这个字符串逆转输出

#include <iostream>
#include <string>
using namespace std;

// 输入一个字符串,然后把这个字符串逆转输出
int main()
{
    string str;
    cout << "请输入需要逆转的字符串:";
    cin >> str;

    int i = 0;
    int j = str.length() - 1;
    char tmp = 0;
    while (i < j)
    {
        tmp = str[i];
        str[i] = str[j];
        str[j] = tmp;
        i++;
        j--;
    }
    cout << str << endl;

    return 0;
}

image

第11关

#include <iostream>

using namespace std;

/*
经典算法题: 千鸡百钱.
1000块钱, 要买100只鸡.
公鸡每只50块
母鸡每只30块
小鸡每3只10块
问:一共有多少种买法?
*/
int main()
{
    for (int i = 0; i <= 20; i++)     // 公鸡
    {
        for (int j = 0; j <= 33; j++) // 母鸡
        {
            int k = 100 - i - j;      // 小鸡
            if ((i * 50 + j * 30 + k / 3 * 10 == 1000) && (k % 3 == 0))
            {
                cout << "公鸡:" << i << "  母鸡:" << j << "  小鸡:" << k << endl;
            }
        }
    }

    return 0;
}

image

第12关

题目:输入一个英文字符串(一句话),统计输入的单词个数.

#include <iostream>
#include <string>
using namespace std;

// 输入一个英文字符串(一句话),统计输入的单词个数.
int main()
{
    string str;
    int index = 0;   // 字符串下标
    int count = 0;   // 单词的数量
    cout << "请输入一句英文:";
    getline(cin, str);

    // 跳过前面连续的空格
    while (str[index] == ' ')
    {
        index++;
    }

    while (str[index])
    {
        // 跳过单词
        while (str[index] != ' '&& str[index]) // 这里还要判断是不是文件结束符,如果是文件结束符没有进行判断的话,下标就会越界
        {
            index++;
        }
        // 跳过空格
        while (str[index] == ' ')
        {
            index++;
        }
        count++;
    }

    cout << "单词数量:" << count << endl;

    return 0;
}

image

第13关

#include <iostream>
#include <string>
using namespace std;

/*
输入一句话,然后把这个字符串以单词为单位,逆转输出。(腾讯笔试题)
比如将“Alice call Jack”转换为“Jack call Alice”
*/
int main()
{
    string str; 
    int index = 0;  // 下标
    cout << "请输入需要逆转的句子:";
    getline(cin, str);

    // 跳过空格
    while (str[index] == ' ')
    {
        index++;
    }

    // 先逆转每一个单词
    int k1 = 0;
    int k2 = 0;
    while (str[index])
    {
        // 跳过单词
        k1 = index;
        while (str[index] && str[index] != ' ')
        {
            index++;
        }
        // 逆转单词
        k2 = index - 1;
        while (k1 < k2)
        {
            char tmp = str[k1];
            str[k1] = str[k2];
            str[k2] = tmp;
            k1++;
            k2--;
        }
        // 跳过空格
        while (str[index] == ' ')
        {
            index++;
        }
    }

    // 逆转句子
    k1 = 0;
    k2 = str.length() - 1;
    while (k1 < k2)
    {
        char tmp = str[k1];
        str[k1] = str[k2];
        str[k2] = tmp;
        k1++;
        k2--;
    }

    cout << str << endl;

    return 0;
}

image

posted @ 2022-04-10 10:35  荒年、  阅读(25)  评论(0编辑  收藏  举报