蓝桥杯官网 试题 PREV-106 历届真题 修改数组【第十届】【省赛】【研究生组】【C++】【C】【Java】【Python】四种解法

简介: 蓝桥杯官网 试题 PREV-106 历届真题 修改数组【第十届】【省赛】【研究生组】【C++】【C】【Java】【Python】四种解法

为帮助大家能在6月18日的比赛中有一个更好的成绩,我会将蓝桥杯官网上的历届决赛题目的四类语言题解都发出来。希望能对大家的成绩有所帮助。



今年的最大目标就是能为【一亿技术人】创造更高的价值。


资源限制


内存限制:256.0MB C/C++时间限制:1.0s Java时间限制:3.0s Python时间限制:5.0s


image.png


C++

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int Max = 1000010;
int f[Max];
int N;
int getf(int n)
{
    if(f[n] == 0)
        return n;
    return  f[n] = getf(f[n]+1);
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> N;
    int d;
    for(int i = 1; i <= N; ++i)
    {
        cin >> d;
        int fd = getf(d);
        cout << fd << " ";
        f[d] = fd;
        f[fd] = fd;  // 这句不能漏掉,否则不能跳过已经使用过的数字
    }
    return 0;
}

C

#include <stdio.h> 
const int N = 1000010;
int p[1000001]={0};
//查找祖宗节点+路径压缩
int find(int x )
{
    if(p[x] != x) p[x] = find(p[x]);
    return p[x];
}
int main()
{
    int n;
    int i;
    scanf("%d",&n);
    for( i = 0; i < N; i++)
        p[i] = i;
    for( i = 0; i < n; i++)
    {
        int x;
        scanf("%d",&x);
        x = find(x);
        printf("%d ",x);
        p[x] = x+1;
    }
    return 0;
}

Java

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Main {
  static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
  static int n;
  static int[] a = new int[100010];
  static int[] p = new int[1000010];
  public static void main(String[] args) throws IOException {
  n = Integer.parseInt(bf.readLine());
  String[] msg = bf.readLine().split(" ");
  StringBuilder sb = new StringBuilder();
  for(int i = 1; i <= n; i++)  a[i] = Integer.parseInt(msg[i-1]);
  for(int i = 0; i < 1000010; i++)  p[i] = i;
  for(int i = 1; i <= n; i++) {
    int pa = find(a[i]);
    sb.append(pa+" ");
    p[pa] = pa+1;
  }
  System.out.println(sb.toString().trim());
  }
  private static int find(int x) {
  if(x!=p[x]) p[x] = find(p[x]);
  return p[x];
  }
}
Python
N=100010
p=[i for i in range(N)]
def find(x):
    if p[x]!=x:
        p[x]=find(p[x])
    return p[x]
n=int(input())
cin=list(map(int,input().split()))
for i in range(n):
    x=find(cin[i])
    if i<n-1:
        print(x,end=' ')
    else:
        print(x)
    p[x]=x+1


相关文章
|
28天前
|
存储 算法 编译器
【C++ 字符数组的模板特化】面向字符串的C++模板特化:理解与实践
【C++ 字符数组的模板特化】面向字符串的C++模板特化:理解与实践
47 1
|
1月前
|
存储 缓存 安全
C++数组全解析:从基础知识到高级应用,领略数组的魅力与技巧
C++数组全解析:从基础知识到高级应用,领略数组的魅力与技巧
53 1
|
1月前
|
存储 算法 搜索推荐
在C++编程语言中数组的作用类型
在C++编程语言中数组的作用类型
14 0
在C++编程语言中数组的作用类型
|
1月前
|
C++
.C++中结构体数组docx
.C++中结构体数组docx
14 0
|
15天前
|
人工智能 机器人 C++
【C++/Python】Windows用Swig实现C++调用Python(史上最简单详细,80岁看了都会操作)
【C++/Python】Windows用Swig实现C++调用Python(史上最简单详细,80岁看了都会操作)
|
1月前
|
编译器 测试技术 C++
【Python 基础教程 01 全面介绍】 Python编程基础全攻略:一文掌握Python语法精髓,从C/C++ 角度学习Python的差异
【Python 基础教程 01 全面介绍】 Python编程基础全攻略:一文掌握Python语法精髓,从C/C++ 角度学习Python的差异
164 0
|
6天前
|
测试技术 C++
[蓝桥杯 2023 省 B] 冶炼金属(c++)
[蓝桥杯 2023 省 B] 冶炼金属(c++)
16 0
|
21天前
|
C++ Python
【C++/Python】C++调用python文件
【C++/Python】C++调用python文件
|
1月前
|
存储 缓存 安全
【C/C++ 基础 数组容器比较】深入探究C++容器:数组、vector与array之间的异同
【C/C++ 基础 数组容器比较】深入探究C++容器:数组、vector与array之间的异同
15 0
|
1月前
|
存储 算法 C语言
C++系列十二:指针数组
C++系列十二:指针数组

热门文章

最新文章