有些人很迷信数字,比如带“4”的数字,认为和“死”谐音,就觉得不吉利。
虽然这些说法纯属无稽之谈,但有时还要迎合大众的需求。某抽奖活动的奖券号码是5位数(10000-99999),要求其中不要出现带“4”的号码,主办单位请你计算一下,如果任何两张奖券不重号,最多可发出奖券多少张。
请提交该数字(一个整数),不要写任何多余的内容或说明性文字。
答案:52488
#include<cstdio>
#include<string>
#include<sstream>
using namespace std;
void i2s(string &str, int &n){
stringstream stream;
stream << n;
stream >> str;
}
int main(){
int n = 10000;
int count = 0;//不吉利的数字个数
string str;
for (int i = n; i <= 99999; ++i) {
i2s(str, i);
for (int j = 0; j <= 5; ++j) {
if(str[j] - '0' == 4){
count++;
break;
}
}
}
printf("%d", 99999 - 10000 - count + 1);
return 0;
}
参考代码2:(改良版)
#include<cstdio>
#include<string>
#include<sstream>
using namespace std;
void i2s(int &i, string &str){
stringstream stream;
stream << i;
stream >> str;
}
int main(){
int ans = 0;
for (int i = 10000; i <= 99999; ++i) {
string str;
i2s(i, str);
if(str.find('4') == string::npos) ans++;
}
printf("%d\n", ans);
return 0;
}