当前位置:网站首页>The way to conquer C language
The way to conquer C language
2022-04-23 02:42:00 【^O ^ play programming】
Catalog
Preface
Today, I'd like to share with you some questions of niuke.com
subject
1 Delete the specified number from the sequence
Original idea : Is to find the number to replace in the sequence first , Delete this number , But I soon found myself unable to delete this number .( Some friends who know how to delete can tell me oh !).
Their thinking :1 Exclude the number of deletions from the sequence .
2 Before moving the number after this sequence forward .
3 Print this new sequence .
Code :
#include<stdio.h>
int main()
{
int n = 0;
scanf("%d",&n);
int arr[50]={0};
int i = 0;
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
// Enter the number of deletions
int del = 0;
scanf("%d",&del);
int j = 0;
for(i=0;i<n;i++)
{
if(arr[i]!=del)
{
arr[j++]=arr[i];
}
}
// Print
for(i=0;i<j;i++)
{
printf("%d ",arr[i]);
}
return 0;
}
2 Integer de duplication in sequence
Their thinking :1 Find the same elements
2 If the number of elements is the same , Move the subsequent elements forward
Code :
#include<stdio.h>
int main()
{
int n = 0;
scanf("%d", &n);
// data input
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++)
{
// Find the same element first
// If the same , Move the subsequent elements previously
if (arr[i] == arr[j])
{
int m = 0;
for (m = j;m < n - 1;m++)
{
arr[m] = arr[m + 1];
}
n--;// Indicates that an identical number has been removed
j--;// Continue to judge whether the next one is multiplicative
}
}
}
for (i = 0;i < n;i++)
{
printf("%d ", arr[i]);
}
return 0;
}
3 Ordered sequence merging
Their thinking :1 Create three arrays arr1 and arr2 And arr3 Let the subscripts be i and j And k( The initial values are 0).
2 Give Way arr1[i] and arr2[j] Compare , Let a smaller number be deposited in arr3 Zhongzai Jean i++ perhaps j++, Then continue to compare .
3 When arr1 perhaps arr2 After all the elements in are saved, continue to store the unfinished array in arr3 in .
Code :
#include<stdio.h>
int main()
{
int n = 0;
int m = 0;
scanf("%d %d", &n, &m);
int arr1[1000] = { 0 };
int arr2[1000] = { 0 };
int arr3[2000] = { 0 };
// data input
int i = 0;
for (i = 0;i < n;i++)
{
scanf("%d", &arr1[i]);
}
for (i = 0;i < m;i++)
{
scanf("%d", &arr2[i]);
}
// Handle
int j = 0;
int k = 0;
i = 0;
while (i < n && j < m)
{
// Put a smaller number into the merged array
if (arr1[i] < arr2[j])
{
arr3[k++] = arr1[i++];
}
else
{
arr3[k++] = arr2[j++];
}
}
// When arr1 The array elements are saved first
// take arr2 Put the remaining number in arr3 in
if (i == n)
{
for (;j < m;j++)
{
arr3[k++] = arr2[j];
}
}
else
{
for (;i < n;i++)
{
arr3[k++] = arr1[i];
}
}
// Print arr3
for (i = 0;i < k;i++)
{
printf("%d ", arr3[i]);
}
return 0;
}
Conclusion
I'll share it with you today , I hope my friends can give me more advice , Come on, everybody , Progress together !
版权声明
本文为[^O ^ play programming]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220748162451.html
边栏推荐
- Flink stream processing engine system learning (I)
- Flink stream processing engine system learning (III)
- JVM runtime data area (I)
- 1215_ Hello world used by scons
- Suggestion: block reference sorting is in the order of keywords
- [suggestion collection] hematemesis sorting out golang interview dry goods 21 questions - hanging interviewer-1
- 程序设计天梯赛 L1-49 天梯赛分配座位(模拟),布响丸辣
- Cuisine leetcode
- Decision tree principle of machine learning
- 全局、独享、局部路由守卫
猜你喜欢
LeetCode 1450 - 1453
一、序列模型-sequence model
Parental delegation model [understanding]
[xjtu Computer Network Security and Management] session 2 Cryptographic Technology
Preliminary understanding of stack and queue
MySQL JDBC编程
国产轻量级看板式Scrum敏捷项目管理工具
Intelligent agricultural management model
After idea is successfully connected to H2 database, there are no sub files
JVM class loader
随机推荐
学习正则表达式选项、断言
Learn regular expression options, assertions
The penultimate K nodes in jz22 linked list
Machine learning (Zhou Zhihua) Chapter 14 probability graph model
程序设计天梯赛 L1-49 天梯赛分配座位(模拟),布响丸辣
本地远程访问云服务器的jupyter
SQL server2019无法下载所需文件,这可能表示安装程序的版本不再受支持,怎么办了
IAR embedded development stm32f103c8t6 Lighting LED
JVM类加载器
智能辅助功能丰富,思皓X6安全配置曝光:将于4月23日预售
Rhcsa day 4 operation
【Hcip】OSPF常用的6种LSA详解
16、 Anomaly detection
使用Go语言构建Web服务器
第46届ICPC亚洲区域赛(昆明) B Blocks(容斥+子集和DP+期望DP)
[untitled]
Efficient music format conversion tool Music Converter Pro
Use of go language web Middleware
This is how the power circuit is designed
MySQL function syntax