24 ΠΔΠ - 1η Φάση: Λύσεις
Δημοσιεύτηκε: Σάβ Φεβ 25, 2012 3:39 pm
Ορίστε η δικιά μου
http://pastebin.com/D5jzUPBe
http://pastebin.com/D5jzUPBe
Κώδικας: Επιλογή όλων
#include <stdio.h>
#include <cmath>
typedef unsigned short int u_int16;
typedef unsigned char u_int8;
bool isPrime(u_int16 n) {
u_int8 max = ceil(sqrt(n)); //check up to sqrt
for(u_int16 i = 3; i <= max; i += 2) if (n % i == 0) return 0;
return 1;
}
int main() {
u_int16 x, y;
FILE *in = fopen("function.in", "r"),
*out = fopen("function.out", "w");
fscanf(in, "%5hu %5hu", &x, &y);
if (x > y) {x = x ^ y; y = x ^ y; x = x ^ y;} //swap x,y
x+=(x % 2 == 0)?1:2;//if (x % 2 == 0) x++;
for(; x < y; x += 2) if (isPrime(x)) fprintf(out, "%u ", x);
if (ftell(out)) { //if output not empty
fseek(out, -1, SEEK_END);
fprintf(out, "\n"); //replace last char with \n
}
fclose(in);
fclose(out);
return 0;
}