当前位置:网站首页>C语言双指针——经典题型
C语言双指针——经典题型
2022-07-06 08:28:00 【终为—NULL】
每天进步一点点,坚持带来大改变!!!

1.序列中删除指定数字
牛客网链接:
描述
有一个整数序列(可能有重复的整数),现删除指定的某一个整数,输出删除指定数字之后的序列,序列中未被删除数字的前后位置没有发生改变。
数据范围:序列长度和序列中的值都满足 1≤n≤50
输入描述:
第一行输入一个整数(0≤N≤50)。
第二行输入N个整数,输入用空格分隔的N个整数。
第三行输入想要进行删除的一个整数。
输出描述:
输出为一行,删除指定数字之后的序列。
示例1
输入:
6
1 2 3 4 5 9
4
输出:1 2 3 5 9示例2
输入:
5
1 2 3 4 6
5
输出:1 2 3 4 6
思路:
定义两个变量,都从数组下标为0的位置开始,i变量遍历整个数组,j变量用来存放不是被删除的元素,当i找到删除的元素之后i继续向后访问,j不加加,当不是要删除的元素的时候,将下标为i的元素存放到j下标,然后j继续加加。
#include<stdio.h> int main() { int n = 0; int arr[50] = { 0 }; scanf("%d", &n); int i = 0; for (i = 0; i < n; i++) { scanf("%d", &arr[i]); } int del = 0; int j = 0; scanf("%d", &del); for (i = 0; i < n; i++) { if (arr[i] != del) { arr[j++] = arr[i]; } } for (i = 0; i < j; i++) { printf("%d ", arr[i]); } return 0; }
2.序列中删除去重
牛客网链接:
描述
输入n个整数的序列,要求对这个序列进行去重操作。所谓去重,是指对这个序列中每个重复出现的整数,只保留该数第一次出现的位置,删除其余位置。
输入描述:
输入包含两行,第一行包含一个正整数n(1 ≤ n ≤ 1000),表示第二行序列中数字的个数;第二行包含n个整数(范围1~5000),用空格分隔。
输出描述:
输出为一行,按照输入的顺序输出去重之后的数字,用空格分隔。
示例1
输入:
5
10 12 93 12 75
输出:
10 12 93 75
思路:
#include<stdio.h> int main() { int n = 0; scanf("%d", &n); int arr[1000] = { 0 }; int i = 0; for (i = 0; i < n; i++) { scanf("%d", &arr[i]); } for (i = 0; i < n; i++) { int j = 0; for (j = i + 1; j < n; j++) { if (arr[i] == arr[j]) { int k = 0; for (k = j; k < n - 1; k++) { arr[k] = arr[k + 1]; } n--; j--; } } } for (i = 0; i < n; i++) { printf("%d ", arr[i]); } return 0; }
3.有序序列的合并:
牛客网链接:
描述
输入两个升序排列的序列,将两个序列合并为一个有序序列并输出。
数据范围:1≤n,m≤1000 , 序列中的值满足 :0≤val≤30000
输入描述:
输入包含三行,
第一行包含两个正整数n, m,用空格分隔。n表示第二行第一个升序序列中数字的个数,m表示第三行第二个升序序列中数字的个数。
第二行包含n个整数,用空格分隔。
第三行包含m个整数,用空格分隔。输出描述:
输出为一行,输出长度为n+m的升序序列,即长度为n的升序序列和长度为m的升序序列中的元素重新进行升序序列排列合并。
示例1
输入:
5 6
1 3 7 9 22
2 8 10 17 33 44
输出:1 2 3 7 8 9 10 17 22 33 44
思路:
#include<stdio.h> int main() { int arr1[1000] = { 0 }; int arr2[1000] = { 0 }; int arr3[2000] = { 0 }; int n = 0; int m = 0; scanf("%d%d", &n, &m); int i = 0; for (i = 0; i < n; i++) { scanf("%d", &arr1[i]); } for (i = 0; i < m; i++) { scanf("%d", &arr2[i]); } i = 0; int j = 0; int k = 0; while (i < n && j < m) { if (arr1[i] < arr2[j]) { arr3[k++] = arr1[i++]; } else { arr3[k++] = arr2[j++]; } } if (i == n) { for (; j < m; j++) { arr3[k++] = arr2[j]; } } else { for (; i < n; i++) { arr3[k++] = arr1[i]; } } for (i = 0; i < n + m; i++) { printf("%d ", arr3[i]); } return 0; }
边栏推荐
- Leetcode skimming (5.29) hash table
- 电脑清理,删除的系统文件
- 【ROS】usb_cam相机标定
- Process of obtaining the electronic version of academic qualifications of xuexin.com
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- sys.argv
- 2022.02.13 - NC004. Print number of loops
- 2022 Inner Mongolia latest water conservancy and hydropower construction safety officer simulation examination questions and answers
- pytorch训练好的模型在加载和保存过程中的问题
- What is CSRF (Cross Site Request Forgery)?
猜你喜欢

3. File operation 3-with

sublime text没关闭其他运行就使用CTRL+b运行另外的程序问题

【MySQL】数据库的存储过程与存储函数通关教程(完整版)

Cisp-pte practice explanation

Leetcode question brushing (5.28) hash table

MySQL learning record 10getting started with JDBC

egg. JS getting started navigation: installation, use and learning

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
![[research materials] 2021 China online high growth white paper - Download attached](/img/51/bea6179e4fac88f8b550b4213a2bca.jpg)
[research materials] 2021 China online high growth white paper - Download attached

Hcip day 16
随机推荐
ESP series pin description diagram summary
堆排序详解
升级 TiDB Operator
China's high purity aluminum target market status and investment forecast report (2022 Edition)
从 TiDB 集群迁移数据至另一 TiDB 集群
egg. JS directory structure
IoT -- 解读物联网四层架构
[MySQL] database stored procedure and storage function clearance tutorial (full version)
ROS编译 调用第三方动态库(xxx.so)
[research materials] 2021 China online high growth white paper - Download attached
[brush questions] top101 must be brushed in the interview of niuke.com
[cloud native] teach you how to build ferry open source work order system
[research materials] 2021 live broadcast annual data report of e-commerce - Download attached
logback1.3. X configuration details and Practice
从 SQL 文件迁移数据到 TiDB
C language custom type: struct
leetcode刷题 (5.31) 字符串
Research Report on supply and demand and development prospects of China's high purity aluminum market (2022 Edition)
2022.02.13 - NC004. Print number of loops
MySQL learning record 07 index (simple understanding)
https://www.nowcoder.com/practice/7bbcdd2177a445a9b66da79512b32dd7?tpId=107&&tqId=33379&rp=1&ru=/ta/beginner-programmers&qru=/ta/beginner-programmers/question-ranking

