package Maths;import java.util.*;class KeithNumber{//user-defined function that checks if the given number is Keith or notstatic boolean isKeith(int x){//List stores all the digits of the XArrayList<Integer> terms=new ArrayList<Integer>();//n denotes the number of digitsint temp = x, n = 0;//executes until the condition becomes falsewhile (temp > 0){//determines the last digit of the number and add it to the Listterms.add(temp%10);//removes the last digittemp = temp/10;//increments the number of digits (n) by 1n++;}//reverse the ListCollections.reverse(terms);int next_term = 0, i = n;//finds next term for the series//loop executes until the condition returns truewhile (next_term < x){next_term = 0;//next term is the sum of previous n terms (it depends on number of digits the number has)for (int j=1; j<=n; j++)next_term = next_term + terms.get(i-j);terms.add(next_term);i++;}//when the control comes out of the while loop, there will be two conditions://either next_term will be equal to x or greater than x//if equal, the given number is Keith, else notreturn (next_term == x);}//driver codepublic static void main(String[] args){Scanner in = new Scanner(System.in);int n = in.nextInt();if (isKeith(n))System.out.println("Yes, the given number is a Keith number.");elseSystem.out.println("No, the given number is not a Keith number.");}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。