zoukankan      html  css  js  c++  java
  • hdu 5055 Bob and math problem

    先把各个数字又大到小排列,如果没有前导零并且为奇数,则直接输出。如果有前导零,则输出-1。此外,如果尾数为偶数,则从后向前找到第一个奇数,并把其后面的数一次向前移动,并把该奇数放到尾部。

    值得注意的是当输入为960时,通过这种方法会得到具有前导零的数字。因此最后需要判断进行移动后,首位是否为零。

    代码如下:

     1 #define MAXN 101
     2 #include <stdlib.h>
     3 #include <iostream>
     4 #include <cstdio>
     5 #include <algorithm>
     6 using namespace std;
     7 int arr[MAXN];
     8 int N;
     9 bool comp(int a, int b)
    10 {
    11     return a>b;
    12 }
    13 
    14 void p(int *a)
    15 {
    16     for( int i = 0 ; i < N ; i++ )
    17     {
    18         printf("%d", a[i]);
    19     }
    20     printf("
    ");
    21 }
    22 void solve()
    23 {
    24     sort(arr, arr+N, comp);  
    25     if( arr[N-1] & 1 && arr[0] != 0)
    26     {
    27         p(arr);
    28     }
    29     else if( arr[0] == 0 )
    30     {
    31         printf("%d
    ", -1);
    32     }
    33     else
    34     {
    35         for( int i = N-2 ; i >= 0 ; i-- )
    36         {
    37             if( arr[i] & 1 )
    38             {
    39                 int tmp = arr[i];
    40                 for( int j = i+1 ; j <= N-1 ; j++ )
    41                 {
    42                     arr[j-1] = arr[j];
    43                 }
    44                 arr[N-1] = tmp;
    45                 break;
    46             }
    47         }
    48         if( arr[N-1] & 1  && arr[0] != 0)
    49         {
    50             p(arr);
    51         }
    52         else
    53         {
    54             printf("%d
    ", -1);
    55         }
    56     }
    57 }
    58 int main(int argc, char *argv[])
    59 {
    60     while( scanf("%d", &N ) != EOF)
    61     {
    62         for( int i = 0 ; i < N ; i++ )
    63         {
    64             scanf("%d", &arr[i]);
    65         }
    66         solve();
    67     }
    68     return 0;
    69 }
  • 相关阅读:
    接上一篇:(四) 控制反转(IOC)/ 依赖注入(DI)
    日常踩坑-------新手使用idea
    聚集索引和非聚集索引的区别
    mysql锁
    常用算法
    sql join查询语句
    bitmap原理和redis bitmap应用
    nginx反向代理、负载均衡配置
    nginx工作模式
    PHP常用设计模式
  • 原文地址:https://www.cnblogs.com/jostree/p/4000044.html
Copyright © 2011-2022 走看看