CS Academy Seven-segment Display

题目链接https://csacademy.com/contest/archive/task/seven-segment-display

题目大意:七段显示器能够显示0-9所有数字,不同的数字需要的发光段的个数不同。现在给出K,要求用恰好K个发光段来组成一个最小的数字,数字不允许有前导0,输出最小的数字。

解题思路:可以发现8需要的段最多-7个,而其余的需要的段数都要少于7,因此对于一个能够整除7的K来说,全部输出8一定是最优的。而对于不能整除7的K来说,每次选择一个数字a,使得a所需要的段数大于等于当前的k%7,将a作为当前的最高位的数字,不停重复下去,知道最后k小于7的时候,选择一个对应的最小的即可。需要注意1和6这两个特殊情况。

代码:

 1 int k, mmp[11] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6};
 2 
 3 void solve(){
 4     if(k == 1){
 5         puts("-1");
 6         return;
 7     }
 8     else if(k == 6){
 9         puts("0");
10         return;
11     }
12     bool fir = true;
13     while(k > 0){
14         int u = k % 7;
15         if(u == 0) {
16             printf("8");
17             k -= 7;
18         }
19         else{
20             for(int i = 0; i <= 9; i++){
21                 if(i == 0 && fir) continue;
22                 if(mmp[i] >= u && k - mmp[i] > 0){
23                     printf("%d", i);
24                     k -= mmp[i];
25                     break;
26                 }
27                 if(mmp[i] == u && k - mmp[i] == 0){
28                     printf("%d", i);
29                     k -= mmp[i];
30                     break;
31                 }
32             }
33         }
34         fir = false;
35     }
36 }
37 int main(){
38     scanf("%d", &k);
39     solve();
40 }

题目:

Seven-segment Display

Time limit: 1000 ms
Memory limit: 128 MB

 

A Seven-segment display (SSD), or seven-segment indicator, is a form of electronic display device for displaying decimal numerals.

Below you can see the representation of every decimal digit.

Different digits use a different number of segments in their representation. For example, 00 uses 66 segments, while 11 uses only 22.

You are given a number KK, what is the smallest non-negative integer that uses exactly KK segments in its representation?

Standard input

The first line contains a single integer KK.

Standard output

If there is no solution print -11.

Otherwise, print the answer on the first line. The number can be quite large and doesn't necessarily fit in a 64 bit integer.

Constraints and notes

  • 1 \leq K \leq 10^51K105​​ 
InputOutput
7
8
10
22
12
28
posted @ 2017-09-16 09:34  EricJeffrey  阅读(278)  评论(0编辑  收藏  举报