本文概述
- C++
- Java
- Python 3
- C#
- PHP
例子:
【检查数字奇数位的数字总和是否可被K整除】输入:N = 4325, K = 4方法:
输出:YES, 因为3 + 5 = 8, 可以被4整除。
输入:N = 1209, K = 3
输出:NO
- 在奇数个位置(从右到左)找到" N"的数字总和。
- 然后, 以" K"取模, 检查总和的可除性。
- 如果可以整除, 则输出"是", 否则输出"否"。
C++
//C++ implementation of the approach
#include <
bits/stdc++.h>
using namespace std;
//function that checks the
//divisibility of the sum
//of the digits at odd places
//of the given number
bool SumDivisible( int n, int k)
{
int sum = 0, position = 1;
while (n>
0) {//if position is odd
if (position % 2 == 1)
sum += n % 10;
n = n /10;
position++;
}if (sum % k == 0)
return true ;
return false ;
}//Driver code
int main()
{
int n = 592452;
int k = 3;
if (SumDivisible(n, k))
cout <
<
"YES" ;
else
cout <
<
"NO" ;
return 0;
}
Java
//Java implementation of the approach
import java.util.*;
class solution
{//function that checks the
//divisibility of the sum
//of the digits at odd places
//of the given number
static boolean SumDivisible( int n, int k)
{
int sum = 0 , position = 1 ;
while (n>
0 ) {//if position is odd
if (position % 2 == 1 )
sum += n % 10 ;
n = n /10 ;
position++;
}if (sum % k == 0 )
return true ;
return false ;
}//Driver code
public static void main(String arr[])
{
int n = 592452 ;
int k = 3 ;
if (SumDivisible(n, k))
System.out.println( "YES" );
else
System.out.println( "NO" );
}
}
//This code is contributed by Surendra_Gangwar
Python 3
# Python 3 implementation of the approach# function that checks the divisibility
# of the sum of the digits at odd places
# of the given number
def SumDivisible(n, k):sum = 0
position = 1
while (n>
0 ) :# if position is odd
if (position % 2 = = 1 ):
sum + = n % 10
n = n //10
position + = 1if ( sum % k = = 0 ):
return True
return False# Driver code
if __name__ = = "__main__" :
n = 592452
k = 3if (SumDivisible(n, k)):
print ( "YES" )
else :
print ( "NO" )# This code is contributed
# by ChitraNayal
C#
//C# implementation of the approach
using System;
class GFG
{
//function that checks the
//divisibility of the sum
//of the digits at odd places
//of the given number
static bool SumDivisible( int n, int k)
{
int sum = 0, position = 1;
while (n>
0)
{ //if position is odd
if (position % 2 == 1)
sum += n % 10;
n = n /10;
position++;
} if (sum % k == 0)
return true ;
return false ;
} //Driver code
static public void Main ()
{
int n = 592452;
int k = 3;
if (SumDivisible(n, k))
Console.WriteLine( "YES" );
else
Console.WriteLine( "NO" );
}
} //This code is contributed by Sachin
PHP
<
?php
//PHP implementation of the approach //function that checks the divisibility
//of the sum of the digits at odd places
//of the given number
function SumDivisible( $n , $k )
{
$sum = 0;
$position = 1;
while ( $n>
0)
{ //if position is odd
if ( $position % 2 == 1)
$sum += $n % 10;
$n = (int) $n /10;
$position ++;
} if ( $sum % $k == 0)
return true;
return false;
} //Driver code
$n = 592452;
$k = 3;
if (SumDivisible( $n , $k ))
echo "YES" ;
else
echo "NO" ;
//This code is contributed
//by Sach_Code
?>
输出如下:
YES
推荐阅读
- 使用快速选择算法的未排序数组的中位数
- 算法设计(在字符串中复制元音)
- 地理信息系统(GIS)中数据的特征
- C语言中的运算符|S1(算术运算符)
- 错误检测代码–校验和是什么()
- Nodejs GM drawLine()函数用法介绍
- win7系统重装后没有声音的处理办法
- win7开机慢怎样办?win7开机慢的处理办法
- 如何激活win7?win7激活图文步骤