当前位置:网站首页>XDOJ - count the number of positive integers
XDOJ - count the number of positive integers
2022-08-08 17:20:00 【Wholehearted】
完整代码如下
在这里插入#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
int a[n];
int i,j,temp;
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(a[i]>a[j]){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
//The above for sorting an array,以下为核心代码
for(i=0;i<n-1;i++){
if(a[i]==a[i+1]){
for(j=i+1;j<n-1;j++){
a[j]=a[j+1];
}
i--; //i--But the key,If once before and after a fairly,After early to judge whether to a new defender is equal.
//比如
n--;
}
}
for(i=0;i<n;i++){
printf("%d\n",a[i]);
}
return 0;
}
核心代码如下
if(a[i]==a[i+1]){
for(j=i+1;j<n-1;j++){
a[j]=a[j+1];
}
i--; //i--But the key,If once before and after a fairly,After early to judge whether to a new defender is equal.
n--;
}
}
首先,This algorithm has a point to note,i在- -,同时n也必定- -;
Cases of calculus:
案例 111223
Before and after the equivalent,Ahead of time
11223,但此时i没有- -,iSmoothly into the1
显然arr[1]= arr[2]
So in the event of avant-garde is equal to the situation after a,一定要i - -
原理:From the original equal position to compare to after a,Equivalent of each has been ahead of time,Until before a is not equal to after a can from under a number as a starting point in comparison with the Numbers of the following is.
Where each comparison position as a starting point,Until when only its own a number of mobile starting point.
同时n也一定要- -,Because it must be the final 123333
Mantissa is equal,如果没有n --,3Always equal to the back of the3
,i始终- - 后又+ +
进入死循环
当然,If you feel the abstract,From the perspective of simple to understand,11223>>12233>>12333,Every do early treatment is more a do not belong to the original array's tail,The original array can not so much3,It is good to cut
补充
This is just growing up row not repeat order,The following statistical code snippet to be modified can
for(i=0;i<n;i++){
b[i]=1;
}
for(i=0;i<n-1;i++){
if(a[i]==a[i+1]){
for(j=i+1;j<n-1;j++){
a[j]=a[j+1];
}
b[i]++;//There have been several times the number of the first,After continuously in advance,In the starting point to the nexti才++,The second number is the second number at this time
i--; //i--But the key,If once before and after a fairly,After early to judge whether to a new defender is equal.
//比如
n--;
}
}
for(i=0;i<n;i++){
printf("%d:%d\n",a[i],b[i]);
}
return 0;
}
边栏推荐
- banner 如何显示drawable图片
- B. Stairs
- 3531. 哈夫曼树
- 2022-08-08日报:Kaggle所有竞赛开源方案和Top思路汇总
- 迁移学习(Transfer Learning)的背景、历史
- Are Huishang Futures official and reliable?Is it safe to open an account in Huishang Futures?
- C语言每日一练——Day01:求最大公约数(三种方法)
- D2. Sage‘s Birthday (hard version)
- Qt——获取文件夹下所有子文件名称
- L2-011 玩转二叉树 (25 分) (二叉树)
猜你喜欢
随机推荐
正则在js中的使用
L2-013 红色警报 (25 分)(并查集)
L2-008 最长对称子串 (25 分)
banner 如何显示drawable图片
在指南针炒股软件中的指标靠谱吗?安全吗?
Regular use in js
dp,dpi,px知识补充
4. S32K14X study notes: S32 Design Studio new and imported projects
The latest research from PNAS: 81% problem solving rate, neural network Codex opens the door to the world of advanced mathematics
leetcode:295. 数据流的中位数
vi编辑器命令
Debug和Release的区别
L2-025 分而治之 (25 分)
L2-023 图着色问题 (25 分)
Appium 自动化测试环境搭建
rv和sv的区别
LeetCode_Backtrack_Medium_491. Incrementing Subsequence
D. Non-zero Segments
The difference between rv and sv
Tensorflow教程(三)——获取数据 feed 和 fetchn