3
질문 :
는 우리는 다음과 같은 규칙을 사용하여 x를 정수의 최고 자리를 정의 : 나는 위의 문제를 해결하기 위해 다음과 같은 프로그램을 작성했습니다스칼라 : 더 나은 솔루션
Iff x has only 1 digit, then its super digit is x.
Otherwise, the super digit of x is equal to the super digit of the digit-sum of x. Here, digit-sum of a number is defined as the sum of its digits.
For example, super digit of 9875 will be calculated as:
super-digit(9875) = super-digit(9+8+7+5)
= super-digit(29)
= super-digit(2+9)
= super-digit(11)
= super-digit(1+1)
= super-digit(2)
= 2.
You are given two numbers - n k. You have to calculate the super digit of P.
P is created when number n is concatenated k times. That is, if n = 123 and k = 3, then P = 123123123.
Input Format
Input will contain two space separated integers, n and k.
Output Format
Output the super digit of P, where P is created as described above.
Constraint
1≤n<10100000
1≤k≤105
Sample Input
148 3
Sample Output
3
Explanation
Here n = 148 and k = 3, so P = 148148148.
super-digit(P) = super-digit(148148148)
= super-digit(1+4+8+1+4+8+1+4+8)
= super-digit(39)
= super-digit(3+9)
= super-digit(12)
= super-digit(1+2)
= super-digit(3)
= 3.
을하지만, 방법도 효율적으로 해결하기 위해 그리고 수학 연산보다 효율적인 문자열 연산 ??? 거의 입력에 대해 당신이 문자열 K 시간으로 간주 수를 연결할 필요가 없습니다 것을 깨닫게
861,568,688,536,788 100000
object SuperDigit {
def main(args: Array[String]) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution
*/
def generateString (no:String,re:BigInt , tot:BigInt , temp:String):String = {
if(tot-1>re) generateString(no+temp,re+1,tot,temp)
else no
}
def totalSum(no:List[Char]):BigInt = no match {
case x::xs => x.asDigit+totalSum(xs)
case Nil => '0'.asDigit
}
def tot(no:List[Char]):BigInt = no match {
case _ if no.length == 1=> no.head.asDigit
case no => tot(totalSum(no).toString.toList)
}
var list = readLine.split(" ");
var one = list.head.toString();
var two = BigInt(list(1));
//println(generateString("148",0,3,"148"))
println(tot(generateString(one,BigInt(0),two,one).toList))
}
}
코드를 좀 더 기능적으로 작성하기 위해 리소스를 공유 할 수 있습니까 ??? 덕분에 – user3280908
이 솔루션은 최고의 솔루션입니다. – Sree
마틴 오데 스키 (Martin Odersky)의 Coursera 클래스 "Functional Programming in Scala"와 Runár의 책인 manning이 발행 한 동일한 이름의 책이 있습니다 (9 월 22 일에 정기적으로 판매됩니다 프로모션 코드 dotd092214cc를 사용할 수 있습니다. 이 두 가지 모두 당신을 중급 수준으로 데려 올 것이며 그것을 적용하는 것입니다. 그런 다음 기술을 향상 시키려면 스카 츠와 쉐도우의 소스 코드를 살펴보십시오. – uberwach