A 1049 Counting Ones (30分)

本文解析了一道算法题目——计数从0到给定数中所有数字中1的出现次数。通过具体实例分析,详细解释了如何通过枚举和数学方法解决此问题,并提供了完整的C语言代码实现。
							A 1049 Counting Ones (30分)

在这里插入图片描述
这道题目我看到后根本没有任何思路,没办法只好一个个数枚举,然后暴力破解,但只通过了部分测试点,想了好久,都没有过,最后去看的《算法笔记》,才看懂了这道题.这道题我感觉要我自己做的话,我估计到死都不会AC的.毕竟太菜了.
**题目大意:**首先给你一个数,然后让你判断从0到这个数中共有多少个1.
举一个例子来分析 48210这个数字,
(下列说的左边 右边都是相对于当前的位数所说的)
①从个位数开始,当个位数是1的时候,那么它左边的位数就只能是0000-4820,不能是0000-4821 因为此时个位数是1,那么数字就变成了42811>48210,所以此时有4821个1;
②十位上是1的时候,此时十位的左边是482,右边是0,先分析从 000-481,个位数可以任意取(0-9),因为 0001X - 4811X都小于48210,然后分析4821X,得出X只能取0,否则超出范围,所以此时有482*10+1 = 4821;
③百位上是1的时候,左边的数是48,右边的数是10,因为48210百位上的数字是2大于1,所以左边可以取00-48,右边的数字可以取00-99,,001XX - 481XX都不会超过范围,所以此时有(48+1)100 = 4900;
④千位上是1的时候,左边的数4,右边的数是210,因为8大于1,所以左边的数0-4任意取,右边的数000-999任意取,01XXX-41XXX都不会超范围,所以此时有 (4+1)
1000 = 5000;
⑤万位上是1的时候,即1XXXX ,所以是 0000 - 9999;此时有10000个数字1;
所以最后的结果 SUM = 4821 + 4821 + 4900 + 5000 + 10000 = 29542

此图

#include <stdio.h>

int main(void)
{
    int left,now,right;
    int num;
    int a = 1;
    int sum = 0;
    scanf("%d",&num);

    while(num / a)
    {
        left = num / (a * 10);
        now = num / a % 10;
        right = num % a;
        if(now == 0) sum += left * a;
        else if(now == 1) sum += left * a + 1 + right;
        else sum += (left + 1) * a;
        a *= 10;
    }

    printf("%d\n",sum);
    return 0;
}

写完后又到了该睡觉的时候了,明天继续冲!

### Relaxation Algorithm and Goal Counting The relaxation algorithm is a fundamental concept used primarily in graph algorithms, such as Dijkstra's shortest path or Bellman-Ford algorithms. The primary purpose of the relaxation process involves iteratively improving estimates on distances between nodes until they reach their optimal values[^1]. In this context, **goal counting** refers to tracking how many times specific conditions are met during these iterations. In programming terms, when implementing relaxation techniques within an algorithm like Dijkstra’s: - A distance array `dist[]` stores tentative distances from source node(s). - For each edge `(u,v)` with weight `w`, check whether relaxing it improves current known minimums. Here is Python pseudocode demonstrating basic principles behind applying relaxations while maintaining counts per iteration step: ```python import math def dijkstra_with_count(graph, start_node): n = len(graph) dist = [math.inf] * n # Initialize all distances as infinite initially count = [0] * n # Track number of updates (counts) made at every vertex parent = [-1]*n # To store reconstructed paths later if needed visited = set() # Keep track which vertices already processed fully once before finalizing them permanently pq = [(0,start_node)] # Priority queue initialized containing only starting point pair value zero cost plus itself index position inside list structure above defined earlier named 'graph' dist[start_node]=0 # Distance from self should always be exactly equal to nothingness i.e., Zero! while pq: curr_dist,node=heapq.heappop(pq) if node not in visited: # Only proceed further ahead upon encountering unvisited ones exclusively hereafter... visited.add(node) for neighbor,cost_to_neighbor in enumerate(graph[node]): new_distance_through_this_edge=curr_dist + cost_to_neighbor if new_distance_through_this_edge<dist[neighbor]: dist[neighbor]=new_distance_through_this_edge heapq.heappush(pq,(new_distance_through_this_edge ,neighbor)) parent[neighbor]=node count[neighbor]+=1 # Increment counter whenever we perform successful update operation via any particular pathway leading towards destination target location under consideration currently being analyzed now... return {"Distance":dist,"Parent":parent,"Count_of_Relaxations":count} ``` This code snippet demonstrates both the application of the relaxation technique alongside keeping tally (`count`) regarding how often certain edges contribute toward reducing overall computed costs throughout execution phases progressively over time steps sequentially executed one after another continuously without interruption whatsoever!
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值