zoukankan      html  css  js  c++  java
  • HDU

     

    Olympiad

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

    Problem Description

    You are one of the competitors of the Olympiad in numbers. The problem of this year relates to beatiful numbers. One integer is called beautiful if and only if all of its digitals are different (i.e. 12345 is beautiful, 11 is not beautiful and 100 is not beautiful). Every time you are asked to count how many beautiful numbers there are in the interval [a,b] (a≤b). Please be fast to get the gold medal!
    Input
    The first line of the input is a single integer T (T≤1000), indicating the number of testcases.
    For each test case, there are two numbers a and b, as described in the statement. It is guaranteed that 1≤a≤b≤100000.
     
    Output
    For each testcase, print one line indicating the answer.
    Sample Input
    2
    1 10
    1 1000
    Sample Output
    10
    738

    思路:题目的要求就是求出[a,b]中所有满足该数中每个权位上的数都不相同的个数。我的思路就是先打表(感觉不打表会被TLE),在打表过程中将前i个数中满足题意的个数储存在sum数组中。要判断该数是不是beautiful数只需要将该数的每个权位上的数字提取出来存进一个A数组中,然后通过两个for循环进行遍历,通过flag进行标记提前结束不满足的循环。最后就是进行通过前缀和求差得到结果~

    代码如下:

     1 #include <cstdio>
     2 
     3 const int Max=1e5+50;
     4 int sum[Max],A[10];
     5 int T,a,b;
     6 
     7 void Sum(){
     8   sum[0]=0;
     9   for(int i=1;i<=100000;i++){
    10     int t=0,k=i;
    11     //接下来的循环用于将i的每个权位的数分离开来;
    12     while(k>0){
    13       A[t++]=k%10;
    14       k/=10;
    15     }
    16     if(t==1){
    17       sum[i]=sum[i-1]+1;     //i<10时的所有数都满足题意;
    18     }
    19     else{
    20       int flag=0;
    21       for(int j=1;j<t;j++){
    22         for(int q=0;q<j;q++){
    23           if(A[j]==A[q]){
    24             flag++;          //立个flag,当有两个数相等时退出当前循环,减少循环次数;
    25             break;
    26           }
    27         }
    28         if(flag) break;
    29       }
    30       if(flag){
    31         sum[i]=sum[i-1];     //当前数不满足要求时,前i个数中满足要求的数的个数等于前i-1时的个数;
    32       }
    33       else{
    34         sum[i]=sum[i-1]+1;   //当前数满足要求时,前i个数中满足要求的数等于前i-1时的个数加1;
    35       }
    36     }
    37   }
    38 }
    39 
    40 int main(){
    41   Sum();                    //打表;
    42   while(~scanf("%d",&T)){
    43     while(T--){
    44       scanf("%d%d",&a,&b);
    45       printf("%d
    ",sum[b]-sum[a-1]);      //一维前缀和经典计算方法;
    46     }
    47   }
    48 }
    版权声明:本文允许转载,转载时请注明原博客链接,谢谢~
  • 相关阅读:
    jquery省市联动,根据公司需求而写
    jquery.easyui使用详解,和遇到的问题,提供大家在使用的时候少走弯路(二)
    div内容滚动,无缝滚动
    使用CSS修改HTML5 input placeholder颜色( 转载 )
    如何编写规范,灵活,稳定,高质量的HTML和css代码
    div+css实现未知宽高元素垂直水平居中
    原生JavaScript实现的addclass,removeclass,hasclass,toggleclass,getbyclass
    JS判断上传图片格式是否正确
    文字超出限制字数后隐藏
    JS判断输入框值是否为空
  • 原文地址:https://www.cnblogs.com/Dillonh/p/8490008.html
Copyright © 2011-2022 走看看