杭电acm2089

http://acm.hdu.edu.cn/showproblem.php?pid=2089

这题感觉应该用打表的方法,否则会超时,刚开始时用把数字放到字符串里,然后依次判断,因为查询的方向错了,wa,,,,后来用数字对10和100取模然后跟4和62比较,会更简洁。

把所有判断好的数按顺序在数字(或字符串)中做好标记,然后可以在输入后直接计算结果

View Code
 1 #include<stdio.h>
 2 int jili(int n)
 3 {
 4  char s[500];
 5  int i=0,j;
 6  while(n)
 7  {
 8   if(n%10==4||n%100==62)//判断是否为吉利数字
 9     return 0;
10   n/=10;
11  }
12  return 1;
13 }
14 int main()
15 {
16  int a,b,t,i;
17  char ans[1000001];
18  for(i=1;i<=1000000;i++)
19    if(jili(i))
20      ans[i]='1';
21    else ans[i]='0';//打表
22  while(scanf("%d%d",&a,&b)&&(a||b))
23  {
24    t=0;
25    for(;a<=b;a++)
26      t+=ans[a]-'0';
27    printf("%d\n",t);
28  }
29  return 0;
30 }
原文地址:https://www.cnblogs.com/huzhenbo113/p/2996635.html