C/C++每日一练(20230401) 移动数组元素、好数对、数组元素的首末位置

简介: C/C++每日一练(20230401) 移动数组元素、好数对、数组元素的首末位置

1. 移动数组中的元素

将一维数组中的元素循环左移 k 个位置

输入描述

第 1 行是一维数组元素的个数 n (数组大小)

第 2 行是一个整数 k , 表示移动的位置

下面 n 行为数组的元素个数

输出描述

输出 n 行,表示移动后的数字

出处:

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

代码:

#include <stdio.h>
#define N 10000
int main()
{
    int k, a[N], b[N], n, t, w, i;
    scanf("%d", &n);
    scanf("%d", &k);
    for (i = 0; i < n; i++)
        scanf("%d", &a[i]);
    for (i = 0; i < k % n; i++)
        b[i] = a[i];
    for (i = 0; i < n; i++)
    {
    if (i < n - k % n)
        a[i] = a[i + k % n];
    else
        a[i] = b[i - n + k % n];
    }
    for (i = 0; i < n; i++)
        printf("%d\n", a[i]);
    return 0;
}

输出:

5↙

2↙

1 2 3 4 5↙

3

4

5

1

2


2. 好数对

已知一个集合A,对A中任意两个不同的元素求和,若求得的和仍在A内,则称其为好数对。

例如,集合A={1 2 3 4},1+2=3,1+3=4,则1,2和1,3 是两个好数对。

编写程序求给定集合中好数对的个数。

注:集合中最多有1000个元素,元素最大不超过10000

示例1:

4↙

1 2 3 4↙

2

示例2:

7↙

2456 3251 654 890 100 754 1234↙

1

其中,“↙”表示输入回车

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

```c++
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int n, i, j, t;
    scanf("%d", &n);
    int *a = (int *)malloc(n * sizeof(int));
    for (i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
    }
    int cout = 0;
    for (i = 0; i < n; i++)
    {
        for (j = i + 1; j < n; j++)
        {
            for (t = 0; t < n; t++)
                ____________;
        }
    }
    printf("%d", cout);
    free(a);
    return 0;
}
```

出处:

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

代码:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int n, i, j, t;
    scanf("%d", &n);
    int *a = (int *)malloc(n * sizeof(int));
    for (i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
    }
    int cout = 0;
    for (i = 0; i < n; i++)
    {
        for (j = i + 1; j < n; j++)
        {
            for (t = 0; t < n; t++)
        if (a[i] + a[j] == a[t])
            cout++;
        }
    }
    printf("%d", cout);
    free(a);
    return 0;
}

输入输出:

7↙

2456 3251 654 890 100 754 1234↙

1


3. 排序数组中查找元素的首末位置

原标题:在排序数组中查找元素的第一个和最后一个位置

给定一个按照升序排列的整数数组 nums,和一个目标值 target。找出给定目标值在数组中的开始位置和结束位置。

如果数组中不存在目标值 target,返回 [-1, -1]

进阶:

  • 你可以设计并实现时间复杂度为 O(log n) 的算法解决此问题吗?

示例 1:

输入:nums = [5,7,7,8,8,10], target = 8

输出:[3,4]

示例 2:

输入:nums = [5,7,7,8,8,10], target = 6

输出:[-1,-1]

示例 3:

输入:nums = [], target = 0

输出:[-1,-1]


提示:

  • 0 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • nums 是一个非递减数组
  • -10^9 <= target <= 10^9

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

```c++
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
    vector<int> searchRange(vector<int> &nums, int target)
    {
        vector<int> res;
        res.push_back(binary_search_begin(nums, target));
        res.push_back(binary_search_end(nums, target));
        return res;
    }
private:
    int binary_search_begin(vector<int> nums, int target)
    {
        int lo = -1;
        int hi = nums.size();
        while (lo + 1 < hi)
        {
            int mid = lo + (hi - lo) / 2;
            if (target > nums[mid])
            {
                lo = mid;
            }
            else
            {
                hi = mid;
            }
        }
        if (hi == nums.size() || nums[hi] != target)
        {
            return -1;
        }
        else
        {
            return hi;
        }
    }
    int binary_search_end(vector<int> nums, int target)
    {
        int lo = -1;
        int hi = nums.size();
        while (lo + 1 < hi)
        {
            int mid = lo + (hi - lo) / 2;
            ______________;
        }
        if (lo == -1 || nums[lo] != target)
        {
            return -1;
        }
        else
        {
            return lo;
        }
    }
};
```

出处:

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

代码:

二分查找,时间复杂度为 O(log n)

#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
  vector<int> searchRange(vector<int> &nums, int target)
  {
    vector<int> res;
    res.push_back(binary_search_begin(nums, target));
    res.push_back(binary_search_end(nums, target));
    return res;
  }
private:
  int binary_search_begin(vector<int> nums, int target)
  {
    int lo = -1;
    int hi = nums.size();
    while (lo + 1 < hi)
    {
      int mid = lo + (hi - lo) / 2;
      if (target > nums[mid])
      {
        lo = mid;
      }
      else
      {
        hi = mid;
      }
    }
    if (hi == nums.size() || nums[hi] != target)
    {
      return -1;
    }
    else
    {
      return hi;
    }
  }
  int binary_search_end(vector<int> nums, int target)
  {
    int lo = -1;
    int hi = nums.size();
    while (lo + 1 < hi)
    {
      int mid = lo + (hi - lo) / 2;
      if (target < nums[mid])
      {
        hi = mid;
      }
      else
      {
        lo = mid;
      }
    }
    if (lo == -1 || nums[lo] != target)
    {
      return -1;
    }
    else
    {
      return lo;
    }
  }
};
int main()
{
  vector<int> res, nums = {5,7,7,8,8,10};
  Solution s;
  res = s.searchRange(nums, 8);
  cout << "[" << res[0] << ",";
  cout << res[1] << "]" << endl;
  return 0;
}

输出:

[3,4]


🌟 每日一练刷题专栏 🌟

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

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

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

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

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


目录
相关文章
|
1天前
|
存储 C++
C++程序数组与指针:深入理解与实践
C++程序数组与指针:深入理解与实践
9 1
|
1天前
|
存储 算法 C++
C++程序一维数组:深入理解与实践
C++程序一维数组:深入理解与实践
9 1
|
1天前
|
存储 C++ 索引
C++程序字符数组:深入理解与实践
C++程序字符数组:深入理解与实践
10 2
|
6天前
|
存储 C++
【C++模板】模板实现通用的数组
【C++模板】模板实现通用的数组
|
6天前
|
存储 人工智能 C++
【重学C++】【指针】详解让人迷茫的指针数组和数组指针
【重学C++】【指针】详解让人迷茫的指针数组和数组指针
34 1
|
1天前
|
C++
【C++基础】类class
【C++基础】类class
9 1
|
1天前
|
安全 程序员 编译器
C++程序中的基类与派生类转换
C++程序中的基类与派生类转换
8 1
|
1天前
|
C++
C++程序中的类成员函数
C++程序中的类成员函数
7 1
|
1天前
|
C++
C++程序中的类封装性与信息隐蔽
C++程序中的类封装性与信息隐蔽
8 1
|
1天前
|
C++
C++程序中的类声明与对象定义
C++程序中的类声明与对象定义
9 1