计算手指上的数字的程序
数一数手指上给定的数字,找到数字结束的正确手指。
- 第一个数字从拇指开始,第二个从食指开始,第三个从中指开始,第四个从无名指开始,第五个从小指开始。
- 无名指再次出现六,依此类推。
- 在这里,我们观察到一个模式,8(最后一个数字)和 2 在第二个位置结束,在中指的第三或第七个位置,依此类推。
- 该模式在每 8 个数字后不断重复
- 1到8
- 9 至 16
- 17 到 24,依此类推
例子:
Input : 17
Output :1
Input :27
Output :3
C++
// CPP Program to Count numbers on fingers
#include
using namespace std;
int count_num_finger(int n)
{
int r = n % 8;
if (r == 0)
return 2;
if (r < 5)
return r;
else
return 10 - r;
}
// Driver code
int main()
{
int n;
n = 30;
cout << count_num_finger(n);
return 0;
}
Java
// Java Program to Count numbers on fingers
class GFG
{
static int count_num_finger(int n)
{
int r = n % 8;
if (r == 0)
return 2;
if (r < 5)
return r;
else
return 10 - r;
}
// Driver Code
public static void main(String[] args)
{
int n;
n = 30;
System.out.println(count_num_finger(n));
}
}
// This code is contributed
// by Mukul Singh
Python3
def count_num_finger( n ):
r = n % 8
if r == 0:
return 2
if r < 5:
return r
else:
return 10 - r
# Driver Code
n = 30
print(count_num_finger(n))
# This code is contributed by "Sharad_Bhardwaj".
C#
// C# Program to Count numbers on fingers
using System;
class GFG
{
static int count_num_finger(int n)
{
int r = n % 8;
if (r == 0)
return 2;
if (r < 5)
return r;
else
return 10 - r;
}
// Driver Code
public static void Main(String[] args)
{
int n;
n = 30;
Console.WriteLine(count_num_finger(n));
}
}
// This code is contributed by Princi Singh
PHP
Javascript
输出:
4