数字百位nbut 1407 1到n的数中 1出现的次数

本文解析了一道关于计算特定字符出现次数的ACM编程题,介绍了如何高效地解决该问题的方法,并提供了完整的代码实现。

文章结束给大家来个程序员笑话:[M]

  • [E] Meow star people learning numbers

  • 间时制约: 1000 ms 内存制约: 65535 K
  • 题问述描
  • Meow star people have a different way of counting. They use J、A、B、C……H、J to represent decimal system. A small cat had just learned it these days, she wrote it again and again, A, B, C, D,……AJ, AA, AB……。One day she wrote many numbers, for example, from A to AJJJ. She was tired of it, and want to calculate how many 'A'were in these numbers, for example there were 2 number of 'A'in number 'AJA'.


  • 输入
  • There are many cases, each contain a string N, means numbers from A to N(1<= strlen <= 9), see more details in hint please.
  • 输出
  • For each case, print the number of 'A'.
  • 例样输入
  • H
    AC
    
  • 例样输出
  • 1
    6
    
  • 提示
  • If N = AC, the numbers are A、B、C、D、E、F、G、H、I、AJ、AA、AB、AC. So we print 6.
  • 源来
  • 小白菜

 

    http://acm.nbut.cn/Problem/view.xhtml?id=1407

    题意 :

A、B、C、D、E、F、G、H、I  J  代表 1 2 3 4 5 6 7  8 9  0   输入一个字符串 长度小于于等9  问在这个字符串代表的数字n  从1 到 n  统共涌现了多少个1 
比如 AC 代表13   则有 1 10 11 12 13  共有6个1  输出6
    每日一道理
聪明人学习,像搏击长空的雄鹰,仰视一望无际的大地;愚笨的人学习,漫无目的,犹如乱飞乱撞的无头飞蛾;刻苦的人学习,像弯弯的河流,虽有曲折,但终会流入大海;懒惰的人学习,像水中的木头,阻力越大倒退得越快。
/*
路思:
  路思全完copy 于官方题解 菜比啊本人   要力努发爆额。。。。。。
官方解题地址:http://www.nbutoj.com/009/



标题的意思就是求从1到n,全部数字上涌现1的个数。
当n为一位数的情况:
当n = 0,f(n) = 0; 当n >= 1, f(n) = 1.
当n为二位数的情况:
如果n = 13,全部数字中,个位和十位上都可能有1,那么离开讨论一下,个位涌现1的数次有两次:1和11,十位涌现1的数次有四次:10、11、12、13,
所以f(n) = 2+4 = 6.再看n = 23,十位涌现1的数次有10次,从10到19,个位涌现1的数次为1、11和21,所以f(n) = 3+10=13。
现发个位涌现1的数次不仅和个位数有关,还和十位数有关,如果n的个位数大于于等1,则个位涌现的数次为十位数的数字+1,
如果n的个位数为0,则个位数涌现1的数次于等十位数的数字。而十位数上涌现1的数次不仅和十位数有关,
还和个位数有关:如果十位数字于等1,则十位数上涌现1的数次为个位数的数字加1,如果十位数大于一,则十位数上涌现1的数次为10。
当n为三位数的情况:
如果n = 123,个位涌现1的个数为13:1、11、21……91,101,111,121。十位涌现1的个数为20:10~19,110~119,。百位涌现1的个数为24:100~123
f(123) = 13 + 20 + 24 = 57。

在现算计一般的情况:假设n = abcde,这里a、b、c、d、e分别是十进制数n的各个位数上的数字。如果要算计百位上涌现1的数次,它将到受三个要素的影响:
百位上的数字,百位以下的数字,百位以上的数字。
如果百位上的数字为0,则百位上可能涌现1的数次由更位高决议,比如12013,则可以晓得百位涌现1的情况多是100~199,1100~1199,2100~2199,…,11199~11199,,
一共有1200个。也就是由更位高数字(12)决议,并且于等更位高数字(12)×前当位数(100).
 如果百位上的数字是1,则百位上可能涌现1的数次收到位高和低位的影响。
例如对于12113,受更位高影响,百位涌现1的情况是100~199,1100~1199,2100~2199,…,11100~11199,一共1200个,和面上第一种情况一样,
于等更位高数字(12)×前当位数(100)。但它还受低位影响,百位涌现1的情况是12100~12113,一共114个,于等低位数字(123)+1。
如果百位上数字大于1,则百位上可能涌现1的数次也仅由更位高决议,比如12213,
则百位涌现1的可能性为:100~199,1100~1199,2100~2199,…,11100~11199,12100~12199,一共有1300个,并且于等更位高数字+1(12+1)×前当位数(100)。
通过这样的结总演绎,就能够计划出较为高效的代码了。
*/
#include<iostream> 
using namespace std;  
int n,len; 
int cal(int n) 
{ 
    int count = 0; 
    int fac = 1; 
    int lower = 0; 
    int curr = 0; 
    int high = 0; 
    while(n / fac != 0) 
    { 
        lower = n - (n / fac) * fac; 
        curr = (n / fac) % 10; 
        high = n / (fac * 10); 
          
        switch(curr) 
        { 
            case 0: 
                count += high * fac; 
                break; 
            case 1: 
                count += high * fac + lower + 1; 
                break; 
            default: 
                count += (high+1)*fac; 
                break; 
        } 
        fac *= 10; 
    } 
    return count; 
} 
  
int main() 
{ 
    char ch[10]; 
    while(~scanf("%s",ch)) 
    { 
        len = strlen(ch); 
        n = 0; 
        for(int i = 0; i < len; i ++) 
        { 
            if(ch[i] == 'J') 
            { 
                n *= 10; 
            } 
            else
            { 
                n = n * 10 + (ch[i]-'A' + 1); 
            } 
        } 
        printf("%d\n",cal(n)); 
    } 
    return 0; 
}

文章结束给大家分享下程序员的一些笑话语录: 女人篇
  有的女人就是Windows虽然很优秀,但是安全隐患太大。
  有的女人就是MFC她条件很好,然而不是谁都能玩的起。
  有的女人就是C#长的很漂亮,但是家务活不行。
  有的女人就是C++,她会默默的为你做很多的事情。
  有的女人就是汇编虽然很麻烦,但是有的时候还得求它。
  有的女人就是SQL,她会为你的发展带来莫大的帮助。

json格式: {"id": "cmpl-e759f3d8-78e1-11f0-b2b4-1a85109d8f07", "choices": [{"finish_reason": "stop", "index": 0, "message": {"content": "<think>\nOkay, so I need to figure out how many whole-number divisors there are for the integer given by the expression $ a(2a + b) - 2a^2 + ab $, where $ a $ and $ b $ are different prime numbers greater than 2. Let me start by simplifying the expression step by step.\n\nFirst, let me write down the expression again to make sure I have it right: $ a(2a + b) - 2a^2 + ab $. Let me expand the first term, which is $ a(2a + b) $. If I distribute the $ a $, that becomes $ 2a^2 + ab $. So substituting that back into the original expression, we have:\n\n$ 2a^2 + ab - 2a^2 + ab $.\n\nNow let me combine like terms. The $ 2a^2 $ and $ -2a^2 $ cancel each other out, right? So that leaves me with $ ab + ab $, which is $ 2ab $. So the entire expression simplifies to $ 2ab $. \n\nWait, so the problem is asking for the number of whole-number divisors of $ 2ab $, where $ a $ and $ b $ are different primes greater than 2. Let me confirm that I simplified correctly. Let me check again:\n\nOriginal expression: $ a(2a + b) - 2a^2 + ab $.\n\nExpanding $ a(2a + b) $: $ 2a^2 + ab $. Then subtract $ 2a^2 $, so $ 2a^2 + ab - 2a^2 = ab $. Then add $ ab $, so $ ab + ab = 2ab $. Yes, that seems right. So the expression simplifies to $ 2ab $.\n\nAlright, so now I need to find the number of positive divisors of $ 2ab $. Since $ a $ and $ b $ are primes greater than 2, they are both odd primes. Let me recall that the number of divisors of a number is found by taking its prime factorization, adding one to each of the exponents, and multiplying those together.\n\nSo first, let me write the prime factorization of $ 2ab $. Since 2 is a prime number, and $ a $ and $ b $ are different primes, all three primes are distinct. Therefore, the prime factors of $ 2ab $ are 2, $ a $, and $ b $. Each of these primes has an exponent of 1 in the factorization. So the prime factorization is $ 2^1 \\times a^1 \\times b^1 $.\n\nTo find the number of divisors, I take each exponent (which are all 1s), add 1 to them, and multiply. That would be $ (1 + 1)(1 + 1)(1 + 1) = 2 \\times 2 \\times 2 = 8 $. Therefore, there are 8 positive divisors.\n\nBut let me verify this with an example to make sure I haven't made a mistake. Let me pick specific primes for $ a $ and $ b $. Let's take $ a = 3 $ and $ b = 5 $, which are both primes greater than 2 and different.\n\nCalculating $ 2ab $: $ 2 \\times 3 \\times 5 = 30 $. Now, the divisors of 30 are 1, 2, 3, 5, 6, 10, 15, 30. That's 8 divisors. Yep, that matches the calculation. Let me try another example to be safe. Let me take $ a = 5 $, $ b = 7 $. Then $ 2ab = 2 \\times 5 \\times 7 = 70 $. Divisors of 70: 1, 2, 5, 7, 10, 14, 35, 70. Again, 8 divisors. Okay, so that seems consistent.\n\nBut let me think again if there's any possibility that 2 could be equal to $ a $ or $ b $. However, the problem states that $ a $ and $ b $ are primes greater than 2, so they are odd primes. Therefore, 2 is not among them. Therefore, 2, $ a $, and $ b $ are distinct primes. Therefore, the prime factorization is indeed three distinct primes each to the first power, leading to 2×2×2=8 divisors.\n\nBut let me check if there's a case where maybe $ a $ or $ b $ is 2. But the problem says they are greater than 2, so they can't be. Therefore, my reasoning holds.\n\nTherefore, the number of whole-number divisors is 8. Therefore, the answer is 8, which in boxed form is \\boxed{8}.\n\nWait, but let me just check once more. Suppose someone might think that maybe the original expression simplifies differently? Let me check again:\n\nOriginal expression: $ a(2a + b) - 2a^2 + ab $.\n\nFirst term: $ a(2a + b) = 2a^2 + ab $.\n\nSubtracting $ 2a^2 $: $ 2a^2 + ab - 2a^2 = ab $.\n\nAdding $ ab $: $ ab + ab = 2ab $. Yes, that's correct. So the expression simplifies to 2ab. So no issues there.\n\nAlternatively, maybe someone might misread the original problem? Let me check again. The problem says: \"the integer $ a(2a + b) - 2a^2 + ab $\". So yes, that's how it's written. So the steps are correct.\n\nTherefore, I think my answer is correct. The number of divisors is 8.\n\n**Final Answer**\n\\boxed{8}\n</think>\n\nWe are given an expression involving two distinct prime numbers $ a $ and $ b $, both greater than 2, and asked to find the number of whole-number divisors of the integer:\n\n$$\na(2a + b) - 2a^2 + ab\n$$\n\n---\n\n### Step 1: Simplify the Expression\n\nStart by expanding the first term:\n\n$$\na(2a + b) = 2a^2 + ab\n$$\n\nNow substitute this back into the full expression:\n\n$$\n2a^2 + ab - 2a^2 + ab\n$$\n\nSimplify by combining like terms:\n\n- $ 2a^2 - 2a^2 = 0 $\n- $ ab + ab = 2ab $\n\nSo the entire expression simplifies to:\n\n$$\n2ab\n$$\n\n---\n\n### Step 2: Prime Factorization of $ 2ab $\n\nSince $ a $ and $ b $ are distinct prime numbers greater than 2, and 2 is also a prime, the expression $ 2ab $ is the product of three **distinct** primes:\n\n$$\n2ab = 2^1 \\cdot a^1 \\cdot b^1\n$$\n\n---\n\n### Step 3: Count the Number of Divisors\n\nThe number of positive divisors of a number is determined by taking the product of one more than each exponent in its prime factorization.\n\nHere, the exponents are all 1:\n\n$$\n(1 + 1)(1 + 1)(1 + 1) = 2 \\cdot 2 \\cdot 2 = 8\n$$\n\n---\n\n### Final Answer\n\n$$\n\\boxed{8}\n$$", "role": "assistant", "reasoning_content": ""}, "review": {"gold": "8", "pred": "8", "result": true}}], "created": 1755157209, "model": "Qwen3-32B", "object": "chat.completion", "usage": {"completion_tokens": 1627, "prompt_tokens": 68, "total_tokens": 1695}, "model_spec": {"api_url": "http://0.0.0.0:9009/v1/chat/completions", "model_id": "Qwen3-32B", "api_key": "EMPTY"}, "answer_id": "answer-1a07ae97260436f698e63d4620d7cff0", "subset_name": "default", "raw_input": {"problem": "Suppose $a$ and $b$ are different prime numbers greater than 2. How many whole-number divisors are there for the integer $a(2a+b)-2a^{2}+ab$?", "solution": "Distributing and combining like terms, we have $a(2a+b)-2a^2+ab=2a^2+ab-2a^2+ab=2ab$. Now $a$ and $b$ are different prime numbers greater than 2, so $2ab=2^1\\cdot a^1\\cdot b^1$ has $(1+1)(1+1)(1+1)=\\boxed{8}$ divisors.", "answer": "8", "subject": "Number Theory", "level": 4, "unique_id": "test/number_theory/1287.json"}, "origin_prompt": {"data": ["Suppose $a$ and $b$ are different prime numbers greater than 2. How many whole-number divisors are there for the integer $a(2a+b)-2a^{2}+ab$?\nPlease reason step by step, and put your final answer within \\boxed{}."], "system_prompt": null, "raw_input": {"problem": "Suppose $a$ and $b$ are different prime numbers greater than 2. How many whole-number divisors are there for the integer $a(2a+b)-2a^{2}+ab$?", "solution": "Distributing and combining like terms, we have $a(2a+b)-2a^2+ab=2a^2+ab-2a^2+ab=2ab$. Now $a$ and $b$ are different prime numbers greater than 2, so $2ab=2^1\\cdot a^1\\cdot b^1$ has $(1+1)(1+1)(1+1)=\\boxed{8}$ divisors.", "answer": "8", "subject": "Number Theory", "level": 4, "unique_id": "test/number_theory/1287.json"}, "index": 0}, "reviewed": true, "review_id": "review-307a3dc16716316d92e62e1f3ae09703", "reviewer_spec": {"metric": ["AveragePass@1"], "reviewer": ["Evaluator"], "revision": ["default"]}, "review_time": 1755194244.6042051} 生成csv: 题目ID ID类型 原始答案(raw_input) 原始答案(review.gold) 原始答案一致性 模型答案(review.pred) 模型答案(boxed) 模型答案一致性 审核结果(review.result) 完成原因 原因分析 模型输出摘要 cmpl-e759f3d8-78e1-11f0-b2b4-1a85109d8f07 UUID 8 N/A N/A 8 8 是 N/A stop 正常完成 <think> Okay, so I need to figure out how many whole-number divisors there are for the integer given... 优化脚本
08-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值