hnust 1607 Sharkovski’s Ordering(规律,排序)

传送门:http://acm.hnust.edu.cn/JudgeOnline/problem.php?cid=1434&pid=6

时间限制: 1 Sec 内存限制: 128 MB
题目描述
In a 1964 paper on continuous mappings of the reals into the reals, Alexandr Sharkovski used the
following ordering of the positive integers:
3 ◄ 5 ◄ 7 ◄ 9 ◄ … ◄ 3·2 ◄ 5·2 ◄ 7·2 ◄ … ◄ 3·22 ◄ 5·22 ◄ … ◄ 23 ◄ 22 ◄ 2 ◄ 1
As Ciesielski and Pogoda (2008) describe it:
“First come the odd numbers, beginning with 3, arranged in increasing order. This sequence is
repeated with each odd integer multiplied by 2. The initial sequence is again repeated with each odd
integer multiplied by 22, and so on. The terminal sequence consists of the nonnegative powers of 2
arranged in decreasing order (note that 1 = 20).”
Write a program that reads an input containing a list of up to 255 unsigned integers with values less
than or equal to 65,535 (not necessarily distinct) separated by white space and terminated by ‘0’. The
program should display on the screen the numbers arranged in Sharkovski’s ordering in one line. The
numbers in the line are separated exactly by one space.
输入
The input starts with an integer N (1 ≤ N ≤ 255). This is followed by N input cases. Each input case

is a non-empty list of up to 255 unsigned integers (not necessarily distinct) with values not exceeding
65,535. Each pair of numbers is separated by white space. Each input case is terminated by ‘0’.
输出
For every input case, print the required numbers arranged in Sharkovski’s ordering in one line. The

numbers in the line are separated exactly by one space.
样例输入
2
3 0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 1 0
样例输出
3
3 5 7 9 11 13 15 17 19 6 10 14 18 12 16 8 4 2 1 1

分析:暴力大法。。 我不会==(TLE)
从题意中可看出因子2是关联,把因子2除尽,计数排序 输出

#include <cstdio>
#include <iostream>
#include <cstring>
#include <iomanip>
#include <cmath>
#include <algorithm>
using namespace std;
#define mem(a,n) memset(a,n,sizeof(a))
#define pb push_back
typedef long long ll;
const int mod=1e9+7;
const double eps=1e-6;
const int INF=0x3f3f3f3f;
const int N=3e3+5;
struct Node
{
    int x,num;
    bool operator < (const Node& m)const
    {
        if(num!=m.num) return num<m.num;
        else return x<m.x;
    }
} a[N],b[N];
int Pow(int n)
{
    int ans=1;
    for(int i=1;i<=n;i++) ans*=2;
    return ans;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int x,cnt=0,times;
        while(~scanf("%d",&x)&&x)
        {
            a[cnt].x=x;
            times=0;
            if(a[cnt].x%2==0)
            {
                while(a[cnt].x%2==0)
                {
                    a[cnt].x/=2;
                    times++;
                }
            }
            a[cnt].num=times;
            cnt++;
        }
        sort(a,a+cnt);
        int co=0,sum=0;
        bool flag=0;
        for(int i=0; i<cnt; i++)
        {
            if(a[i].x==1) b[co].x=a[i].x,b[co].num=a[i].num,co++;
            else
            {
                printf("%d",a[i].x*Pow(a[i].num));
                sum++;
                if(sum!=cnt) printf(" ");
                else
                {
                    flag=1;
                    puts("");
                    break;
                }
            }
        }
        if(!flag)
        {
            sort(b,b+co);
            for(int i=co-1; i>0; i--) printf("%d ",Pow(b[i].num));
            printf("%d\n",Pow(b[0].num));
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值