当前位置:网站首页>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刷题 (5.31) 字符串
- Use dumping to back up tidb cluster data to S3 compatible storage
- Migrate data from SQL files to tidb
- 【MySQL】数据库的存储过程与存储函数通关教程(完整版)
- 生成器参数传入参数
- JVM 快速入门
- 【Nvidia开发板】常见问题集 (不定时更新)
- On the day of resignation, jd.com deleted the database and ran away, and the programmer was sentenced
- Let the bullets fly for a while
- Roguelike游戏成破解重灾区,如何破局?
猜你喜欢

2. File operation - write
![[secretly kill little partner pytorch20 days -day01- example of structured data modeling process]](/img/ae/4e616882f6d68acdf8e885843e68a3.jpg)
[secretly kill little partner pytorch20 days -day01- example of structured data modeling process]

CISP-PTE实操练习讲解

All the ArrayList knowledge you want to know is here

Sort according to a number in a string in a column of CSV file

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

FairGuard游戏加固:游戏出海热潮下,游戏安全面临新挑战

Use Alibaba icon in uniapp

ESP系列引脚说明图汇总

JVM 快速入门
随机推荐
Pointer advanced --- pointer array, array pointer
延迟初始化和密封类
LDAP應用篇(4)Jenkins接入
Char to leading 0
Permutation and combination function
Summary of phased use of sonic one-stop open source distributed cluster cloud real machine test platform
Leetcode skimming (5.29) hash table
[research materials] 2022 China yuancosmos white paper - Download attached
IoT -- 解读物联网四层架构
ROS编译 调用第三方动态库(xxx.so)
Configuring OSPF load sharing for Huawei devices
How to use information mechanism to realize process mutual exclusion, process synchronization and precursor relationship
Introduction to number theory (greatest common divisor, prime sieve, inverse element)
Synchronized solves problems caused by sharing
2022 Inner Mongolia latest water conservancy and hydropower construction safety officer simulation examination questions and answers
1. Color inversion, logarithmic transformation, gamma transformation source code - miniopencv from zero
CISP-PTE实操练习讲解
个人电脑好用必备软件(使用过)
Cisp-pte practice explanation
2022.02.13 - NC004. Print number of loops
https://www.nowcoder.com/practice/7bbcdd2177a445a9b66da79512b32dd7?tpId=107&&tqId=33379&rp=1&ru=/ta/beginner-programmers&qru=/ta/beginner-programmers/question-ranking

