当前位置:网站首页>C語言雙指針——經典題型
C語言雙指針——經典題型
2022-07-06 08:32: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; }
边栏推荐
- VMware virtualization cluster
- Introduction to backup and recovery Cr
- LDAP应用篇(4)Jenkins接入
- Zhong Xuegao, who cannot be melted, cannot escape the life cycle of online celebrity products
- Mobile Test Engineer occupation yyds dry goods inventory
- [research materials] 2021 China online high growth white paper - Download attached
- ROS编译 调用第三方动态库(xxx.so)
- All the ArrayList knowledge you want to know is here
- JS pure function
- Unified ordering background interface product description Chinese garbled
猜你喜欢
What is the use of entering the critical point? How to realize STM32 single chip microcomputer?
Fibonacci sequence
【刷题】牛客网面试必刷TOP101
2. File operation - write
How to use information mechanism to realize process mutual exclusion, process synchronization and precursor relationship
ESP系列引脚說明圖匯總
sublime text中conda环境中plt.show无法弹出显示图片的问题
JVM performance tuning and practical basic theory - Part 1
synchronized 解决共享带来的问题
JS native implementation shuttle box
随机推荐
个人电脑好用必备软件(使用过)
MySQL learning record 07 index (simple understanding)
被破解毁掉的国产游戏之光
Introduction to backup and recovery Cr
JVM 快速入门
Sort according to a number in a string in a column of CSV file
China high purity silver nitrate Market Research and investment strategy report (2022 Edition)
Migrate data from CSV files to tidb
根据csv文件某一列字符串中某个数字排序
Summary of MySQL index failure scenarios
[research materials] 2021 live broadcast annual data report of e-commerce - Download attached
China's high purity aluminum target market status and investment forecast report (2022 Edition)
LDAP Application Section (4) Jenkins Access
电脑清理,删除的系统文件
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
JVM performance tuning and practical basic theory - Part 1
sys.argv
Mobile Test Engineer occupation yyds dry goods inventory
Online yaml to CSV tool
1. Color inversion, logarithmic transformation, gamma transformation source code - miniopencv from zero