当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
一、搭建django自动化平台(实现一键执行sql)
PNAS最新研究:81%解题率,神经网络 Codex 推开高等数学世界大门
Mysql都有那些最需要掌握的原理?
[Paper Reading] RAL 2022: Receding Moving Object Segmentation in 3D LiDAR Data Using Sparse 4D Convolutions
Appium 自动化测试环境搭建
开源一夏 | 疫情期间闲来无事,我自制了一个按钮展示框特效来展示我的博客
Tensorflow教程(四)——MNIST项目入门
【 8.7 】 source code - card to LCM with GCD 】 【 】
R文件找不到问题
2022年11大最佳缺陷管理工具盘点
随机推荐
The latest research from PNAS: 81% problem solving rate, neural network Codex opens the door to the world of advanced mathematics
软件工程基础知识--认识软件工程
L2-008 最长对称子串 (25 分)
Cholesterol-PEG-DBCO,CLS-PEG-DBCO,胆固醇-聚乙二醇-二苯基环辛炔一种环炔烃
京东二面:高并发设计,都有哪些技术方案?
Basic knowledge of software engineering, software engineering
The difference between rv and sv
Cy5反式环辛烯,TCO-Cy5,Cy5 trans-cyclooctene标记生物分子
L2-017 人以群分 (25 分)
L2-025 分而治之 (25 分)
二、junit接口自动化框架之二次开发
爬百度图片
Charles MOCK 数据 htpps代理
【 8.7 】 source code - card to LCM with GCD 】 【 】
yarn : 无法加载文件 D:xxx\node_global\yarn.ps1 因为在此系统上禁止运行脚本
【TypeScript】函数类型:返回值类型和参数类型到底如何定义?
L2-010 排座位 (25 分) (DFS)
H. Huge Boxes of Animal Toys
博客主页rrs代码
uri (url urn 的区别)