工大后院

 找回密码
 加入后院

扫一扫,访问微社区

QQ登录

只需一步,快速开始

搜索
查看: 1278|回复: 14

[简单题]zju_2481

[复制链接]
发表于 2005-7-20 18:20 | 显示全部楼层 |阅读模式
Unique Ascending Array

--------------------------------------------------------------------------------

Time limit: 1 Seconds   Memory limit: 32768K   
Total Submit: 1086   Accepted Submit: 501   

--------------------------------------------------------------------------------

Given an array of integers A[N], you are asked to decide the shortest array of integers B[M], such that the following two conditions hold.

For all integers 0 <= i < N, there exists an integer 0 <= j < M, such that A == B[j]
For all integers 0 =< i < j < M, we have B < B[j]
Notice that for each array A[] a unique array B[] exists.


Input

The input consists of several test cases. For each test case, an integer N (1 <= N <= 100) is given, followed by N integers A[0], A[1], ..., A[N - 1] in a line. A line containing only a zero indicates the end of input.


Output

For each test case in the input, output the array B in one line. There should be exactly one space between the numbers, and there should be no initial or trailing spaces.


Sample Input

8 1 2 3 4 5 6 7 8
8 8 7 6 5 4 3 2 1
8 1 3 2 3 1 2 3 1
0


Sample Output

1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3



Author: SHI, Xiaohan



很简单,不过我不过,会不会数据很大呢?
发表于 2005-7-20 19:12 | 显示全部楼层
int search(int array[],int number,int post){
        for(int i=post;i>=0;i--){
                if(number<array) continue;
                else if(number==array) {
                        return -1;
                }
                else if(number>array) {
                        return i+1;
                }
        }
        return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
        int count; // Size of A
        while(scanf(\"%d\",&count),count!=0){
                int b[1000];
                int bi=-1;
                int location;
                int element;

                for(int i=0;i<count;i++){
                        scanf(\"%d\",&element); //读入一个元素
                        int location=search(b,element,bi);
                        if(location==-1)continue;

                        for(int j=bi+1;j>=location;j--){
                                b[j]=b[j-1];
                        }
                        b[location]=element;
                        bi++;
                }

                b[bi+1]=-1;

                int index=0;

                while(b[index]!=-1){
                        printf(\"%d \",b[index]);
                        index++;
                }
                printf(\"\\n\");
        }
}
回复

使用道具 举报

 楼主| 发表于 2005-7-20 19:51 | 显示全部楼层
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
        bool appear[65536];
        ifstream cin(\"in.txt\");
        ofstream cout(\"out.txt\");
        unsigned long times,i,k,j,max;
        while(1)
        {
                cin >> times;
                max = 0;
                for(k = 0;k< 65536;k++)
                {
                        appear[k] = false;
                }
                if(times == 0)
                {
                        break;
                }
                else
                {
                        for(j = 0;j < times;j++)
                        {
                                cin >> i;
                                appear = true;
                                if(i > max)
                                {
                                        max = i;
                                }
                        }
                }
                for(k = 0;k<=max;k++)
                {
                        if(k == max)
                        {
                                cout <<k<<endl;
                        }
                        else if(appear[k])
                        {
                                cout <<k<<\" \";
                        }
                }
               
        }
}
我就真的不知道错哪里了。。。。
回复

使用道具 举报

发表于 2005-7-20 22:01 | 显示全部楼层
这道题的意思说,在一个序列中,找出满足以下条件的序列B
序列B 的数字都要在A中出现.
序列B中的数字由小到大

可见,序列B是序列A中的数字的一个排序,但是不输出相同的数字
回复

使用道具 举报

发表于 2005-7-20 22:02 | 显示全部楼层
A了
#include<stdio.h>
int main()
{
        int shu[101],i,j,k,n;
        freopen(\"in.txt\",\"r\",stdin);
        while(scanf(\"%d\",&n),n)
        {
                for (i=0;i<n;i++) scanf(\"%d\",&shu);
                for (i=0;i<n;i++)//排序
                        for (j=i+1;j<n;j++)
                                if(shu[j]<shu)
                                {
                                        k=shu[j];
                                        shu[j]=shu;
                                        shu=k;
                                }
                printf(\"%d\",shu[0]);
                for (i=1;i<n;i++) if(shu>shu[i-1])
                        printf(\" %d\",shu);
                printf(\"\\n\");


        }

        return 0;
}
回复

使用道具 举报

发表于 2005-7-21 18:54 | 显示全部楼层
小康的代码怎么比大象的少那么多???晕!!!!
回复

使用道具 举报

发表于 2005-7-23 20:41 | 显示全部楼层
不知道这样对不对?

#include<stdio.h>
main()
{
int a[100],b[100],i,j,k,n;
while(scanf(\"%d\",&n),n)
   {
    for(i=0;i<n;i++)
      scanf(\"%d\",&a);
    for(i=0;i<n;i++)
       {for(j=i+1;j<n;j++)
          if(a[j]<a)
            {
             k=a[j];
             a[j]=a;
             a=k;
            }
            b=a;
          }
printf(\"%d\",b[0]);
for(i=1;i<n;i++)
   {
    if(b==b[i-1])continue;
    printf(\" %d\",b);
   }
printf(\"\\n\");
   }
}
回复

使用道具 举报

发表于 2005-7-24 08:09 | 显示全部楼层
两个程序基本都一样
回复

使用道具 举报

发表于 2005-7-24 08:58 | 显示全部楼层
你仔细看清楚了
回复

使用道具 举报

发表于 2005-7-24 08:59 | 显示全部楼层
你仔细看清楚了,老大
回复

使用道具 举报

发表于 2005-7-24 12:09 | 显示全部楼层
你个数组B好像不用也行吧,还是觉得思路程序都一样
回复

使用道具 举报

发表于 2005-7-25 01:16 | 显示全部楼层
嘻…………但它要求是有B数组的,不用就不符合题目要求了
小康是不是师兄来的?
回复

使用道具 举报

发表于 2005-7-25 08:55 | 显示全部楼层
你误解题目意思了.数组B,只是题目为了说明意思的.实际编程,你只要输出的答案完全正确就行了.程序要求内存越少,速度越快,答案对,不需要做出错出来.这样就AC
回复

使用道具 举报

发表于 2005-7-25 10:09 | 显示全部楼层
有道理,不愧是高手来的
回复

使用道具 举报

发表于 2005-7-25 19:45 | 显示全部楼层
你有兴趣加到做题群来一起做题
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 加入后院

本版积分规则

QQ|Archiver|手机版|小黑屋|广告业务Q|工大后院 ( 粤ICP备10013660号 )

GMT+8, 2025-5-13 18:04

Powered by Discuz! X3.5

Copyright © 2001-2024 Tencent Cloud.

快速回复 返回顶部 返回列表