当前位置:网站首页>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; }
边栏推荐
- 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
- Process of obtaining the electronic version of academic qualifications of xuexin.com
- What is CSRF (Cross Site Request Forgery)?
- Function coritization
- Introduction to number theory (greatest common divisor, prime sieve, inverse element)
- egg. JS directory structure
- MySQL learning record 11jdbcstatement object, SQL injection problem and Preparedstatement object
- pytorch训练好的模型在加载和保存过程中的问题
- Research Report on Market Research and investment strategy of microcrystalline graphite materials in China (2022 Edition)
- JS inheritance method
猜你喜欢

C language - bit segment

Hungry for 4 years + Ali for 2 years: some conclusions and Thoughts on the road of research and development

C language custom type: struct

Personalized online cloud database hybrid optimization system | SIGMOD 2022 selected papers interpretation

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

egg. JS project deployment online server

根据csv文件某一列字符串中某个数字排序

生成器参数传入参数

Roguelike游戏成破解重灾区,如何破局?

tree树的精准查询
随机推荐
电脑F1-F12用途
All the ArrayList knowledge you want to know is here
查看局域网中电脑设备
Introduction to number theory (greatest common divisor, prime sieve, inverse element)
3. File operation 3-with
LDAP Application Section (4) Jenkins Access
China high purity silver nitrate Market Research and investment strategy report (2022 Edition)
Use br to back up tidb cluster data to S3 compatible storage
MySQL learning record 11jdbcstatement object, SQL injection problem and Preparedstatement object
String to leading 0
Sort according to a number in a string in a column of CSV file
Leetcode question brushing (5.28) hash table
logback1.3. X configuration details and Practice
2022.02.13 - NC003. Design LRU cache structure
【ROS】usb_cam相机标定
Verrouillage [MySQL]
VMware 虚拟化集群
Restore backup data on S3 compatible storage with br
What is the use of entering the critical point? How to realize STM32 single chip microcomputer?
LDAP application (4) Jenkins access
https://www.nowcoder.com/practice/7bbcdd2177a445a9b66da79512b32dd7?tpId=107&&tqId=33379&rp=1&ru=/ta/beginner-programmers&qru=/ta/beginner-programmers/question-ranking

