2012年3月11日dlut周赛1002
LOVELY-POINT
TimeLimit: 1 Second MemoryLimit: 32 Megabyte
Totalsubmit: 154 Accepted: 42
Description
Lolihunter loves Lolita,To tell while lolita is batter,he makes the lovely-point of a Lolita which is found by summing the digits of the fascination of the Lolita. If the resulting value is a single digit then that digit is the Lolita’s lovely-point. If the resulting value contains two or more digits, those digits are summed and the process is repeated. This is continued as much as necessary times to obtain a single digit.
The fascination of a Lolita is n^n.n is a positive integer which is her age.
For example, consider the her age n=4. 4*4*4*4=256. Adding the 2 ,the 5,and the 6 yields a value of 13. Since 13 is not a single digit, the process must be repeated. Adding the 1 and the 3 yeilds 4, the lovely-point of the Lolita is 4.
Input
The input file will contain a list of positive integers, one per line(0<=n<=10000). The end of the input will be indicated by an integer value of zero.
Output
For each integer in the input, output its digital sum on a separate line of the output.
Sample Input
4
3
0
Sample Output
4
9
接触到的第一道数论题,开始以为是大数运算问题,其实可以步步求余来简化。
那么本题的关键在于一个%9
我们看这样的例子abc-->表示为100*a +10*b +c 那么进行各个位的数值相加运算后是什么结果呢?
我们认为是(100*a +10*b +c )%9的余数,因为(100*a +10*b +c )可以写成(99*a +9*b)+(a+b+c),推广之~那么我们步步对9求余,如果能被9整除则结果为9,否则继续求余。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <stack>
using namespace std;
int main()
{
//freopen("in.txt","r",stdin);
int n;
while(cin >> n)
{
int flag=0;
if (n==0)break;
int temp=n;
for(int i=0;i<temp-1;i++)
{
if(n%9==0)
{
flag=1;
break;
}
n%=9;
n=n*temp;
}
if(flag==1)cout << 9 << endl;
else
{
if(n>9)n%=9;
cout << n << endl;
}
}
return 0;
}