0
0
0
1. 云栖社区>
2. 博客>
3. 正文

## 题一 电子数字

this_is_bill 2016-03-19 14:35:00 浏览988

3
3 50
3 1
1 4 5
1 5 6 7
4 100
1 2 3
4 5
6
7
1 1
7

3
0
1

# 二、code

``````#include <iostream>
#include <string>
#include <math.h>

using namespace std;

int res[12][12];
int digitCnt[6] ={0}; //该位可行计数
int miniCnt[6] = {0}; //该位比之小的
int equalCnt[6] = {0}; //是否和所比数当位有相等

int manyDigit(int a) {

int cnt = 1;
while ( a/=10 ) {
++cnt;
}

return cnt;
}

//get特定位数
int getDigit(int a, int n) {

int p = pow(10,n-1);
return (a/p)%10;
}

void work(int &resCnt,int deep,int n) {

if(deep == n) { //递归结束条件
return;
}

if ( equalCnt[deep] ) { //如果可相等

work(resCnt,deep+1,n);
int tmpCnt = miniCnt[deep];
for (int i=deep+1;i<n;++i)
tmpCnt*=digitCnt[i];

resCnt += tmpCnt;
}else {

int tmpCnt = miniCnt[deep];
for (int i=deep+1;i<n;++i)
tmpCnt*=digitCnt[i];

resCnt += tmpCnt;
}
}

int main()
{
freopen("input.txt","r",stdin);

int LED[10][8]={
0,1,1,1,0,1,1,1 //0
,0,0,0,1,0,0,1,0 //1
,0,1,0,1,1,1,0,1 //2
,0,1,0,1,1,0,1,1 //3
,0,0,1,1,1,0,1,0 //4
,0,1,1,0,1,0,1,1 //5
,0,1,1,0,1,1,1,1 //6
,0,1,0,1,0,0,1,0 //7
,0,1,1,1,1,1,1,1 //8
,0,1,1,1,1,0,1,1 //9
};

int T;
cin>>T;

start:
while (T--) {

//init
for (int i=0;i<12;++i) {
for (int j=0;j<12;++j) {
res[i][j] = 1;
}
}

memset(digitCnt,0,sizeof(digitCnt));
memset(miniCnt,0,sizeof(miniCnt));
memset(equalCnt,0,sizeof(equalCnt));

int K;
cin>>K;
int N;
cin>>N;

getchar();
for (int i=0;i<K;++i) {

while (1){
char tmp;
tmp = getchar();

if(tmp == ' ')
continue;

if(tmp=='\n')
break;

for (int j=0;j<=9;++j) { //0-9个数字

if( 0 == LED[j][tmp-48] )
//与某个数字j不吻合
res[i][j] = 0; //此位不能为j
}
}
}

//输入完毕，得出res可以为的数字
//判断N是几位数
int highdigit = manyDigit(N);

//前导0
for (int i=0;i<K-highdigit;++i) {
if( res[i][0] != 1 ) {
//错误
cout<<"0"<<endl;
goto start;
}
}

//N最高位，是否比之小的
int tmpDigit = getDigit(N,highdigit);
int tmpFlag = 0;
for (int i=0;i<=tmpDigit;++i) {
if( res[K-highdigit][i] == 1 ) {
tmpFlag = 1;
break;
}
}
if (tmpFlag == 0) {
//错误
cout<<"0"<<endl;
goto start;
}

for (int i=K-highdigit;i<K;++i) {

for(int j=0;j<=9;++j) {
if(res[i][j] == 1) {
digitCnt[i]++;
if( j<getDigit(N,K-i) )
miniCnt[i]++;

else if(j == getDigit(N,K-i))
equalCnt[i]++;
}
}
}

//计算个数
int resCnt = 0;
int deep = K-highdigit;
work(resCnt,deep,K);

cout<<resCnt<<endl;
//system("pause");
}//while(T--)

return 0;
}``````

``````#include <iostream>
#include <vector>
using namespace std;

void getNum(vector<vector<int>> numList, int deep, int temp, vector<int> &totalNums);

int main(void)
{
int deng[10][7] = { {1,1,1,0,1,1,1},//0
{0,0,1,0,0,1,0},
{1,0,1,1,1,0,1},
{1,0,1,1,0,1,1},
{0,1,1,1,0,1,0},
{1,1,0,1,0,1,1},
{1,1,0,1,1,1,1},
{1,0,1,0,0,1,0},//7
{1,1,1,1,1,1,1},
{1,1,1,1,0,1,1}//9
};

int S_test; //测试点的个数
int K_LED;    //K个LED数码管
int compareNum; //给定每个测试点比较的数

cin>>S_test;

int *endAnswer = new int[S_test]; //输出结果

for(int i=0;i<S_test;i++) //测试用例
{
cin>>K_LED>>compareNum; //K个LED数码管， 测试的数字
cin.ignore(); //忽略上一行的换行符！！！

vector<vector<int>>num_K_LED; //存储每个LED可能的数字

for (int j=0;j<K_LED;j++) //多少LED数码管 就多少行
{
char eachLine[100]; //每行表示对应数码管已点亮的二极管的情况

cin.getline(eachLine,100);//cin>>eachLine;      //每行

int servenLight[7] = {0};   //单个LED的7个指示灯哪个亮
int idx = 0; //数组下标
for(int k=0;k<strlen(eachLine);k++) //每行转为数字
{
if(eachLine[k]!=' ')
{
servenLight[idx] = int(eachLine[k] - '0'); //取数字
idx++;
if(idx == 7)
{
break;
}
}
}

vector<int > num_eachLED; //记录单个LED中servenLight[7]中亮的可能是哪些数字？
for (int deng_i = 0; deng_i <10;deng_i ++) //循环0-9,符合的加入num_eachLED
{
bool rs = true;
int idx = 0;

while (servenLight[idx] !=0)  //所有亮的指示灯都在 这个数字中
{
if(deng[deng_i][ servenLight[idx] -1] != 1)
{
rs = false;
break;
}
idx++;
}
if(rs)
{
num_eachLED.push_back(deng_i);
}
}

num_K_LED.push_back(num_eachLED);
}

vector<int> totalNums; //记录所有组合出来的数字
int deep =0;
int temp =0;
getNum(num_K_LED, deep, temp, totalNums);

int count = 0;
for (int totalNums_i = 0; totalNums_i < totalNums.size();totalNums_i++)
{
if(compareNum >= totalNums[totalNums_i])
{
count++;
}
}
}

for(int i = 0; i<S_test; i++)
{
}

//system("pause");
return 0;
}

void getNum(vector<vector<int>> numList, int deep, int temp, vector<int> &totalNums)
{
if(deep < numList.size()-1)
{
for(int i=0; i < numList[deep].size(); i++)
{
int newInt = temp + numList[deep][i] * pow(10,numList.size()- deep -1);
getNum(numList, deep+1, newInt,totalNums);
}
}
else if(deep == numList.size()-1)
{
for(int i=0; i < numList[deep].size(); i++)
{
int newInt = temp + numList[deep][i] * pow(10,numList.size()- deep -1);
totalNums.push_back(newInt);
}
}
}
``````

this_is_bill
+ 关注