数根

数根
1.

数根(又称数字根Digital root)是自然数的一种性质,换句话说,每个自然数都有一个数根。

数根是将一正整数的各个位数相加(即横向相加),若加完后的值大于等于10的话,则继续将各位数进行横向相加直到其值小于十为止,或是,将一数字重复做数字和,直到其值小于十为止,则所得的值为该数的数根。例如54817的数根为7,因为5+4+8+1+7=25,25大于10则再加一次,2+5=7,7小于十,则7为54817的数根 (百度百科)

int shugen(int n)
{
   if(n == 0) return 0;    //n为0时,其数根为0。
   else if(n % 9 == 0) return 9;   //n为9的倍数时,其数根为 9。
   else
      return n % 9;     //其他n,其数根为n % 9;
}

简单的写法:

int shugen(int n)
{
    return 1 + ( n - 1 ) % 9;
}

2.求第 k 个数根为m的值n:

B. Digital root
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Today at the lesson of mathematics, Petya learns about the digital root.

The digital root of a non-negative integer is the single digit value obtained by an iterative process of summing digits, on each iteration using the result from the previous iteration to compute a digit sum. The process continues until a single-digit number is reached.
Let’s denote the digital root of xx as S(x)S(x). Then S(5)=5S(5)=5, S(38)=S(3+8=11)=S(1+1=2)=2S(38)=S(3+8=11)=S(1+1=2)=2, S(10)=S(1+0=1)=1S(10)=S(1+0=1)=1.
As a homework Petya got nn tasks of the form: find kk-th positive number whose digital root is xx.
Petya has already solved all the problems, but he doesn’t know if it’s right. Your task is to solve all nn tasks from Petya’s homework.

Input
The first line contains a single integer nn (1≤n≤1031≤n≤103) — the number of tasks in Petya’s homework. The next nn lines contain two integers kiki (1≤ki≤10121≤ki≤1012) and xixi (1≤xi≤91≤xi≤9) — ii-th Petya’s task in which you need to find a kiki-th positive number, the digital root of which is xixi.

Output
Output nn lines, ii-th line should contain a single integer — the answer to the ii-th problem.

Example
inputCopy
3
1 5
5 2
3 1
outputCopy
5
38
19

解题 :

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

int main()
{
    int n,k,m;
    cin>>n;
    while(n--)
    {
        cin>>k>>m;
        cout<<( m - 1 ) * 9 + k;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值