zoukankan      html  css  js  c++  java
  • Codeforces Gym

    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Using at most 7 matchsticks, you can draw any of the 10 digits as in the following picture:

    The picture shows how many sticks you need to draw each of the digits.

    Zaytoonah has a number that consists of N digits. She wants to move some sticks (zero or more) to maximize the number. Note that she doesn’t want to remove any of the sticks, she will only move them from one place to another within the N digits. She also doesn’t want to add new digits as N is her lucky number.

    Can you help Zaytoonah maximize her number?

    Input

    The first line of input contains a single integer T, the number of test cases.

    Each test case contains a single integer N (1 ≤ N ≤ 105), followed by a space, then N digits that represent the number Zaytoonah currently has.

    Output

    For each test case, print on a single line the maximum number Zaytoonah can get.

    Example
    input
    3
    1 3
    3 512
    3 079
    output
    5
    977
    997
    题意:略。
    方法:贪心,先求出总的火柴数,再从最大数字开始贪,最后一个单独输出。
    代码:
    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int N=1e5+5;
    int a[10]={
        6,2,5,5,4,5,6,3,7,6    
    };
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            int n;
            char s[N];
            int sum=0;
            scanf("%d %s",&n,s);
            for(int i=0;i<n;i++)
            {
                sum+=a[s[i]-'0'];
            }
            for(int i=1;i<n;i++)
            {
                for(int j=9;j>=0;j--)
                {
                    int temp=sum-a[j];
                    if(temp>=2*(n-i)&&temp<=7*(n-i))
                    {
                        cout<<j;
                        sum-=a[j];
                        break;
                    }
                }
            } 
            for(int i=9;i>=0;i--)
            {
                if(sum==a[i])
                {
                    cout<<i<<endl;
                    break;
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    Head First Servlets & JSP 学习笔记 第七章 —— 作为JSP
    Head First Servlets & JSP 学习笔记 第六章 —— 会话状态
    八大基本排序--基数排序
    Stack类常用api
    3.从尾到头打印链表
    八大基本排序--归并排序
    八大基本排序--选择排序
    八大基本排序--希尔排序
    八大基本排序--插入排序
    八大基本排序--冒泡排序
  • 原文地址:https://www.cnblogs.com/widsom/p/6739985.html
Copyright © 2011-2022 走看看