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;
    }

  • 相关阅读:
    Herny
    机器学习No.4
    机器学习No.3
    机器学习No.2
    机器学习No.1
    算法第五章上机实践报告
    算法第五章作业
    算法第四章实践报告
    算法第四章作业
    算法第三章作业
  • 原文地址:https://www.cnblogs.com/DreamHighWithMe/p/3441876.html
Copyright © 2011-2022 走看看