Quantcast
Channel: Dạy Nhau Học - Latest topics
Viewing all articles
Browse latest Browse all 34359

Nhờ check hộ đoạn code liệt kê số lượng số thoả mãn là số nguyên tố và tổng chữ số của nó là một số trong dãy Fibonacci

$
0
0

@honghm211 wrote:

Một số được coi là đẹp nếu nó là số nguyên tố và tổng chữ số là một số trong dãy Fibonaci. Viết chương trình liệt kê trong một đoạn giữa hai số nguyên cho trước có bao nhiêu số đẹp như vậy

#include <stdio.h>
#include <math.h>

int fib[9];

int fibo(int n) {
    if(n==1 || n==2) return 1;
    if(fib[n]) return fib[n];
    fib[n] = fibo(n-1) +fibo(n-2);
    return fib[n];
}

int checkNt(int n) {
    if( (n>2 && n%2==0) || n<2) return 0;
    int i;
    for(i=3; i<=sqrt(n); ++i) {
        if(n%i==0) return 0;
    }
    return 1;
}

int main() {
    fib[1]=1;
    fib[2]=1;
    fibo(9);
    int a, b, max, min, i, j;
    scanf("%d %d",&a,&b);
    max = a<b?b:a;
    min = a<b?a:b;
    for(i=min; i<=max; i++) {
        int sum=0;
        int temp = i;
        if(checkNt(temp)) {
            while(temp>0) {
                sum+=(temp%10);
                temp /= 10;
            }
            for(j=1; j<10; ++j) {
                if(sum==fib[j]) {
                    printf("%d ", i);
                    break;
                }
            }
        }
    }
    return 0;
}

code em ra kqua đúng nhưng nộp bài ko được, mng check xem em còn thiếu xót gì không ạ

Posts: 5

Participants: 3

Read full topic


Viewing all articles
Browse latest Browse all 34359

Trending Articles