zl程序教程

您现在的位置是:首页 >  后端

当前栏目

C/C++每日一练(20230411)

C++ 每日
2023-09-14 09:01:28 时间

目录

1. 排列序列  🌟🌟🌟

2. 翻转字符串里的单词  🌟🌟

3. 能被13又能被20整除的四位正整数的和  ※

🌟 每日一练刷题专栏 🌟

Golang每日一练 专栏

Python每日一练 专栏

C/C++每日一练 专栏

Java每日一练 专栏


1. 排列序列

给出集合 [1,2,3,...,n],其所有元素共有 n! 种排列。

按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下:

  1. "123"
  2. "132"
  3. "213"
  4. "231"
  5. "312"
  6. "321"

给定 n 和 k,返回第 k 个排列。

示例 1:

输入:n = 3, k = 3
输出:"213"

示例 2:

输入:n = 4, k = 9
输出:"2314"

示例 3:

输入:n = 3, k = 1
输出:"123"

提示:

  • 1 <= n <= 9
  • 1 <= k <= n!

以下程序实现了这一功能,请你填补空白处的内容:

出处:

https://edu.csdn.net/practice/25240972

代码:

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static char *getPermutation(int n, int k)
{
    int i;
    char *result = (char*)malloc(n + 1);
    bool *used = (bool*)malloc(n * sizeof(bool));
    memset(used, false, n * sizeof(bool));
    int total = 1;
    for (i = 1; i <= n; i++)
    {
        total *= i;
    }
    k = k - 1;
    for (i = 0; i < n; i++)
    {
        total /= (n - i);
        int gid = k / total;
        k %= total;
        int x = -1;
        int count = 0;
		while (count <= gid)
		{
		    x = (x + 1) % n;
		    if (!used[x])
		    {
		        count++;
		    }
		}
        used[x] = true;
        result[i] = x + 1 + '0';
    }
    result[n] = '\0';
    return result;
}
int main()
{
    printf("%s\n", getPermutation(3, 3));
    printf("%s\n", getPermutation(4, 9));
    printf("%s\n", getPermutation(3, 1));
    return 0;
}

输出:

213
2314
123


2. 翻转字符串里的单词

给你一个字符串 s ,逐个翻转字符串中的所有 单词 。

单词 是由非空格字符组成的字符串。s 中使用至少一个空格将字符串中的 单词 分隔开。

请你返回一个翻转 s 中单词顺序并用单个空格相连的字符串。

说明:

  • 输入字符串 s 可以在前面、后面或者单词间包含多余的空格。
  • 翻转后单词间应当仅用一个空格分隔。
  • 翻转后的字符串中不应包含额外的空格。

示例 1:

输入:s = "the sky is blue"
输出:"blue is sky the"

示例 2:

输入:s = "  hello world  "
输出:"world hello"
解释:输入字符串可以在前面或者后面包含多余的空格,但是翻转后的字符不能包括。

示例 3:

输入:s = "a good   example"
输出:"example good a"
解释:如果两个单词间有多余的空格,将翻转后单词间的空格减少到只含一个。

示例 4:

输入:s = "  Bob    Loves  Alice   "
输出:"Alice Loves Bob"

示例 5:

输入:s = "Alice does not even like bob"
输出:"bob like even not does Alice"

提示:

  • 1 <= s.length <= 10^4
  • s 包含英文大小写字母、数字和空格 ' '
  • s 中 至少存在一个 单词

进阶:

  • 请尝试使用 O(1) 额外空间复杂度的原地解法。

出处:

https://edu.csdn.net/practice/25240973

代码:

#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
    string reverseWords(string s)
    {
        splitStr(s);
        return joinStr();
    }
private:
    void splitStr(string s)
    {
        if (s == "")
            return;
        int len = s.length();
        string tmp = "";
        for (int i = 0; i < len; i++)
        {
            if (s[i] == ' ' && tmp != "")
            {
                myStack.push(tmp);
                tmp = "";
                continue;
            }
            if (s[i] == ' ')
                continue;
            tmp += s[i];
        }
        if (tmp != "")
            myStack.push(tmp);
        return;
    }
    string joinStr()
    {
        if (myStack.empty())
            return "";
        string s = "";
        while (!myStack.empty())
        {
            s += myStack.top();
            s += " ";
            myStack.pop();
        }
        return s.substr(0, s.length() - 1);
    }
    stack<string> myStack;
};

int main()
{
	Solution sol;
    string s = "the sky is blue";
    cout << sol.reverseWords(s) << endl;
    
    s =  "  hello world  ";
    cout << sol.reverseWords(s) << endl;
    
    s =  "a good   example";
	cout << sol.reverseWords(s) << endl;
	    
    return 0;
}

输出:

blue is sky the
world hello
example good a


3. 能被13又能被20整除的四位正整数的和

计算所有4位正整数中同时能被13和20整除的数的和, 并同时做到如下显示:
①显示这些数:
②显示这些数的个数:
③显示这些数的和。

以下程序实现了这一功能,请你填补空白处内容:

···c++
#include "stdio.h"
int main()
{
    int i = 1000;
    int count = 0;
    int sum = 0;
    printf("所有4位正整数中同时能被13和20整除的数:\n");
    for(i = 1000;i<10000;i++)
    {
        if(                                )
        {
            count++;
            sum = sum + i;
            printf("%d、",i);
        }
    }
    printf("\n这些数一共有%d个\n",count);
    printf("这些数的和是:%d\n",sum);
}
```

出处:

https://edu.csdn.net/practice/25240974

代码:

#include "stdio.h"
int main()
{
    int i = 1000;
    int count = 0;
    int sum = 0;
    printf("所有4位正整数中同时能被13和20整除的数:\n");
    for(i = 1000;i<10000;i++)
    {
        if(i % 13 == 0 && i % 20 == 0)
        {
            count++;
            sum = sum + i;
            printf("%d、",i);
        }
    }
    printf("\n这些数一共有%d个\n",count);
    printf("这些数的和是:%d\n",sum);
}

输出:


🌟 每日一练刷题专栏 🌟

持续,努力奋斗做强刷题搬运工!

👍 点赞,你的认可是我坚持的动力! 

🌟 收藏,你的青睐是我努力的方向! 

评论,你的意见是我进步的财富!  

 主页:https://hannyang.blog.csdn.net/

Golang每日一练 专栏

Python每日一练 专栏

C/C++每日一练 专栏

Java每日一练 专栏