【百度之星技术社区邀请赛12月11号竞赛题目一】du熊学斐波那契I

du熊学斐波那契I

Time Limit : 2000/1000ms (C/Other)   Memory Limit : 65535/32768K (C/Other)

本次组委会推荐使用C、C++

Problem Description

du熊对数学一直都非常感兴趣。最近在学习斐波那契数列的它,向你展示了一个数字串,它称之为“斐波那契”串:

 

11235813471123581347112358........

 

聪明的你当然一眼就看出了这个串是这么构造的:

1.先写下两位在0~9范围内的数字a, b,构成串ab;

2.取串最后的两位数字相加,将和写在串的最后面。

上面du熊向你展示的串就是取a = b = 1构造出来的串。

显然,步骤1之后不停地进行步骤2,数字串可以无限扩展。现在,du熊希望知道串的第n位是什么数字。

Input

输入数据的第一行为一个整数T(1 <= T <= 1000), 表示有T组测试数据;

每组测试数据为三个正整数a, b, n(0 <= a, b < 10, 0 < n <= 10^9)。

Output

对于每组测试数据,输出一行“Case #c: ans”(不包含引号) 

c是测试数据的组数,从1开始。

Sample Input

3

1 1 2

1 1 8

1 4 8

Sample Output

Case #1: 1

Case #2: 3

Case #3: 9

Hint

对于第一、二组数据,串为112358134711235......

对于第三组数据,串为14591459145914......

 

 

 

不知道这代码好不好,但是这是我做出来的哦~~~~~~~~

#include<malloc.h>
#include<iostream>
using namespace std;

typedef struct node
{
    int data;
    struct node *next;
}SqStack;

void Push(SqStack *&s,int a)
{
    SqStack *p;
    p=(SqStack *)malloc(sizeof(SqStack));
    p->data=a;
    p->next=s->next;
    s->next=p;
}
void FBNQ(SqStack *&s,int a,int b,int n)
{
    int sum;
    Push(s,a);
    n--;
    if(n==0)
        return;
    Push(s,b);
    n--;
    if(n==0)
        return;
    while(n!=0)
    {
    sum=s->next->data+s->next->next->data;
    if(sum>9)
    {
        Push(s,sum/10);
        n--;
        if(n==0)
            return;
    }
    Push(s,sum%10);
    n--;
    }
}

int main()
{
    SqStack *s;
    int i,a,b,T;
    int n;
    s=(SqStack *)malloc(sizeof(SqStack));
    s->next=NULL;
    cin>>T;
    for(i=1;i<=T;i++)
    {
        cin>>a>>b>>n;
        FBNQ(s,a,b,n);
        cout<<"Case #"<<i<<": ";
        cout<<s->next->data<<endl;
        
    }
    return 0;
}

 

 

 

posted @ 2012-12-11 22:45  紫清婷  阅读(448)  评论(0编辑  收藏  举报