| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
囧,我在ZJU一直WA,在这里一次就AC了。。什么原因In Reply To:any tricks? i got AC in zju Posted by:richardhuang at 2005-04-23 19:24:33 > rt
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 1024
int min(int a, int b)
{
if(a <= b) {
return a;
}
else {
return b;
}
}
int max(int a, int b)
{
if(a >= b) {
return a;
}
else {
return b;
}
}
void reverse(char *a)
{
int left = 0;
int right = strlen(a)-1;
char temp;
while(left < right) {
temp = a[left];
a[left] = a[right];
a[right] = temp;
left++;
right--;
}
}
void add(char *a, char *b, char *c)
{
int lenA = strlen(a);
int lenB = strlen(b);
int i;
int carry = 0;
int temp;
int len = min(lenA, lenB);
for(i = 0; i < len; i++) {
temp = (a[lenA-1-i] - '0') + (b[lenB-1-i] - '0') + carry;
carry = temp / 10;
c[i] = (temp % 10) + '0';
}
if(lenA <= lenB) {
for(i = len; i < lenB; i++) {
temp = (b[lenB-1-i] - '0') + carry;
carry = temp / 10;
c[i] = (temp % 10) + '0';
}
}
else {
for(i = len; i < lenA; i++) {
temp = (a[lenA-1-i] - '0') + carry;
carry = temp / 10;
c[i] = (temp % 10) + '0';
}
}
if(carry != 0) {
c[max(lenA, lenB)] = carry + '0';
c[max(lenA, lenB)+1] = '\0';
}
else {
c[max(lenA, lenB)] = '\0';
}
reverse(c);
}
int compare(char *a, char *b)
{
int rt = 0;
if(strlen(a) < strlen(b)) {
rt = -1;
}
else if(strlen(a) > strlen(b)) {
rt = 1;
}
else {
rt = strcmp(a, b);
}
return rt;
}
int fibnum(char *a, char *b)
{
int i;
int num = 0;
char f1[SIZE];
char f2[SIZE];
char temp[SIZE];
memset(f1, 0, SIZE);
memset(f2, 0, SIZE);
memset(temp, 0, SIZE);
f1[0] = '1';
f2[0] = '2';
i = 0;
strcpy(temp, a);
while(temp[i] == '0' && i < strlen(a)-1) {
i++;
}
memset(a, 0, SIZE);
strcpy(a, temp+i);
i = 0;
memset(temp, 0, SIZE);
strcpy(temp, b);
while(temp[i] == '0' && i < strlen(b)-1) {
i++;
}
memset(b, 0, SIZE);
strcpy(b, temp+i);
while(1) {
if(compare(b, f1) == -1) {
break;
}
if(compare(a, f1) == -1 || compare(a, f1) == 0) {
num++;
}
memset(temp, 0, SIZE);
add(f1, f2, temp);
memset(f1, 0, SIZE);
strcpy(f1, f2);
memset(f2, 0, SIZE);
strcpy(f2, temp);
}
return num;
}
int main()
{
char a[SIZE];
char b[SIZE];
memset(a, 0, SIZE);
memset(b, 0, SIZE);
while(1) {
memset(a, 0, SIZE);
memset(b, 0, SIZE);
scanf("%s", a);
scanf("%s", b);
if(strcmp(a, "0") == 0 && strcmp(b, "0") == 0) {
break;
}
printf("%d\n", fibnum(a, b));
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator