codewars算法习题 6级 C A Rule of Divisibility by 13
2018-03-16 23:01
1431 查看
描述:When you divide the successive powers of
10by
13you get the following remainders of the integer divisions:
1, 10, 9, 12, 3, 4.Then the whole pattern repeats.Hence the following method: Multiply the right most digit of the number with the left most number in the sequence shown above, the second right most digit to the second left most digit of the number in the sequence. The cycle goes on and you sum all these products. Repeat this process until the sequence of sums is stationary............................................................................Example: What is the remainder when
1234567is divided by
13?
7×1 + 6×10 + 5×9 + 4×12 + 3×3 + 2×4 + 1×1 = 178We repeat the process with 178:
8x1 + 7x10 + 1x9 = 87and again with 87:
7x1 + 8x10 = 87...........................................................................From now on the sequence is stationary and the remainder of
1234567by
13is the same as the remainder of
87by
13:
9Call
thirtthe function which processes this sequence of operations on an integer
n (>=0).
thirtwill return the stationary number.
thirt(1234567)calculates 178, then 87, then 87 and returns
87.
thirt(321)calculates 48, 48 and returns
48
解析:给定一个数(long long 型),将倒着的个位数与数组1,10,9,12,3,4顺序相乘相加,数组可周而复始,相加的数重复上述过程,直至数不再变化,输出数
算法:两个函数,第一个函数先做上述过程相加的两个数的计算,然后在while循环中如果两数不相等则在传递一个数给另一个数,相等则退出,另一个函数则做上述过程中相加的计算
函数代码:long long thirt(long long n) { long long transmit(long long n); long a,b,temp; a=transmit(n); b=transmit(a); if(a==b){return a;}//如果相等则直接返回 while(a!=b){ a=transmit(b);//大数换成第三个数 temp=a;a=b;b=temp;//因为数会越来越小,且上一个返回数变小,所以换位使a>b,并抛弃掉第一个数 } return(a); } long long transmit(long long n)//计算相加的函数 { int lsl[6]={1,10,9,12,3,4}; int b=0,a=0,c=0; while(n!=0){ a=(int)n%10; n=n/10; if(b!=6){ c+=a*lsl[b]; b++; } if(b==6){b=0;} } return c;
}
更简洁的方法:使用递归,一个函数足矣
相关文章推荐
- Rule 13: Configure ETags13(Chapter 13 of High performance Web Sites)
- Change Number to English By Reading rule of money
- codewars习题(2) python3 算法 Delete occurrences of an element if it occurs more than n times 6级
- Caused by: java.lang.OutOfMemoryError: PermGen space异常处理
- [Step By Step]SAP HANA PAL演绎推理算法Lite Aprior实现LITEAPRIORIRULE
- 习题6-13 电子表格计算器(Spreadsheet Calculator, ACM/ICPC World Finals 1992, UVa215)
- python核心编程学习笔记-2016-07-20-01-习题3-13
- Question 41: Which of the following operators must be overloaded by function objects in the Standard Template Library?
- 解决Deprecated: Assigning the return value of new by reference
- android编程中遇到的Caused by: android.view.InflateException: Binary XML file line #13错误的原因及解决办法
- rule of poker
- ML 12 & 13 mixture of gaussions and EM
- Science14年的聚类论文——Clustering by fast search and find of density peaks
- Incorrect decrement of the reference count of an object that is not owned at this point by the caller1
- 目标跟踪系列十一:Exploiting the Circulant Structure of Tracking-by-detection with Kernels代码思路
- The resource identified by this request is only capable of generating responses with characteristics
- CRM SaveChanges Exception has been thrown by the target of an invocation
- Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) B. Divisiblity of Differences
- this product is covered by one or more of the following
- error C2360: initialization of 'hPen' is skipped by 'case' label的解决方法