OpenJudge / Poj 1102 LC-Display C++

链接地址:

Openjudge:http://bailian.openjudge.cn/practice/1102

Poj:http://poj.org/problem?id=1102

题目:

总时间限制:
1000ms
内存限制:
65536kB
描述
A friend of you has just bought a new computer. Until now, the most powerful computer he ever used has been a pocket calculator. Now, looking at his new computer, he is a bit disappointed, because he liked the LC-display of his calculator so much. So you decide to write a program that displays numbers in an LC-display-like style on his computer.
输入
The input contains several lines, one for each number to be displayed. Each line contains two integers s, n (1 <= s <= 10, 0 <= n <= 99 999 999), where n is the number to be displayed and s is the size in which it shall be displayed.

The input file will be terminated by a line containing two zeros. This line should not be processed.
输出
Output the numbers given in the input file in an LC-display-style using s "-" signs for the horizontal segments and s "|" signs for the vertical ones. Each digit occupies exactly s+2 columns and 2s+3 rows. (Be sure to fill all the white space occupied by the digits with blanks, also for the last digit.) There has to be exactly one column of blanks between two digits.

Output a blank line after each number. (You will find a sample of each digit in the sample output.)
样例输入
2 12345
3 67890
0 0
样例输出
      --   --        -- 
   |    |    | |  | | 
   |    |    | |  | | 
      --   --   --   -- 
   | |       |    |    |
   | |       |    |    |
      --   --        -- 

 ---   ---   ---   ---   --- 
|         | |   | |   | |   |
|         | |   | |   | |   |
|         | |   | |   | |   |
 ---         ---   --- 
|   |     | |   |     | |   |
|   |     | |   |     | |   |
|   |     | |   |     | |   |
 ---         ---   ---   ---
来源

思路:

模拟题,使用bieset保存结果,用iterator迭代数组,注意数字间有空格,每一行之间有空行

代码:

  1 #include <iostream>
  2 #include <bitset>
  3 #include <string>
  4 #include <sstream>
  5 using namespace std;
  6 
  7 
  8 int main()
  9 {
 10     string str0 = "1110111";
 11     string str1 = "0100100";
 12     string str2 = "1011101";
 13     string str3 = "1101101";
 14     string str4 = "0101110";
 15     string str5 = "1101011";
 16     string str6 = "1111011";
 17     string str7 = "0100101";
 18     string str8 = "1111111";
 19     string str9 = "1101111";
 20 
 21     string str = str9 + str8 + str7 + str6 + str5 + str4 + str3 + str2 + str1 + str0;
 22 
 23     bitset<70> bs(str);
 24 
 25     int s,n;
 26     cin>>s>>n;
 27     int flag;
 28     string::iterator iter;
 29     int i;
 30     while(s!=0 || n != 0)
 31     {
 32         ostringstream s1;
 33         s1<<n;
 34         string str_n = s1.str();
 35 
 36         flag = 0;
 37         for(iter = str_n.begin(); iter != str_n.end(); ++iter)
 38         {
 39             if(flag) cout<<" ";
 40             else flag = 1;
 41             int num = (*iter) - '0';
 42             cout<<" ";
 43             for(int i = 0; i < s; i++)
 44             {
 45                 if(bs[num * 7]) cout<<"-";
 46                 else cout<<" ";
 47             }
 48             cout<<" ";
 49         }
 50         cout<<endl;
 51 
 52         
 53         for(i = 0; i < s; i++)
 54         {
 55             flag = 0;
 56             for(iter = str_n.begin(); iter != str_n.end(); ++iter)
 57             {
 58                 if(flag) cout<<" ";
 59                 else flag = 1;
 60                 int num = (*iter) - '0';
 61                 if(bs[num * 7 + 1]) cout<<"|";
 62                 else cout<<" ";
 63                 for(int j = 0; j < s; j++) cout<<" ";
 64                 if(bs[num * 7 + 2]) cout<<"|";
 65                 else cout<<" ";
 66             }
 67             cout<<endl;
 68         }
 69 
 70         flag = 0;
 71         for(iter = str_n.begin(); iter != str_n.end(); ++iter)
 72         {
 73             if(flag) cout<<" ";
 74             else flag = 1;
 75             int num = (*iter) - '0';
 76             //step1
 77             cout<<" ";
 78             for(i = 0; i < s; i++)
 79             {
 80                 if(bs[num * 7 + 3]) cout<<"-";
 81                 else cout<<" ";
 82             }
 83             cout<<" ";
 84         }
 85         cout<<endl;
 86 
 87         for(i = 0; i < s; i++)
 88         {
 89             flag = 0;
 90             for(iter = str_n.begin(); iter != str_n.end(); ++iter)
 91             {
 92                 if(flag) cout<<" ";
 93                 else flag = 1;
 94                 int num = (*iter) - '0';
 95                 if(bs[num * 7 + 4]) cout<<"|";
 96                 else cout<<" ";
 97                 for(int j = 0; j < s; j++) cout<<" ";
 98                 if(bs[num * 7 + 5]) cout<<"|";
 99                 else cout<<" ";
100             }
101             cout<<endl;
102         }
103 
104         flag = 0;
105         for(string::iterator iter = str_n.begin(); iter != str_n.end(); ++iter)
106         {
107             if(flag) cout<<" ";
108             else flag = 1;
109             int num = (*iter) - '0';
110             //step1
111             cout<<" ";
112             for(i = 0; i < s; i++)
113             {
114                 if(bs[num * 7 + 6]) cout<<"-";
115                 else cout<<" ";
116             }
117             cout<<" ";
118         }
119         cout<<endl;/**/
120         cout<<endl;
121         cin>>s>>n;
122     }
123     return 0;
124 }
原文地址:https://www.cnblogs.com/mobileliker/p/3510870.html