zoukankan      html  css  js  c++  java
  • A. Strange Addition 暴力

     
    A. Strange Addition
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Unfortunately, Vasya can only sum pairs of integers (ab), such that for any decimal place at least one number has digit 0 in this place. For example, Vasya can sum numbers 505 and 50, but he cannot sum 1 and 4.

    Vasya has a set of k distinct non-negative integers d1, d2, ..., dk.

    Vasya wants to choose some integers from this set so that he could sum any two chosen numbers. What maximal number of integers can he choose in the required manner?

    Input

    The first input line contains integer k (1 ≤ k ≤ 100) — the number of integers.

    The second line contains k distinct space-separated integers d1, d2, ..., dk (0 ≤ di ≤ 100).

    Output

    In the first line print a single integer n the maximum number of the chosen integers. In the second line print n distinct non-negative integers — the required integers.

    If there are multiple solutions, print any of them. You can print the numbers in any order.

    Sample test(s)
    input
    4
    100 10 1 0
    output
    4
    0 1 10 100
    input
    3
    2 70 3
    output
    2
    2 70

     答案最多4个最少1个



    const int maxn = 30000;
    int d[maxn];
    int vis[4];
    bool ok()
    {
    repf(i,0,2)
    if(vis[i]) return false;
    return true;
    }
    int main()
    {
    //freopen("in.txt","r",stdin);
    int n;
    while(cin>>n)
    {
    int ans = 0;
    repf(i,1,n)
    {
    scanf("%d",&d[i]);
    if(n>1 && d[i] == 0) ans++;
    }
    if(n == 1)
    {
    cout<<1<<endl;
    cout<<d[1]<<endl;
    continue;
    }
    int a = -1;
    int b = -1;
    int c = -1;
    int tag = 0;
    repf(i,1,n)
    {
    repf(j,i+1,n)
    {
    repf(k,0,2) vis[k] = 1;
    if(d[i] == 0 || d[j] == 0) continue;
    int t = 3;
    int tt = d[i];
    while(t > 0)
    {
    int tmp = tt%10;
    tt/=10;
    if(tmp == 0)
    vis[t - 1] = 0;
    t--;
    }

    tt = d[j];
    t = 3;
    while(t > 0)
    {
    int tmp = tt%10;
    tt/=10;
    if(tmp == 0)
    vis[t - 1] = 0;
    t--;
    }
    if(ok())
    {
    a = d[i];
    b = d[j];
    tag = 1;
    }
    }
    }
    if(tag == 0)
    {
    if(ans)
    {
    cout<<2<<endl;
    int kk;
    repf(i,1,n)
    if(d[i] != 0)
    kk = d[i];
    cout<<0<<" "<<kk<<endl;
    }else
    {
    cout<<1<<endl;
    cout<<d[1]<<endl;
    }
    }else
    {
    int flag = 0;
    //clr(vis)
    repf(i,1,n)
    repf(j,i+1,n)
    repf(k,j+1,n)
    {
    if(d[i] == 0 || d[j] == 0 || d[k] == 0) continue;
    int t = 3;
    int tt = d[i];
    clr(vis);
    while(t > 0)
    {
    int tmp = tt%10;
    tt/=10;
    if(tmp)
    {
    vis[t - 1]++;
    }
    t--;
    }
    t = 3;
    tt = d[j];
    while(t > 0)
    {
    int tmp = tt%10;
    tt/=10;
    if(tmp)
    vis[t - 1]++;
    t--;
    }
    t = 3;
    tt = d[k];
    while(t > 0)
    {
    int tmp = tt%10;
    tt/=10;
    if(tmp)
    vis[t - 1]++;
    t--;
    }
    if(vis[0] == 1&& vis[1] == 1 && vis[2] == 1)
    {
    a = d[i];
    b = d[j];
    c = d[k];
    flag = 1;
    }
    }
    if(flag)
    {
    if(ans)
    {
    cout<<4<<endl;
    printf("%d %d %d %d ",0,a,b,c);
    }else
    {
    cout<<3<<endl;
    printf("%d %d %d ",a,b,c);
    }
    }else
    {
    if(ans)
    {
    cout<<3<<endl;
    printf("%d %d %d ",0,a,b);
    }else
    {
    cout<<2<<endl;
    printf("%d %d ",a,b);
    }
    }
    }
    }
    return 0;
    }

  • 相关阅读:
    dapper+linq+json+ztree构建树
    ubuntu,从一个新用户,要转到新用户的命令行操作
    OC本学习笔记Foundatio框架集
    无法Debug SQL: Unable to start T-SQL Debugging. Could not attach to SQL Server process on
    Struts2 + uploadify 多文件上传完整的例子!
    How use Instruments and display the console in Command Lines applications
    Android ActionBar详解(二):ActionBar实现Tabs标签以及下拉导航
    Android ActionBar详解(一):ActionBar概述及其创建
    Android Fragment详解(六):Fragement示例
    Android Fragment详解(五):Fragment与Activity通讯
  • 原文地址:https://www.cnblogs.com/DreamHighWithMe/p/3441876.html
Copyright © 2011-2022 走看看