UVa10334 - Ray Through Glasses

#include <stdio.h>
#include <string.h>
#define MAX 1000
void reverse(char *from, char *to);
void call_add(char *first, char *second, char *result);
char Fin[MAX][MAX];
int main()
{
    char f[MAX], s[MAX], r[MAX];
    int n;
    int i;
    strcpy(Fin[0], "1");
    strcpy(Fin[1], "2");
    for (i = 2; i <= 1000; i++) {
        call_add(Fin[i - 2], Fin[i - 1], Fin[i]);
    }
    while (scanf("%d", &n) != EOF) {
        printf("%s\n", Fin[n]);
    }
    return 0;
}
void reverse(char *from, char *to)
{
    int i, len = strlen(from);
    for (i = 0; i < len; i++)
        to[i] = from[len - 1 - i];
    to[len] = '\0';
}
void call_add(char *first, char *second, char *result)
{
    char F[MAX], S[MAX], Res[MAX];
    int f_len, s_len, i, carry;
    int sum;
    f_len = strlen(first);
    s_len = strlen(second);
    reverse(first, F);
    reverse(second, S);
    for (i = 0, carry = 0; i < f_len && i < s_len; i++) {
        sum = (F[i] - '0') + (S[i] - '0') + carry;
        Res[i] = sum % 10 + '0';
        carry = sum / 10;
    }
    for (; i < f_len; i++) {
        sum = (F[i] - '0') + carry;
        Res[i] = sum % 10 + '0';
        carry = sum / 10;
    }
    for (; i < s_len; i++) {
        sum = (S[i] - '0') + carry;
        Res[i] = sum % 10 + '0';
        carry = sum / 10;
    }
    if (carry)
        Res[i++] = '0' + carry;
    Res[i] = '\0';
    reverse(Res, result);
}

你可能感兴趣的:(include)