由于这题对于学过数据结构的我来说,真的是很简单,为了减少时间上的损失,链表无疑是最好的选择(因为数组要往后移位子)。然后,因为最近想玩些STL的骚操作,所以就用<list>了,然后顺便学了下迭代器的用法,其原理其实和指针差不多,就是比指针安全。然后,在a题的过程中发现指针的+—还是有些问题的,所以,就用数值代替了。闲话不多说,奉上ac代码:

#include <iostream>
#include<math.h>
#include <iomanip>
#include<cstdio>
#include<string>
#include<map>
#include<vector>
#include<list>
#include<algorithm>
#include<stdlib.h>
#include<iterator>
using namespace std; int main()
{
int n,m;
while(cin>>n>>m)
{
if(n==&&m==){
break;
} list<int>num;
list<int>::iterator p;
list<int>::iterator q;
list<int>::iterator r,beg,e_d;
int ck;
for(int i=;i<n;i++){
cin>>ck;
num.push_back(ck);
} q=num.begin();
for(p=num.begin();p!=num.end();p++){
if(*p>=m)
{
q=p;
break;
}
}
num.insert(q,m);
// beg=num.begin();
// e_d=num.end()--; int daxiao=num.size();
r=num.begin();
for(int i=;i<daxiao;r++){ if(i<daxiao-)
{
cout<<*r<<" ";
}
else{
cout<<*r;
}
i++; }
cout<<endl; } return ;
}

然后,再奉上用数组写的代码:

#include <iostream>

using namespace std;

int main()
{
int n,m,*input_str,flag=-;
while(cin>>n>>m)
{
//n=0,m=0结束输入
if(n==&&m==)break;
//为输入的数据分配空间,因为最后要插入一个数,所以分配大小为n+1
input_str=new int[n+]; for(int i=; i<n; i++)
{
cin>>input_str[i];
//flag记住第一次比m大的位置,最后是要把m放在flag这个位置
if(input_str[i]>=m&&flag==-)
{
flag=i;
}
}
for(int j=n; j>flag; j--)
{
//将flag后面的元素都后移1位
input_str[j]=input_str[j-];
}
//将m放入flag位置
input_str[flag]=m;
//格式输出
for(int k=; k<n+; k++)
{
if(k==n) cout<<input_str[k]<<endl;
else
cout<<input_str[k]<<" ";
}
//重新初始化flag,方便下次使用
flag=-;
delete input_str;
}
return ;
}

杭电2019 数列有序!(STL解法)的更多相关文章

  1. 杭电ACM2019--数列有序!

    数列有序! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  2. hdu 2019:数列有序!(数据结构,直接插入排序+折半插入排序)

    数列有序! Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submiss ...

  3. HDU 2019 数列有序!

    Time Limit: 1000 MS Memory Limit: 32768 KB 64-bit integer IO format: %I64d , %I64u   Java class name ...

  4. 杭电ACM2009--求数列的和

    求数列的和 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  5. HDOJ 2019 数列有序!

    #include<vector> #include<iostream> #include<algorithm> #include<cstdio> usi ...

  6. 杭电2019多校第八场 Acesrc and Good Numbers——思维打表&&oeis

    题意 给定 $d,x$,$f(d,k)$ 表示 $1 \sim k$ 中 $d$ 出现的次数, $k$ 满足 $f(d,k) = k$,求小于 $x$ 的最大的 $k$. 分析 正解不会...,学习了 ...

  7. 杭电2019多校第一场,Problem I,String 2019

    题目描述 Tom has a string containing only lowercase letters. He wants to choose a subsequence of the str ...

  8. 杭电OJ第11页2010-2019道题(C语言)

    2010. 水仙花数 问题描述 Problem Description 春天是鲜花的季节,水仙花就是其中最迷人的代表,数学上有个水仙花数,他是这样定义的: "水仙花数"是指一个三位 ...

  9. 杭电oj2012-2021

    2012  素数判定 #include <stdio.h> #include <math.h> int main() { int x,y,i,j,a,flag; while(s ...

随机推荐

  1. 外键约束:foreign key

    *外键 foreign key* 1.概念:如果一个实体的(student)的某个字段,指向(引用)另个实体(class)的主键 (class:class_id),就称为student实体的class ...

  2. web应用程序上传文件 超过了最大请求长度

    具体问题如下图 具体问题描述:在web应用程序中,上传了200M的文件,出现了如上图的问题,上传较小文件的时候,没有任何的问题.但是,测试的能力,不容小觑,真真的会测试的很全面.测试到了这个问题,好吧 ...

  3. 查看Oracle的表中有哪些索引(用user_indexes和user_ind_columns)

    用user_indexes和user_ind_columns系统表查看已经存在的索引 对于系统中已经存在的索引我们可以通过以下的两个系统视图(user_indexes和user_ind_columns ...

  4. if的特性

    js中 if(变量),这个变量只要满足: 变量如果不为0,null,undefined,false,都会被处理为true.只要变量有非0的值或是某个对象,数组,字符串,都会认为true

  5. 存储引擎,MySQL中的数据类型及约束

    存储引擎,MySQL中的数据类型及约束 一.存储引擎 1.不同的数据应该有不同的处理机制 2.mysql存储引擎 ​ Innodb:默认的存储引擎,查询速度叫myisam慢,但是更安全 ​ 支持事务, ...

  6. 吴裕雄 python 机器学习——模型选择损失函数模型

    from sklearn.metrics import zero_one_loss,log_loss def test_zero_one_loss(): y_true=[1,1,1,1,1,0,0,0 ...

  7. java redis 实现用户签到功能(很普通简单的签到功能)

    业务需求是用户每天只能签到一次,而且签到后用户增加积分,所以把用户每次签到时放到redis 缓存里面,然后每天凌晨时再清除缓存,大概简单思想是这样的 直接看代码吧如下 @Transactional @ ...

  8. leetcode菜鸡斗智斗勇系列(8)--- Find N Unique Integers Sum up to Zero

    1.原题: https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/ Given an integer n, retur ...

  9. AgileReview 代码检视工具使用

    AgileReview 它是一个eclipse插件. http://www.agilereview.org/ 官网地址. 四种下载插件方式. 1.Marketplace QuickInstall    ...

  10. JDBC 获取自动生成的主键

    为什么需要获取自动生成的主键 例如: