[CF]Sums of Digits

本文介绍了一种算法,用于根据给定的数字序列重建一个丢失的、严格递增的整数序列。该序列的每个元素的数字之和等于给定序列中的对应值。文章提供了完整的代码实现,帮助读者理解如何解决这一有趣的问题。

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

C. Sums of Digits
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasya had a strictly increasing sequence of positive integers a1, ..., an. Vasya used it to build a new sequence b1, ..., bn, where bi is the sum of digits of ai's decimal representation. Then sequence ai got lost and all that remained is sequence bi.

Vasya wonders what the numbers ai could be like. Of all the possible options he likes the one sequence with the minimum possible last number an. Help Vasya restore the initial sequence.

It is guaranteed that such a sequence always exists.

Input

The first line contains a single integer number n (1 ≤ n ≤ 300).

Next n lines contain integer numbers b1, ..., bn  — the required sums of digits. All bi belong to the range 1 ≤ bi ≤ 300.

Output

Print n integer numbers, one per line — the correct option for numbers ai, in order of following in sequence. The sequence should be strictly increasing. The sum of digits of the i-th number should be equal to bi.

If there are multiple sequences with least possible number an, print any of them. Print the numbers without leading zeroes.

Sample test(s)
input
3
1
2
3
output
1
2
3
input
3
3
2
1
output
3
11
100

挺有意思的一道题目,给你N个数字,其N个数是严格递增序列数字每个数位上的和。求出原来的递增序列。递增序列满足最小的情况。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <vector>
#include <ctime>
#define LL __int64
#define eps 1e-8
#define pi acos(-1)
using namespace std;
string ans[310];
string go(int a,int b){
	string cnt="";
	int l=ans[a].length();
	for (int i=l-1;i>=0;i--){
		for (int j=ans[a][i]-'0'+1;j<=9;j++){
			cnt="";
			int s=j;
			for (int k=0;k<i;k++){
				s+=ans[a][k]-'0';
				cnt+=ans[a][k];
			}
			cnt+=char(j+'0');
			if (s+9*(l-1-i)>=b && s<=b){
				int g=b;
				for (int k=i+1;k<l;k++){
					for (int t=0;t<=9;t++)
						if (s+t+9*(l-k-1)>=g){
							cnt+=char(t+'0');
							g-=t;
							break;
						}
				}
				return cnt;
			}
		}
	}
	for (int i=l+1;;i++){
		for (int j=1;j<=9;j++){
			cnt="";
			int s=j;
			cnt=char(s+'0');
			if (s+9*(i-1)>=b && s<=b){
				int g=b;
				for (int k=0;k<i-1;k++){
					for (int t=0;t<=9;t++)
						if (s+t+9*(i-k-2)>=g){
							cnt+=char(t+'0');
							g-=t;
							break;
						}
				}
				return cnt;
			}
		}
	}
}
int main(){
	int n,k;
	scanf("%d",&n);
	//memset(ans,0,sizeof(ans));
	for (int i=1;i<=n;i++){
		scanf("%d",&k);
		ans[i]=go(i-1,k);
	}
	for (int i=1;i<=n;i++)
		cout<<ans[i]<<endl;
	return 0;
}


<think>好的,用户的问题是关于“sums”的功能或用法,或者“sums”在编程中的含义。我需要仔细分析用户提供的引用内容,并结合之前的对话历史来回答这个问题。 首先,用户提到了几个引用,其中引用[4]和引用[5]都提到了使用map函数生成sums变量的例子。例如,在引用[4]中,sums是通过map(add, numbers1, numbers2)生成的,然后转换为列表输出[5,7,9]。引用[5]中也有类似的例子,使用lambda函数将两个列表相加生成sums。因此,可以看出sums在这里是一个变量名,存储了map函数处理后的结果,即将两个列表对应元素相加的和。 接下来,用户之前的问题是关于百分号%的多种用途,现在转向sums的用法。需要明确的是,sums并不是一个内置函数,而是一个变量名,通常用于存储求和的结果。根据引用中的例子,sums可能是一个列表,包含两个列表元素相加的结果。比如在Python中,使用map函数结合lambda或add函数来生成这样的列表。 还需要注意用户可能存在的混淆点:是否有sums函数的存在?根据现有的知识,Python中有内置的sum()函数,用于对可迭代对象求和,但sums在引用中作为变量名出现。因此,用户可能误以为sums是一个函数,但实际上它是一个变量。 此外,用户提供的引用中并没有提到sums作为函数的情况,因此需要澄清这一点。可能需要解释sum()函数的用法,同时说明sums作为变量名的常见使用场景,比如存储多个和或结果集合。 另外,用户可能是在看到示例代码中的sums变量后,想了解它的作用或如何正确使用。因此,需要详细说明在示例中sums是如何生成的,以及它在程序中的用途,比如存储计算结果以便后续使用或输出。 还需要考虑用户可能的背景,他们可能对map函数或lambda表达式不太熟悉,因此需要简要解释这些概念,以帮助理解sums变量的生成过程。同时,指出在Python中,变量名是自定义的,sums只是一个常见的命名习惯,实际中可以使用其他名称。 最后,要确保回答结构清晰,分点说明sums的可能含义和用法,并结合引用中的示例进行具体解释,同时对比sum()函数的功能,避免用户混淆。</think>根据上下文和引用内容,"sums" 在编程中主要有两种含义,需根据具体场景区分: --- ### 一、作为变量名(常见用途) #### 1. **存储计算结果集合** - 常见于存储**多组数值求和结果**,尤其在向量化操作中。 ```python # 引用[5]示例:两个列表逐元素相加 numbers1 = [1, 2, 3] numbers2 = [4, 5, 6] sums = list(map(lambda x, y: x + y, numbers1, numbers2)) # sums = [5,7,9] ``` - `sums` 在此处是**变量名**,保存了 `map()` 函数对两个列表逐元素求和的结果[^5]。 #### 2. **应用场景扩展** - **多维数据聚合**:如计算矩阵每行的和 ```python matrix = [[1,2], [3,4]] sums = [sum(row) for row in matrix] # sums = [3,7] ``` - **实时统计**:记录动态累加结果 ```python data_stream = [10, 20, 30] sums = [] total = 0 for num in data_stream: total += num sums.append(total) # sums = [10,30,60] ``` --- ### 二、与内置函数 `sum()` 的关系 #### 1. **`sum()` 的核心功能** - Python 内置函数,用于计算**可迭代对象的元素总和**: ```python numbers = [1, 2, 3] total = sum(numbers) # total = 6 ``` - 支持自定义起始值: ```python sum([1,2], start=10) # 结果:13(10+1+2) ``` #### 2. **命名习惯对比** - `sums`(复数):通常表示**多个独立的和** ```python sums = [sum(col) for col in zip(*matrix)] # 矩阵列求和列表 ``` - `total_sum` 或 `sum_total`:表示**单一总和** ```python total_sum = sum(chain.from_iterable(matrix)) # 矩阵所有元素总和 ``` --- ### 三、易混淆点解析 #### 1. **与 SQL 中 `SUM()` 的区别** - SQL 的 `SUM()` 是聚合函数,直接返回单值: ```sql SELECT SUM(salary) FROM employees; -- 返回所有薪资总和 ``` - Python 的 `sum()` 功能类似,但 `sums` 作为变量名需手动实现类似逻辑[^3]。 #### 2. **函数式编程中的角色** - 在引用[2]提到的函数式编程中,`sums` 可作为**高阶函数的输出**: ```javascript // 类似引用[2]风格:函数作为参数 const sumArrays = (arr1, arr2) => arr1.map((x, i) => x + arr2[i]); const sums = sumArrays([1,2], [3,4]); // sums = [4,6] ``` --- ### 四、最佳实践建议 1. **明确命名意图** - 若存储多个和值,优先使用 `sums` - 若为单个总和,使用 `total` 或 `total_sum` 2. **性能优化** - 对大列表操作时,优先使用 `numpy` 的向量化求和: ```python import numpy as np arr = np.array([[1,2], [3,4]]) sums = arr.sum(axis=1) # 高效计算每行和 [3,7] [^4] ``` --- ### 相关问题 1. 如何在 Python 中高效计算多维数组的每列总和? 2. `sum()` 函数在处理字符串列表时会发生什么? 3. 函数式编程中的 `map()` 和 `sum()` 如何结合使用? 4. 为什么在 SQL 和 Python 中聚合函数的设计差异如此大?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值