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

简介: C/C++每日一练(20230406)

1. 按要求求质数


求10-100之间个位数为7的质数

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

```c++
#include <stdio.h>
int isp(int n)
{
    int i;
    if (n<2)
        return 0;
    for (i=2;i*i<=n;++i)
    {
        _______________;
    }
    return 1;
}
int main()
{
    int i=17;
    while (i<=100)
    {
        if (isp(i))
            printf("%d  ",i);
        i+=10;   
    }
    printf("\n");  
    return 0;
}
```


出处:

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

代码:

#include <stdio.h>
int isp(int n)
{
  int i;
  if (n<2)
    return 0;
  for (i=2;i*i<=n;++i)
  {
    if (n%i==0)
            return 0;
  }
  return 1;
}
int main()
{
  int i=17;
  while (i<=100)
  {
    if (isp(i))
      printf("%d  ",i);
    i+=10;   
  }
  printf("\n");  
  return 0;
}


输出:


2. 两数之和


给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 的那 两个 整数,并返回它们的数组下标。

你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。

你可以按任意顺序返回答案。


示例 1:

输入:nums = [2,7,11,15], target = 9

输出:[0,1]

解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。


示例 2:

输入:nums = [3,2,4], target = 6

输出:[1,2]


示例 3:

输入:nums = [3,3], target = 6

输出:[0,1]


提示:

   2 <= nums.length <= 10^3

   -10^9 <= nums[i] <= 10^9

   -10^9 <= target <= 10^9

   只会存在一个有效答案

出处:

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

代码:

#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
class Solution
{
public:
    vector<int> twoSum(vector<int> &nums, int target)
    {
        std::unordered_map<int, int> hset;
        vector<int> r;
        for (int i = 0; i < nums.size(); ++i)
        {
            int c = target - nums[i];
            auto iter = hset.find(c);
            if (iter != hset.end() && iter->second != i)
            {
                r.push_back(i);
                r.push_back(iter->second);
                return r;
            }
            hset.insert(std::make_pair(nums[i], i));
        }
        return r;
    }
};
void PrintArray(vector<int> arr)
{
    cout << "[";
    for (int i = 0; i < arr.size(); i++) {
        cout << arr[i];
        if (i < arr.size() - 1)
            cout << ",";
    }
    cout << "]" << endl;
}
int main()
{
  Solution s;
  vector<int> nums = {2,7,11,15};
  PrintArray(s.twoSum(nums, 9));
  nums = {3,2,4};
  PrintArray(s.twoSum(nums, 6));
  nums = {3,3};
  PrintArray(s.twoSum(nums, 6));
  return 0;
} 

输出:

[1,0]

[2,1]

[1,0]


3. 颜色分类


给定一个包含红色、白色和蓝色,一共 n 个元素的数组,原地 对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。

此题中,我们使用整数 0、 1 和 2 分别表示红色、白色和蓝色。


示例 1:

输入:nums = [2,0,2,1,1,0]

输出:[0,0,1,1,2,2]


示例 2:

输入:nums = [2,0,1]

输出:[0,1,2]


示例 3:

输入:nums = [0]

输出:[0]


示例 4:

输入:nums = [1]

输出:[1]


提示:

   n == nums.length

   1 <= n <= 300

   nums[i] 为 0、1 或 2


进阶:

   你可以不使用代码库中的排序函数来解决这道题吗?

   你能想出一个仅使用常数空间的一趟扫描算法吗?


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

```c++
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
    void sortColors(vector<int> &nums)
    {
        int i = 0, j = nums.size() - 1;
        while (i < j)
        {
            if (nums[i] == 0)
            {
                i++;
                continue;
            }
            if (nums[j] != 0)
            {
                j--;
                continue;
            }
            swap(nums[i], nums[j]);
        }
        j = nums.size() - 1;
        while (i < j)
        {
            _____________________;
            swap(nums[i], nums[j]);
        }
    }
};
```


出处:

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

代码:

#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
class Solution
{
public:
  void sortColors(vector<int> &nums)
  {
    int i = 0, j = nums.size() - 1;
    while (i < j)
    {
      if (nums[i] == 0)
      {
        i++;
        continue;
      }
      if (nums[j] != 0)
      {
        j--;
        continue;
      }
      swap(nums[i], nums[j]);
    }
    j = nums.size() - 1;
    while (i < j)
    {
      if (nums[i] == 1)
      {
          i++;
          continue;
      }
      if (nums[j] != 1)
      {
          j--;
          continue;
      }
      swap(nums[i], nums[j]);
    }
  }
};
void PrintArray(vector<int> arr)
{
    cout << "[";
    for (int i = 0; i < arr.size(); i++) {
        cout << arr[i];
        if (i < arr.size() - 1)
            cout << ",";
    }
    cout << "]" << endl;
}
int main()
{
  Solution s;
  vector<int> nums = {2,0,2,1,1,0};
  s.sortColors(nums);
  PrintArray(nums);
  return 0;
} 




输出:

[0,0,1,1,2,2]








目录
相关文章
|
4月前
|
Linux 监控 Ubuntu
Linux 终端操作命令(1)
Linux 终端操作命令(1)
89 1
Linux 终端操作命令(1)
|
4月前
|
算法 Java Go
Rust每日一练(Leetday0018) N皇后II、最大子数组和、螺旋矩阵
Rust每日一练(Leetday0018) N皇后II、最大子数组和、螺旋矩阵
40 1
Rust每日一练(Leetday0018) N皇后II、最大子数组和、螺旋矩阵
|
4月前
|
Linux 监控 Shell
Linux 终端命令之文件浏览(4) head, tail
Linux 终端命令之文件浏览(4) head, tail
46 0
Linux 终端命令之文件浏览(4) head, tail
|
4月前
|
Shell Linux 机器学习/深度学习
Linux 终端操作命令(3)内部命令用法
Linux 终端操作命令(3)内部命令用法
72 0
Linux 终端操作命令(3)内部命令用法
|
4月前
|
Python Linux Ubuntu
Linux系统部署Python语言开发运行环境
Linux系统部署Python语言开发运行环境
197 0
Linux系统部署Python语言开发运行环境
|
4月前
|
Go Unix 开发者
Go语言time库,时间和日期相关的操作方法
Go语言time库,时间和日期相关的操作方法
87 0
Go语言time库,时间和日期相关的操作方法
|
4月前
|
C++ 存储 Serverless
力扣C++|一题多解之数学题专场(2)
力扣C++|一题多解之数学题专场(2)
43 0
力扣C++|一题多解之数学题专场(2)
|
4月前
|
Go 机器学习/深度学习 Rust
Golang每日一练(leetDay0119) 反转字符串I\II Reverse String
Golang每日一练(leetDay0119) 反转字符串I\II Reverse String
76 0
Golang每日一练(leetDay0119) 反转字符串I\II Reverse String
|
4月前
|
Java Go C++
Golang每日一练(leetDay0115) 重新安排行程、递增的三元子序列
Golang每日一练(leetDay0115) 重新安排行程、递增的三元子序列
56 0
Golang每日一练(leetDay0115) 重新安排行程、递增的三元子序列
|
4月前
|
Java Go C++
Golang每日一练(leetDay0111) 摆动排序II\I Wiggle Sort
Golang每日一练(leetDay0111) 摆动排序II\I Wiggle Sort
47 0
Golang每日一练(leetDay0111) 摆动排序II\I Wiggle Sort