【UVA 1583】Digit Generator
题意
a加上 a的各位数=b,则b是a的digitSum,a是b的generator,现在给你digitSum,让你求它的最小的generator。
分析
一种方法是:
预处理打表,也就是把1到100000的digitSum求出来,对每个digitSum保存最小的generator。
另一种方法是:
对digitSum-45到digitSum-1的数求它的digitSum,最先算到给定的digitSum的就是最小的generator。
因为最多6位数,每位上的数最大是9,最多5个9,所以a的各位数之和最大是45,b比a最多多45。
代码
法一(打表运行速度更快)
#include<stdio.h>
#include<cstring>
int t,n,ds[]; int dd(int a)
{
int d=;
while(a)
{
d+=a%;
a/=;
}
return d;
}
int main()
{
for(int i=; i<; i++)
{
int s=i+dd(i);
if(s>) continue;
if(!ds[s]) ds[s]=i;
}
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
if(ds[n]) printf("%d\n",ds[n]);
else printf("0\n");
}
return ;
}
法二
#include<stdio.h>
#include<cstring>
int t,n,ok; int dd(int a)
{
int d=;
while(a)
{
d+=a%;
a/=;
}
return d;
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
ok=;
for(int i=n-;i<n&&ok;i++)
if(i+dd(i)==n){
printf("%d\n",i);
ok=;
}
if(ok)printf("0\n");
}
return ;
}
【UVA 1583】Digit Generator的更多相关文章
- 【例题3-5 UVA - 1583】Digit Generator
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] for (int i = 1;i <= n;i++) { 算出i是哪一个的生成元. 假设是y. 则ans[y] = min(a ...
- 【巧妙算法系列】【Uva 11464】 - Even Parity 偶数矩阵
偶数矩阵(Even Parity, UVa 11464) 给你一个n×n的01矩阵(每个元素非0即1),你的任务是把尽量少的0变成1,使得每个元素的上.下.左.右的元素(如果存在的话)之和均为偶数.比 ...
- 【贪心+中位数】【UVa 11300】 分金币
(解方程建模+中位数求最短累积位移) 分金币(Spreading the Wealth, UVa 11300) 圆桌旁坐着n个人,每人有一定数量的金币,金币总数能被n整除.每个人可以给他左右相邻的人一 ...
- 【UVa 10881】Piotr's Ants
Piotr's Ants Porsition:Uva 10881 白书P9 中文改编题:[T^T][FJUT]第二届新生赛真S题地震了 "One thing is for certain: ...
- 【UVa 116】Unidirectional TSP
[Link]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- 【UVa 1347】Tour
[Link]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- 【UVA 437】The Tower of Babylon(记忆化搜索写法)
[题目链接]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- 【uva 1025】A Spy in the Metro
[题目链接]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- 【Uva 11584】Partitioning by Palindromes
[Link]:https://cn.vjudge.net/contest/170078#problem/G [Description] 给你若干个只由小写字母组成的字符串; 问你,这个字符串,最少能由 ...
随机推荐
- CSS 属性 :before && :after的用法,伪类和伪元素的区别
一::before && :after的用法 :before 如同对伪元素的名称一样,:before 是用来给指定的元素的内容前面插入新的内容.举例说明: .before:before ...
- python中的深拷贝和潜拷贝
>>> a = ['ace',['age',10]] >>> b = a[:] >>> c = list(a) >>> for ...
- Android外部SD卡的读取
package com.kevin.writeorreadfile1_1; import android.app.Activity; import android.bluetooth.le.ScanF ...
- AutoIT删除Internet临时文件
搜集了几个超赞的方法: 1.删除临时文件 Temporary Internet Files:RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8 2. 删 ...
- C和指针笔记 3.8 static关键字
当用于不同的上下文环境时,static关键字具有不同的意思. 当它用于函数定义时,或用于代码块之外的变量声明时,static关键字用于修改标识符的链接属性,从external改为internal,但标 ...
- C# WinForm 中Console 重定向输出到ListBox控件中显示
{ VoidAction action = { lstBox.Items. ...
- codevs 3008 加工生产调度[贪心]
3008 加工生产调度 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 某工厂收到了n个产品的订 ...
- 3016 质子撞击炮 II
3016 质子撞击炮 II 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description [抱歉数据错误~~已修 ...
- poj 1046 Color Me Less
Color Me Less Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 33007 Accepted: 16050 D ...
- win server 2008配置ftp无法登陆问题的解决办法
解决办法放在最前面,方便急需答案的同学: 创建了ftp使用的windows账户后,一定要给该账户添加ftp目录的权限,如下图所示,为新账户添加权限后(且设置了“ftp身份验证”),即可正常访问ftp: ...