当前位置:网站首页>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; }
边栏推荐
- [cloud native topic -45]:kubesphere cloud Governance - Introduction and overall architecture of enterprise container platform based on kubernetes
- [2022 广东省赛M] 拉格朗日插值 (多元函数极值 分治NTT)
- Erc20 token agreement
- Modify the video name from the name mapping relationship in the table
- 游戏解包的危害及资源加密的重要性
- Summary of phased use of sonic one-stop open source distributed cluster cloud real machine test platform
- tree树的精准查询
- Is it safe to open an account in Zheshang futures?
- What is CSRF (Cross Site Request Forgery)?
- Introduction to backup and recovery Cr
猜你喜欢

JS native implementation shuttle box

Chrome浏览器的crash问题

Configuring OSPF load sharing for Huawei devices
![[cloud native topic -45]:kubesphere cloud Governance - Introduction and overall architecture of enterprise container platform based on kubernetes](/img/ac/773ce8ee7f380df19edf8373250608.jpg)
[cloud native topic -45]:kubesphere cloud Governance - Introduction and overall architecture of enterprise container platform based on kubernetes

C language custom type: struct

Zhong Xuegao, who cannot be melted, cannot escape the life cycle of online celebrity products

Leetcode question brushing (5.28) hash table

What is CSRF (Cross Site Request Forgery)?

化不掉的钟薛高,逃不出网红产品的生命周期

Precise query of tree tree
随机推荐
2022.02.13 - NC002. sort
What is the use of entering the critical point? How to realize STM32 single chip microcomputer?
Restore backup data on S3 compatible storage with tidb lightning
China dihydrolaurenol market forecast and investment strategy report (2022 Edition)
The resources of underground pipe holes are tight, and the air blowing micro cable is not fragrant?
[research materials] 2021 China online high growth white paper - Download attached
Sort according to a number in a string in a column of CSV file
被破解毁掉的国产游戏之光
logback1.3. X configuration details and Practice
2022.02.13 - NC004. Print number of loops
2022.02.13 - 238. Maximum number of "balloons"
Yyds dry goods inventory three JS source code interpretation eventdispatcher
【Nvidia开发板】常见问题集 (不定时更新)
Synchronized solves problems caused by sharing
Bottom up - physical layer
JS pure function
Configuring OSPF load sharing for Huawei devices
从表中名称映射关系修改视频名称
Remote storage access authorization
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
https://www.nowcoder.com/practice/7bbcdd2177a445a9b66da79512b32dd7?tpId=107&&tqId=33379&rp=1&ru=/ta/beginner-programmers&qru=/ta/beginner-programmers/question-ranking

