当前位置:网站首页>Greedy - deletion
Greedy - deletion
2022-07-20 08:26:00 【C0re in the lonely age】
Catalog
The theory exists , Practice begins !
Love is worth years. Love is worth years .
Preface
Number of deletions !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Topic introduction
sample input :
178543 4
sample output :
13
Before starting, you can write it yourself to see if you can write it yourself ~ Pay attention to the output format !
analysis
Greedy strategy , Find the number whose later number is greater than the previous number, and then ! Delete him , You can minimize the remaining number !
The theory exists , Practice begins !
Code :
#include <stdio.h>
#include <string.h>
int main()
{
int s,w,i;
char a[101];
scanf("%s%d",a,&s);
while (s)// If s==0 Then direct output is good !
{
w=strlen(a);
i=0;
while(i<w&&a[i]<=a[i+1])// Traverse each number
{
i++;
}
// find a[i]>a[i+1] The number of
while (i<w)
{
a[i]=a[i+1];// Move the array forward and delete it a[i]
i++;
}
s--;// Delete a number until there is no need to delete
}
int j=0;
while (a[0] =='0')// If the first digit is 0;
{
while (j<w)
{
a[j]=a[j+1];// Move array forward
j++;
}
j=0;// hold j Revert to the first one for next recycling
}
if(a[0]==0)// At this point if a[0] also ==0 So there's only 0 了
printf("0\n");
else
printf("%s\n",a);
return 0;
}
matters needing attention :
1. I think so.
This while It's very essential to use .
2. Consider that the first number equals 0 The situation of , If the first number equals 0, Then remove it .
3. Want to consider a[0]==0 Otherwise, it cannot be output
ok The conclusion is over
Love is worth years.
Love is worth years .
边栏推荐
猜你喜欢
Redis geographic algorithm geo analysis and Application
整型及浮点型数据在内存中的存储
[yolov5 realizes mask and face detection]
ZABBIX sender adds a custom monitoring item -- Ping to destination link monitoring -- bat script cycle
Securityerror: (:) [] occurs when CMD executes the command, parentcontainserrorrecordexception
STM32-基于汇编来分析延时
Sword finger offer serialized binary tree
Pytorch implements the use of data enhancement classification evaluations
自定义类型:结构体,位段,枚举,联合
2021水下声学目标检测总结-Rank2
随机推荐
练习(2)创建一个集合,存放元素“1“,“$“,“2“,“$“,“3“,“$“,“4“
pytorch 华为云杯垃圾分类总结(目标检测)
位运算——异或
Student achievement management system (C language)
ThreadLocal learning notes
Pytorch yolo5 training any training set
pytorch 实现数据增强分类 albumentations的使用
Introduction to pytoch target classification competition
第五十九篇:main.c:62:9: note: use option -std=c99 or -std=gnu99 to compile your code
Partition of integers
C语言中的指针(学习心得)
[Day.2]约瑟夫环问题,如何用数组代替循环链表(详解)
yolov3的GUI界面(简易,图片检测)
第七十四篇:机器学习优化方法及超参数设置综述
函数指针
第六十七篇:opencv中KeyPoint与point2f之间相互转换
MySQL ten million level sub table optimization
递归回溯—走迷宫
巧解杨氏矩阵
2021 underwater acoustic target detection summary -rank2