今天写了一下英雄会的初赛题。明明正确不知道为什么显示失败。。。郁闷
题目:
//英雄会第四届在线编程大赛·线上初赛:带通配符的数
#include "stdio.h"
#include "iostream"
#include <vector>
#include <string>
#include <math.h>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::string;
#define WILD_CHAR '?'
vector<string> unknown;
vector<string> known;
string unknown_str;
string known_str;
void Init(){
unknown.push_back("36?1?8");
known.push_back("236428");
unknown.push_back("8?3");
known.push_back("910");
unknown.push_back("?");
known.push_back("5");
}
void InitData(){
cout<<"Input(0 to end):\n";
string input;
int index = 0;
while( cin>>input ){
if( input.compare("0") == 0)
break;
if( index++ % 2){
known.push_back(input);
}else{
unknown.push_back(input);
}
}
cout<<endl;
}
int getNumOfWild(string &sourceStr){
int count = 0;
char ch;
for(size_t i = 0; i < sourceStr.size(); i++){
ch = sourceStr[i];
if( ch == WILD_CHAR)
count++;
}
return count;
}
int getAllResult(int num_wildchar, int index){
if( num_wildchar == 0 )
return 0;
int result = 0;
char tmpInt;
int unknown_int_j;
int known_int_j;
tmpInt = known_str[index];
known_int_j = atoi(&tmpInt);
tmpInt = unknown_str[index];
if( tmpInt != WILD_CHAR){
unknown_int_j = atoi(&tmpInt);
}else{
num_wildchar--;
result += (9 - known_int_j)*((int)pow((float)10, (float)num_wildchar));
unknown_int_j = known_int_j;
}
if( unknown_int_j == known_int_j){
return result + getAllResult( num_wildchar, ++index);
}else if( unknown_int_j > known_int_j ){
return result + (int)pow((float)10, (float)num_wildchar);
}else{
return result;
}
}
void csdn_str(){
InitData();
//Init();
for( size_t i = 0; i < unknown.size(); i++){
unknown_str = unknown[i];
known_str = known[i];
int result = 0;
int num_wildchar = getNumOfWild( unknown_str);
result = getAllResult(num_wildchar, 0);
cout<<result<<endl;
}
}
int main(){
csdn_str();
return 0;
}