英雄会第四届在线编程大赛·线上初赛:带通配符的数
2014-03-22 17:34
323 查看
题目:
给定一个带通配符问号的数W,问号可以代表任意一个一位数字。
再给定一个整数X,和W具有同样的长度。
问有多少个整数符合W的形式并且比X大?
输入格式
多组数据,每组数据两行,第一行是W,第二行是X,它们长度相同。在[1..10]之间.
输出格式
每行一个整数表示结果。
答题说明
输入样例
36?1?8
236428
8?3
910
?
5
输出样例
100
0
4
#include<iostream>
#include<string>
#include <iomanip>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::setprecision;
//计算字符串中通配符的个数
int TPF(string str)
{
int cnt = 0;
for(int i=0; i<str.length(); ++i)
{
if(str[i] == '?')
cnt++;
}
return cnt;
}
//计算10的次方的函数
double power(int n)
{
double res = 1;
while(n--)
res*=10;
return res;
}
int main()
{
string str1 , str2;
double result;
int i , num;
while(cin >> str1 >>str2)
{
result = 0;
for(i=0; i<str1.length();++i)
{
if(str1[i] == '?')
{//字符是通配符
num = '9' - str2[i];
//获取该位置后面通配符的个数
result+=power(TPF(str1.substr(i+1 , str1.length()-i-1)))*num;
continue;
}else{
//字符不是通配符的时候
if(str1[i] > str2[i])
{
result+=power(TPF(str1.substr(i+1 , str1.length()-i-1)));
break;
}else if(str1[i] < str2[i])
{
break;
}else
{
continue;
}
}
}
cout << setprecision(11) << result <<endl;
}
return 0;
}
给定一个带通配符问号的数W,问号可以代表任意一个一位数字。
再给定一个整数X,和W具有同样的长度。
问有多少个整数符合W的形式并且比X大?
输入格式
多组数据,每组数据两行,第一行是W,第二行是X,它们长度相同。在[1..10]之间.
输出格式
每行一个整数表示结果。
答题说明
输入样例
36?1?8
236428
8?3
910
?
5
输出样例
100
0
4
#include<iostream>
#include<string>
#include <iomanip>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::setprecision;
//计算字符串中通配符的个数
int TPF(string str)
{
int cnt = 0;
for(int i=0; i<str.length(); ++i)
{
if(str[i] == '?')
cnt++;
}
return cnt;
}
//计算10的次方的函数
double power(int n)
{
double res = 1;
while(n--)
res*=10;
return res;
}
int main()
{
string str1 , str2;
double result;
int i , num;
while(cin >> str1 >>str2)
{
result = 0;
for(i=0; i<str1.length();++i)
{
if(str1[i] == '?')
{//字符是通配符
num = '9' - str2[i];
//获取该位置后面通配符的个数
result+=power(TPF(str1.substr(i+1 , str1.length()-i-1)))*num;
continue;
}else{
//字符不是通配符的时候
if(str1[i] > str2[i])
{
result+=power(TPF(str1.substr(i+1 , str1.length()-i-1)));
break;
}else if(str1[i] < str2[i])
{
break;
}else
{
continue;
}
}
}
cout << setprecision(11) << result <<endl;
}
return 0;
}
相关文章推荐
- 英雄会第四届在线编程大赛·线上初赛:带通配符的数
- csdn第四届在线编程大赛·线上初赛:带通配符的数
- 英雄会第四届在线编程大赛·线上初赛:带通配符的数
- 英雄会第四届在线编程大赛·线上初赛:带通配符的数
- 英雄会第四届在线编程大赛·线上初赛:带通配符的数
- 英雄会第四届在线编程大赛·线上初赛:带通配符的数
- 英雄会第四届在线编程大赛·线上初赛:带通配符的数
- 英雄会第四届在线编程大赛·线上初赛:带通配符的数
- CSDN英雄会第四届在线编程大赛·线上初赛:带通配符的数
- 【Java】 英雄会第四届在线编程大赛·线上初赛:带通配符的数 Java实现
- 英雄会第四届在线编程大赛·线上初赛:带通配符的数
- 用python实现csdn第四届在线编程大赛·线上初赛:带通配符的数
- csdn 英雄会第四届在线编程大赛·线上初赛:带通配符的数
- 英雄会第四届在线编程大赛·线上初赛:带通配符的数
- 第四届在线编程大赛·线上初赛:带通配符的数-我的解题方法
- CSDN第四届在线编程大赛2014初赛:带通配符的数
- 【思考题】CSDN第四届在线编程大赛2014初赛:带通配符的数
- 【思考题】CSDN第四届在线编程大赛2014初赛:带通配符的数
- 庞果英雄会第二届在线编程大赛·线上初赛:AB数
- 英雄会第二届在线编程大赛·线上初赛:AB数