Graduation Examination

Graduation Examination
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB
Total submit users: 41, Accepted users: 33
Problem 11566 : No special judgement
Problem description
  After four year's study, ACgege is about to graduate from the university of ACM(ACMu). Unlike a general university, ACMu assess students' quality not on the paper they have published, but on their ability to solve ACM problems. Consequently, a student can graduate smoothly from ACMu only if he can address a problem provided by his superviser. As you know, ACgege is a naughty boy, so as a punishment to him, his supervisor deliberately gives him a problem beyond his reach.  After repeated thinking without success, ACgege decides to ask you, a clever programmer, to help him. The problem is as follows.
Start with a number x0, say x0=12345. Form a new number x1 by summing all the digits of x0(x1=1+2+3+4+5=15). Iterate the above process of adding digits of the previous number, we obtain a sequence of numbers: x2=1+5=6, x3=x4=...=6. Just as you have observed, the sequence is stable when arriving at 6. It is known that any such sequence can be stable after a finite number of iteration, no matter what the initial value is. Your task is to output the stable value for any initial integer x0. To make the problem a little easier, we assume that x0 is a Fibonacci number. Fibonacci sequence is defined by the recurrent formula Fn=Fn-1+Fn-2 with the initial value F1=F2=1. A number is called a Fibonacci number if it is in the Fibonacci sequence. 
 
Input
  There are several test cases, each test case in one line contains a positive integer n, 0 The input will finish with the end of file.
 
Output
  For each the case, output the stable value for the sequence with the initial value Fn. 
 
Sample Input
7
8
3
Sample Output
4
3
2
Problem Source
  2014哈尔滨理工大学秋季训练赛

 

打表找规律,有循环节;

代码:

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

using namespace std;

int main(){
    int f[1000];
    int a[100];//={1,1,2,3,5,8,4,3,7,1,8,9,8,8,7,6,4,1 5 6 2 8 1 9};
    f[0]=1;f[1]=1;
    for(int i=2;i<=24;i++)f[i]=f[i-1]+f[i-2];
    long long sum,cnt,m;
    for(int i=0;i<24;i++){
        sum=f[i];
        while(sum>=10){
            cnt=sum;
            m=0;
            while(cnt){
                m+=cnt%10;
                cnt/=10;
            }
            sum=m;
        }
        a[i]=sum;
    }
    long long n;
    while(scanf("%I64d",&n)!=EOF){
        n=(n-1)%24;
        cout<<a[n]<<endl;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值