杭电oj 1106

排序

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 74104    Accepted Submission(s): 22754

Problem Description

输入一行数字,如果我们把这行数字中的‘5’都看成空格,那么就得到一行用空格分割的若干非负整数(可能有些整数以‘0’开头,这些头部的‘0’应该被忽略掉,除非这个整数就是由若干个‘0’组成的,这时这个整数就是0)。

你的任务是:对这些分割得到的整数,依从小到大的顺序排序输出。

Input

输入包含多组测试用例,每组输入数据只有一行数字(数字之间没有空格),这行数字的长度不大于1000。

输入数据保证:分割得到的非负整数不会大于100000000;输入数据不可能全由‘5’组成。

Output

对于每个测试用例,输出分割得到的整数排序的结果,相邻的两个整数之间用一个空格分开,每组输出占一行。

Sample Input

0051231232050775

Sample Output

0 77 12312320

这是我刷的杭电oj的第一题,我开始用的是c++,然后还有很多取巧的地方,结果是wa,害的我差点以为是杭电不支持vector这些库,但其实人家是支持的,是我忘记了排除中间连续出现5的情况,害的我是用两种方法解决的,但是这两种方法的思想是一样的,用的东西不一样而已,下面是我的代码,因为我比较懒,代码思想是一样的,只写一个的注释。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;
int main(){
    string a;//a是输入的字符串
    while(cin>>a){
        vector<long>b;//b是用来保存分割得到的整数的。
        string c;//c是临时变量,用来保存分割中的整数的
        int j;
        for(j=0;a[j]==‘5‘;j++);//排除开头的连续‘5’
        for(int i=j;i<a.length();i++){
            if(a[i]!=‘5‘)c+=a[i];
            else if(a[i-1]!=‘5‘){//如果是连续的5则不保存多余的数。
                b.push_back(atol(c.c_str()));
                c="";
            }
        }
        if(a[a.length()-1]!=‘5‘)//如果结尾不是‘5’,还需要将最后一个分割的整数保存下来
        b.push_back(atol(c.c_str()));
        sort(b.begin(),b.end());//排序
        cout<<b[0];
        for(int i=1;i<b.size();i++){
            cout<<" "<<b[i];
        }
        cout<<"\n";
    }
} 
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
using namespace std;
int main(){
    char a[1001] ;
    long b[1000];
    int k=0;
    while(scanf("%s",a)!=EOF){
        long s=0;
        int j;
        for(j=0;a[j]==‘5‘;j++);
        int i;
        for(i=j;a[i]!=‘\0‘;i++){
            if(a[i]!=‘5‘)
            {
                s=s*10+a[i]-‘0‘;
            }
            else if(a[i-1]!=‘5‘){
                b[k++]=s;
                s=0;
            }
        }
        if(a[--i]!=‘5‘)
        b[k++]=s;
        s=0;
        sort(b,b+k);
        for(int i=0;i<k;i++){
            if(i==0)cout<<b[i];
            else cout<<" "<<b[i];
        }
        cout<<"\n";
        k=0;
    }
} 

原文地址:https://www.cnblogs.com/fromzore/p/9748284.html

时间: 2024-08-29 06:38:02

杭电oj 1106的相关文章

【转】对于杭电OJ题目的分类

[好像博客园不能直接转载,所以我复制过来了..] 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDI

杭电oj 1009 FatMouse&#39; Trade

Tips:本题采用贪心算法,类似于背包问题,关键在于读入数据之后,将数据按 J[i]/F[i] 从大到小排列即可. 1 /**本程序主要采用贪心算法思想,类似于背包问题*/ 2 #include<stdio.h> 3 #include<string.h> 4 int main() 5 { 6 int M,N; 7 while(scanf("%d %d",&M,&N)) 8 { 9 if(M == -1 && N == -1) 10

异或^符号在C/C++中的使用 &amp; 杭电oj 2095

异或^符号,在平时的学习时可能遇到的不多,不过有时使用得当可以发挥意想不到的结果. 值得注意的是,异或运算是建立在二进制基础上的,所有运算过程都是按位异或(即相同为0,不同为1,也称模二加),得到最终结果. 特点:任何数和0异或都等于它本身;两个相同的数异或后的结果是0: 举例如下: int a = 4 =100(二进制) int b = 3 =011(二进制) int c = a^b = 111 = 7: 下面就^常用应用做个介绍: 1. 在一排数中找到独一无二的一个数 本例启发来自于杭电oj

杭电OJ(HDU)-ACMSteps-Chapter Two-《An Easy Task》《Buildings》《decimal system》《Vowel Counting》

http://acm.hdu.edu.cn/game/entry/problem/list.php?chapterid=1§ionid=2 1.2.5 #include<stdio.h> /* 题意:找闰年. if((i%4==0 && i%100!=0) || i%400==0)count++; 3 2005 25 1855 12 2004 10000 2108 1904 43236 */ int main() { int t,y,n; int i,count=0; whil

杭电OJ(HDU)-ACMSteps-Chapter Three-《FatMouse&amp;#39; Trade》《今年暑假不AC》《排名》《开门人和关门人》

http://acm.hdu.edu.cn/game/entry/problem/list.php?chapterid=1§ionid=3 1.3.1 FatMouse' Trade #include <algorithm> /* 题意:价值/代价的比值来排序,买比值大的. Sample Input 5 3 7 2 4 3 5 2 20 3 25 18 24 15 15 10 -1 -1 Sample Output 13.333 31.500 */ #include<stdio.h>

杭电oj 1069 Monkey and Banana

Monkey and Banana Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6866 Accepted Submission(s): 3516 Problem Description A group of researchers are designing an experiment to test the IQ of a monkey

杭电OJ(HDU)-ACM Steps-Chapter Two-《Biker&#39;s Trip Odometer》《Climbing Worm》《hide handkerchief》《Nasty Hac》

1.2.1 Biker's Trip Odometer #include<stdio.h> #include<math.h> const double PI=acos(-1.0); /* 计算题,根据公式做就行,PI*d*r/(12*5280);res1/t*3600; Sample Input 26 1000 5 27.25 873234 3000 26 0 1000 Sample Output Trip #1: 1.29 928.20 Trip #2: 1179.86 1415

杭电OJ(HDU)-ACMSteps-Chapter Three-《FatMouse&#39; Trade》《今年暑假不AC》《排名》《开门人和关门人》

http://acm.hdu.edu.cn/game/entry/problem/list.php?chapterid=1§ionid=3 1.3.1 FatMouse' Trade #include <algorithm> /* 题意:价值/代价的比值来排序,买比值大的. Sample Input 5 3 7 2 4 3 5 2 20 3 25 18 24 15 15 10 -1 -1 Sample Output 13.333 31.500 */ #include<stdio.h>

杭电oj 1002

1 #include <iostream> 2 #include <algorithm> 3 using namespace std; 4 int nCases; 5 int m[1001], n[1001]; 6 char a[1001], b[1001]; 7 int main() 8 { 9 scanf("%d", &nCases); 10 for(int i = 1; i <= nCases; ++i) 11 { 12 memset(m,