简体   繁体   English

如何编写打印给定数字是否可被 3 整除的程序? [等候接听]

[英]How to write a program that prints whether a given number is divisible by 3? [on hold]

Write a program that prints whether a given number is divisible by 3. The number can be huge (may contain upto 1000 digits).编写一个程序,打印给定的数字是否能被 3 整除。这个数字可能很大(最多可能包含 1000 位数字)。 (Hint: A number is divisible by 3 if the sum of its digits is divisible by 3.) (提示:如果一个数字的数字之和可以被 3 整除,则该数字可以被 3 整除。)

As in the question it is given that input can be upto 1000 digits so input will be a huge number.. and a hint is given by me according to mathematical knowledge.. so now if i try to approach through my logic ( given hint ) i should have to take the whole number into array then i have add all the digit.正如在问题中给出的输入最多可以是 1000 位,所以输入将是一个巨大的数字。我根据数学知识给出了一个提示。所以现在如果我尝试通过我的逻辑接近(给出提示)我应该将整个数字放入数组中,然后我添加了所有数字。 But the thing is i couldn't convert the number into a integer array.. what should i do to complete my code According to the way i am approaching?但问题是我无法将数字转换为 integer 数组。根据我接近的方式,我应该怎么做才能完成我的代码? If you have any new idea or code you can share it..如果您有任何新的想法或代码,您可以分享它..

` If the input ( integer value): 141414141414141414 ` 如果输入(integer 值):141414141414141414

 Output: Divisible by 3 `

I am assuming you are receiving a BigInt.我假设您正在接收 BigInt。

boolean checkMultipleOfThree(BigInt input){
  char[] numberCharArray= input.toString().toCharArray();
  /*
    if original BigInt contains up­to 1000 digits, 
    it means its digit sums will be a maximum of 9000. 
    So no problem operating with int.
  */
  int sum = 0;
  for (int i = 0; i < numberCharArray.length; i++) { 
    sum += Integer.valueOf(numberCharArray[i]); 
  }

  return sum % 3 == 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查数字是否可以被3和7整除,或者两者都不可分 - Check whether the number is divisible by both 3 and 7, or by neither of them Java程序输出可被其他数字整除的数字 - Java program that prints out numbers that are divisible by other numbers Java中高度可分割的三角数:程序效率低下 - Highly divisible triangular number in java: Program inefficiency 在Java中,如何分配给定的内存量,并保留它直到程序退出? - In Java, how to allocate given amount of memory, and hold it until program exit? 在Java中,如何创建一个简单的程序来打印短语中的辅音和元音的数量? - In Java, how to create a simple program that prints the number of consonants and vowels in a phrase? 如何查找给定数字是否是某个数字的阶乘? - how to find whether the given number is a factorial of some number or not? 程序通过一次更改数字的一位数来计算有多少个不同的数字可以被 3 整除 - Program to figure out the count of how many different numbers can be divisible by 3 by changing number's one digit at a time 查找由两位数字除以给定数字所形成的最小数字 - Find smallest number formed by two digits divisible by given number 如何检查数字是否可以被某个数字整除? - How to check if number is divisible by a certain number? 在下列条件下,编写一个程序将数字舍入到下一个10的倍数? - Write a program to round a number to the next multiple of 10 given the following conditions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM