当前位置:网站首页>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; }
边栏推荐
- egg. JS project deployment online server
- Migrate data from a tidb cluster to another tidb cluster
- Research Report on Market Research and investment strategy of microcrystalline graphite materials in China (2022 Edition)
- IOT -- interpreting the four tier architecture of the Internet of things
- Summary of MySQL index failure scenarios
- Use br to back up tidb cluster data to S3 compatible storage
- [research materials] 2022 China yuancosmos white paper - Download attached
- China Light conveyor belt in-depth research and investment strategy report (2022 Edition)
- 同一局域网的手机和电脑相互访问,IIS设置
- 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
猜你喜欢

Wincc7.5 download and installation tutorial (win10 system)

Deep learning: derivation of shallow neural networks and deep neural networks

Restful API design specification

pytorch训练好的模型在加载和保存过程中的问题

2. File operation - write

Circular reference of ES6 module

游戏解包的危害及资源加密的重要性

Pointer advanced --- pointer array, array pointer

ESP系列引脚说明图汇总

【ROS】usb_cam相机标定
随机推荐
Huawei cloud OBS file upload and download tool class
torch建立的网络模型使用torchviz显示
Research and investment forecast report of citronellol industry in China (2022 Edition)
LDAP application (4) Jenkins access
egg. JS getting started navigation: installation, use and learning
LDAP應用篇(4)Jenkins接入
How to use information mechanism to realize process mutual exclusion, process synchronization and precursor relationship
Configuring OSPF load sharing for Huawei devices
Upgrade tidb operator
Colorlog combined with logging to print colored logs
MySQL learning record 10getting started with JDBC
logback1.3. X configuration details and Practice
Use dumping to back up tidb cluster data to S3 compatible storage
按位逻辑运算符
leetcode刷题 (5.31) 字符串
vulnhub hackme: 1
Mobile Test Engineer occupation yyds dry goods inventory
查看局域网中电脑设备
Bottom up - physical layer
What is CSRF (Cross Site Request Forgery)?
https://www.nowcoder.com/practice/7bbcdd2177a445a9b66da79512b32dd7?tpId=107&&tqId=33379&rp=1&ru=/ta/beginner-programmers&qru=/ta/beginner-programmers/question-ranking

