Mountain Number

解决在给定范围内找出所有满足特定条件的山峰数字的问题,这些数字必须为正整数且满足中间数值大于两边的性质。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

One integer number x is called "Mountain Number" if:

(1) x>0 and x is an integer;

(2) Assume x=a[0]a[1]...a[len-2]a[len-1](0≤a[i]≤9, a[0] is positive). Any a[2i+1] is larger or equal to a[2i] and a[2i+2](if exists).

For example, 111, 132, 893, 7 are "Mountain Number" while 123, 10, 76889 are not "Mountain Number".

Now you are given L and R, how many "Mountain Number" can be found between L and R (inclusive) ?

Input

The first line of the input contains an integer T (T≤100), indicating the number of test cases.

Then T cases, for any case, only two integers L and R (1≤L≤R≤1,000,000,000).

Output

For each test case, output the number of "Mountain Number" between L and R in a single line.

Sample Input

31 101 1001 1000

Sample Output

954384


<span style="font-size:18px;"># include <cstdio>
# include <cstring>
# include <iostream>
using namespace std;

int dp[12][12][2];  //三维数组分别表示当前数位、前一位的数值、当前位属于的奇偶状态;
int doing,judge;   //doing是否达到临界,judge是否含有前导零;
int digit[12];  //储存数字的每一位:

int dfs(int pos,int pre,int pari,int judge,int doing)
{
    if(pos == -1)   return 1;
    if(!doing&& dp[pos][pre][pari] != -1)   return dp[pos][pre][pari];

    int ans = 0,end;
    end = doing ? digit[pos] : 9;

    for(int i = 0 ; i <= end;i++)
    {
        if(!(judge || i))   ans += dfs(pos-1,9,0,judge||i,doing &&( i == end));   //前一位和当前位都含前导零;
        else if(pari && pre <= i)  ans += dfs(pos-1,i,pari^1,judge||i,doing &&( i == end));
        else if(!pari&& pre >= i)  ans += dfs(pos-1,i,pari^1,judge||i,doing &&( i == end));
    }
    if(!doing)  dp[pos][pre][pari] = ans;
    return ans;
}

int cal(int x)<span style="white-space:pre">	</span>//取出数值每一位的数字;
{
    int cnt = 0;
    while(x > 0){
        digit[cnt++] = x % 10;
        x /= 10;
    }
    return dfs(cnt-1,9,0,0,1);  //当前数位,前一位数值,当前位奇偶,是否含有前导零,是否达到上界:
}

int main()
{
    int n;
    scanf("%d",&n);
    memset(dp,-1,sizeof(dp));
    while(n--){
        int l,r;
        scanf("%d%d",&l,&r);
        printf("%d\n",cal(r)-cal(l-1));
    }
    return 0;
}
</span>



export interface Question { word: string; //单词 sentence: string; //例句 options: string[]; //选项 answer: string; //答案 } //题库 export const questionData: Question[] = [ { word: "book", options: ["书籍", "笔", "橡⽪", "背包"], answer: "书籍", sentence: "I love to read a good book every night." }, { word: "computer", options: ["电视", "电脑", "⼿机", "相机"], answer: "电脑", sentence: "I use the computer for work and entertainment." }, { word: "apple", options: ["⾹蕉", "桃⼦", "梨", "苹果"], answer: "苹果", sentence: "She enjoys eating a crisp apple in the afternoon." }, { word: "sun", options: ["⽉亮", "太阳", "星星", "地球"], answer: "太阳", sentence: "The sun provides warmth and light to our planet." }, { word: "water", options: ["⽕", "⼟地", "⻛", "⽔"], answer: "⽔", sentence: "I always carry a bottle of water with me." }, { word: "mountain", options: ["沙漠", "海洋", "平原", "⼭"], answer: "⼭", sentence: "The mountain range is covered in snow during winter." Question.ets ArkTS 16 }, { word: "flower", options: ["树⽊", "草地", "花", "灌⽊"], answer: "花", sentence: "The garden is filled with colorful flowers." }, { word: "car", options: ["⾃⾏⻋", "⻜机", "船", "汽⻋"], answer: "汽⻋", sentence: "I drive my car to work every day." }, { word: "time", options: ["空间", "时钟", "⽇历", "时间"], answer: "时间", sentence: "Time flies when you're having fun." }, { word: "music", options: ["画", "舞蹈", "⾳乐", "戏剧"], answer: "⾳乐", sentence: "Listening to music helps me relax." }, { word: "rain", options: ["雪", "雷电", "阳光", "⾬"], answer: "⾬", sentence: "I enjoy the sound of rain tapping on the window." }, { word: "fire", options: ["冰", "⽕焰", "烟雾", "闪电"], answer: "⽕焰", sentence: "The campfire warmed us on a chilly evening." }, { word: "friend", options: ["陌⽣⼈", "邻居", "家⼈", "朋友"], answer: "朋友", sentence: "A true friend is always there for you." }, { word: "food", options: ["⽔果", "蔬菜", "⾁", "⻝物"], answer: "⻝物", 17 sentence: "Healthy food is essential for a balanced diet." }, { word: "color", options: ["⿊⾊", "⽩⾊", "红⾊", "颜⾊"], answer: "颜⾊", sentence: "The artist used a vibrant color palette." }, { word: "bookshelf", options: ["椅⼦", "桌⼦", "书架", "床"], answer: "书架", sentence: "The bookshelf is filled with novels and reference books." }, { word: "moon", options: ["太阳", "星星", "⽉亮", "地球"], answer: "⽉亮", sentence: "The moonlight illuminated the night sky." }, { word: "school", options: ["公园", "商店", "医院", "学校"], answer: "学校", sentence: "Students go to school to learn and grow." }, { word: "shoes", options: ["帽⼦", "⾐服", "裤⼦", "鞋⼦"], answer: "鞋⼦", sentence: "She bought a new pair of stylish shoes." }, { word: "camera", options: ["电视", "电脑", "相机", "⼿机"], answer: "相机", sentence: "The photographer captured the moment with his camera." } ] //从题库中随机抽取n个题⽬ export function getRandomQuestions(count: number) { let length = questionData.length; let indexes: number[] = []; while (indexes.length < count) { let index = Math.floor(Math.random() * length); if (!indexes.includes(index)) { indexes.push(index) } } return indexes.map(index => questionData[index]) } 报错Array literals must contain elements of only inferrable types (arkts-no-noninferrable-arr-literals) <ArkTSCheck> Object literal must correspond to some explicitly declared class or interface (arkts-no-untyped-obj-literals) <ArkTSCheck> 应为'(', ',' 或 '}',得到'Question'
03-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值