Integers: Recreation One (C語言CodeWars)

解題思路:

(1)解法很暴力

(2)就是指針不太熟悉,容易出錯,哈哈哈

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

typedef struct Pair Pair;
struct Pair {
    long long first;
    long long snd;
};
// fill length with the number of pairs in your array of pairs
Pair** listSquared(long long m, long long n, int* length) {
    // your code
    Pair **p = (Pair**)calloc(0,sizeof(Pair*));
    int count = 0;
    long long sum = 0,temp;
    for(long long i = m;i<=n;i++) {
	sum=0;
	for(long long j=1;j<=i;j++) {
	    if (i%j==0) sum+=j*j;
	}
	temp = sqrt(sum);
	if (temp*temp==sum) {
            p = (Pair**)realloc(p,(count+1)*sizeof(Pair*));
	    Pair* s = (Pair*)calloc(1, sizeof(Pair));
	    s->first = i;
	    s->snd = sum;
	    p[count++] = s;
	}
    }
    *length=count;
    return p;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章