UVa1583 Digit Generator另一种解法
来源:互联网 发布:网络h小说合集 编辑:程序博客网 时间:2024/05/29 04:18
紫书上的解法:枚举100000内的所有正整数m的最小生成元,然后查表即可。
之前一看到这个题目就想到了另一个方法:
简单分析一下,题目中给出 n 的范围是 [ 1, 100000 ],所以 n 的最小生成元 x 也必定在这个区间中,那么 x 与 n 最多差多少呢?易知为 45 ,所以对于任意的 n ,我们只须遍历其前 45 个元素即可。
代码如下:
#include <stdio.h>
int main(void)
{
int T;
scanf("%d", &T);
while(T--) {
int n, i;
scanf("%d", &n);
for(i = n - 45; i <= n; ++i)
if(n == i + sum(i)) { printf("%d\n", i); break; }
if(i > n) printf("%d\n", 0);
}
return 0;
}
int sum(int n) {
int a = 0;
while(n > 0) {
a += n%10;
n /= 10;
}
return a;
}
阅读全文
1 0
- UVa1583 Digit Generator另一种解法
- UVa1583 - Digit Generator
- UVA1583 - Digit Generator
- uva1583 - Digit Generator
- uva1583-digit generator
- UVA1583 UVALive3355 Digit Generator
- UVA1583 - Digit Generator
- UVa1583-Digit Generator
- Digit Generator—UVa1583
- UVa 1583 Digit Generator另一种解法
- UVa1583——Digit Generator
- UVA1583 - Digit Generator(生成元,打表)
- Digit Generator, ACM/ICPC Seoul 2005, UVa1583
- [Digit Generator, ACM/ICPC Seoul 2005, UVA1583]
- 生成元(Digit Generator,ACM/ICPC Seoul 2005, UVa1583)
- 生成元(Digit Generator, ACM/ICPC Seoul 2005, UVa1583)
- uva1583 Digit Generator && uva1584 Circular Sequence(水)
- UVa1583 - Digit Generator 生成元 (java版本)
- CentOS7 安装 RabbitMQ
- 2串口收发数据
- Exception & Issue for Selenium
- 【模板】(新)快速幂+快速乘
- DeepLearing学习笔记-从逻辑回归出发
- UVa1583 Digit Generator另一种解法
- 城市列表
- zTree 设置父节点checkbox不可点击
- MySQL UNION 操作符
- oracle 错误汇总大全 数据库笔记
- leetcode-4. Median of Two Sorted Arrays
- lib.so.6软链接被删除的恢复问题
- Java泛型
- IntentService解析与使用