当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
随机推荐
迁移学习(Transfer Learning)的背景、历史
arxiv国内镜像——快速下载
Fluorescein-PEG-CLS,胆固醇-聚乙二醇-荧光素用于缩短包封周期
Tensorflow教程(三)——获取数据 feed 和 fetchn
L2-028 秀恩爱分得快 (25 分)
Solve the inexplicable problem of MySQL violently - restart the service!
七、jmeter发出请求的逻辑
离线安装 Anaconda + TensorFlow
R文件找不到问题
Tensorflow教程(六)——变量基础操作
MySQL database
并发与并行
【TypeScript】函数类型:返回值类型和参数类型到底如何定义?
通俗易懂的epoll
Camera calibration toobox for Matlab(一)—— 工具包的基本使用
L2-021 点赞狂魔 (25 分)
L2-024 部落 (25 分)(并查集)
The difference between B+ tree and B- tree
KITTI数据集简介(一)—— 传感器介绍
2 prerequisites for the success of "digital transformation" of enterprises!









