当前位置:网站首页>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; }
边栏推荐
- 【Nvidia开发板】常见问题集 (不定时更新)
- China dihydrolaurenol market forecast and investment strategy report (2022 Edition)
- Sort according to a number in a string in a column of CSV file
- Grayscale upgrade tidb operator
- Deep learning: derivation of shallow neural networks and deep neural networks
- C language - bit segment
- Leetcode question brushing (5.31) string
- China vanadium battery Market Research and future prospects report (2022 Edition)
- pcd转ply后在meshlab无法打开,提示 Error details: Unespected eof
- Hungry for 4 years + Ali for 2 years: some conclusions and Thoughts on the road of research and development
猜你喜欢

synchronized 解决共享带来的问题

2022.02.13 - NC001. Reverse linked list
![[research materials] 2022 enterprise wechat Ecosystem Research Report - Download attached](/img/35/898a8086bc35462b0fcb9e6b58b86b.jpg)
[research materials] 2022 enterprise wechat Ecosystem Research Report - Download attached
![[MySQL] log](/img/e9/1617122888c096cf6aba2bdb88f0ef.png)
[MySQL] log

Summary of phased use of sonic one-stop open source distributed cluster cloud real machine test platform

sublime text的编写程序时的Tab和空格缩进问题

【MySQL】鎖

2. File operation - write

704 二分查找

2022 Inner Mongolia latest water conservancy and hydropower construction safety officer simulation examination questions and answers
随机推荐
PLT in Matplotlib tight_ layout()
Use Alibaba icon in uniapp
如何进行接口测试测?有哪些注意事项?保姆级解读
CISP-PTE实操练习讲解
C language - bit segment
Day29-t77 & t1726-2022-02-13-don't answer by yourself
Online yaml to CSV tool
String to leading 0
Upgrade tidb with tiup
【MySQL】日志
logback1.3. X configuration details and Practice
China Light conveyor belt in-depth research and investment strategy report (2022 Edition)
Restore backup data on S3 compatible storage with tidb lightning
The resources of underground pipe holes are tight, and the air blowing micro cable is not fragrant?
【MySQL】数据库的存储过程与存储函数通关教程(完整版)
Pointer advanced --- pointer array, array pointer
vulnhub hackme: 1
[research materials] 2021 Research Report on China's smart medical industry - Download attached
Huawei cloud OBS file upload and download tool class
Bottom up - physical layer
https://www.nowcoder.com/practice/7bbcdd2177a445a9b66da79512b32dd7?tpId=107&&tqId=33379&rp=1&ru=/ta/beginner-programmers&qru=/ta/beginner-programmers/question-ranking

