hdu 5373 The shortest problem
2015-08-26 10:47
197 查看
Problem Description
In this problem, we should solve an interesting game. At first, we have an integer n, then we begin to make some funny change. We sum up every digit of the n, then insert it to the tail of the number n, then let the new number be
the interesting number n. repeat it for t times. When n=123 and t=3 then we can get 123->1236->123612->12361215.
Input
Multiple input.
We have two integer n (0<=n<=104
) , t(0<=t<=105)
in each row.
When n==-1 and t==-1 mean the end of input.
Output
For each input , if the final number are divisible by 11, output “Yes”, else output ”No”. without quote.
Sample Input
Sample Output
解答:
首先我们要知道什么样的数字能被11整除:奇数位的和减去偶数位的和能被11整除的数字一定能被11整除。
同时积累一下和这道题有关的小知识:
①、被3整除:位数和能被3整除即可;
②、被4整除:末尾两位能被4整除即可;
③、被7整除:将个位数字截去,在余下的数中减去个位数字的二倍,差是7的倍数即可;(可以递归)
④、被8整除:末尾三位能被8整除即可;
⑤、被9整除:位数和能被9整除即可;
⑥、被11整除:第一种方法就是用上面说的,还有一种是采用和“被7整除”一样的方法,不过要减去的是个位的一倍;
⑦、被12整除:同时被3和4整除;
⑧、被13整除:同“被7整除”,不过我们不是要减去,而是要加上个位的四倍;
⑨、被17整除:同“被7整除”,不过要减去的是个位数的五倍;
⑩、被19整除:同“被7整除”,不过要加上个位数的两倍;
代码:
In this problem, we should solve an interesting game. At first, we have an integer n, then we begin to make some funny change. We sum up every digit of the n, then insert it to the tail of the number n, then let the new number be
the interesting number n. repeat it for t times. When n=123 and t=3 then we can get 123->1236->123612->12361215.
Input
Multiple input.
We have two integer n (0<=n<=104
) , t(0<=t<=105)
in each row.
When n==-1 and t==-1 mean the end of input.
Output
For each input , if the final number are divisible by 11, output “Yes”, else output ”No”. without quote.
Sample Input
35 2 35 1 -1 -1
Sample Output
Case #1: Yes Case #2: No
解答:
首先我们要知道什么样的数字能被11整除:奇数位的和减去偶数位的和能被11整除的数字一定能被11整除。
同时积累一下和这道题有关的小知识:
①、被3整除:位数和能被3整除即可;
②、被4整除:末尾两位能被4整除即可;
③、被7整除:将个位数字截去,在余下的数中减去个位数字的二倍,差是7的倍数即可;(可以递归)
④、被8整除:末尾三位能被8整除即可;
⑤、被9整除:位数和能被9整除即可;
⑥、被11整除:第一种方法就是用上面说的,还有一种是采用和“被7整除”一样的方法,不过要减去的是个位的一倍;
⑦、被12整除:同时被3和4整除;
⑧、被13整除:同“被7整除”,不过我们不是要减去,而是要加上个位的四倍;
⑨、被17整除:同“被7整除”,不过要减去的是个位数的五倍;
⑩、被19整除:同“被7整除”,不过要加上个位数的两倍;
代码:
#include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #include<iostream> using namespace std; int s[20],k; long long fun(long long x) { long long tem=0; while(x) { s[k++]=x%10; tem+=x%10; x/=10; } return tem; } int main() { long long n,m,ans,tem; int t,v,j=1; while(scanf("%lld%d",&n,&t)&&(n!=-1||t!=-1)) { ans=k=0; v=1; m=fun(n); while(k--) if(v) ans+=s[k],v=0; else ans-=s[k],v=1; while(t--) { k=0; m+=fun(m); while(k--) if(v) ans+=s[k],v=0; else ans-=s[k],v=1; } printf("Case #%d: ",j++); if(abs(ans)%11) puts("No"); else puts("Yes"); } }
相关文章推荐
- 使用xinetd来配置rsync服务器
- HDOJ 2095 find your present (2) 位运算
- HTML基础笔记1
- 《C算法》读书笔记9:希尔排序的性质研究
- TCP的三次握手和四次挥手
- SVN版本控制工具使用学习
- jquery学习笔记---闭包,原型链,this关键字
- Ring - HDU 2296(自动机+dp)
- linux系统NFS服务端配置方法
- github创建blog并绑定域名之终结篇
- Oracle与Mysql数据的事务处理机制
- PS学习笔记-----选区工具------套索工具
- Remove Element
- HDU 4267 A Simple Problem with Integers(树状数组)
- poj 3164 Command Network 【最小树形图】【朱刘算法 入门】
- Spring MVC控制层redirect
- n!的长度(stirling公式的应用)
- C#中的IComparable 和 IComparer 接口,实现列表中的对象比较和排序
- 关于Reportviewer
- PAT (Basic Level) Practise:1019. 数字黑洞