0
Follow
0
View

# Programming to determine whether an integer is a "cow number".

dianhaosu 注册会员
2023-02-27 16:23
wcong123456 注册会员
2023-02-27 16:23

In the modified code, the cow function is used to determine whether a number is a cow. It first determines if the number is prime, and if so returns 0 because primes are not composite. Otherwise, it iterates from $2$to $\sqrt[3]{x}$, determining in turn whether the number can be decomposed into a product of two composite numbers, returning 1 if so, and 0 if not.

In the main function, after reading the data, the is_cow function is called to judge and output the result.

#include
#include

int is_prime(int x) {
if (x <= 1) return 0;
int i, l = sqrt(x);
for (i = 2; i <= l; i++) {
if (x % i == 0) return 0;
}
return 1;
}

int is_cow(int x) {
if (is_prime(x)) return 0;
int i, j, l = pow(x, 1.0 / 3);
for (i = 2; i <= l; i++) {
if (x % i == 0) {
for (j = i + 1; j <= pow(x / i, 0.5); j++) {
if (x % j == 0 && !is_prime(j) && !is_prime(x / i / j)) {
return 1;
}
}
}
}
return 0;
}

int main() {
int n, x, i;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &x);
if (is_cow(x)) {
printf("cow\n");
} else {
printf("no\n");
}
}
return 0;
}


aidalaohuyou 注册会员
2023-02-27 16:23
< div class = "md_content_show e397 data - v - 3967" = "" >

bosses, buggy! < br / > I changed, or the wrong answer

#include
#include
int a[10000];
int is_prime(int x) {
if (x <= 1) return 0;
int i, l = sqrt(x);
for (i = 2; i <= l; i++) {
if (x % i == 0) return 0;
}
return 1;
}

int cow(int x) {
if (is_prime(x)) return 0;
int i, j, l = pow(x, 1.0 / 3);
for (i = 2; i <= l; i++) {
if (x % i == 0) {
for (j = i + 1; j <= pow(x / i, 0.5); j++) {
if (x % j == 0 && !is_prime(j) && !is_prime(x / i / j)) {
return 1;
}
}
}
}
return 0;
}

int main() {
int n, x, i;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for (i = 0; i < n; i++) {
if (is_cow(a[i])) {
printf("cow\n");
} else {
printf("no\n");
}
}
return 0;
}


zt910424 注册会员
2023-02-27 16:23
< div class = "md_content_show e397 data - v - 3967" = "" >

this

#include
#include

using namespace std;

// 判断一个数是否为质数
bool isPrime(int num) {
if (num <= 1) {
return false;
}
int limit = sqrt(num);
for (int i = 2; i <= limit; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}

// 判断一个数是否为“牛数”
bool isCowNum(int num) {
for (int i = 2; i <= num / 2; i++) {
if (!isPrime(i) && !isPrime(num - i)) {
return true;
}
}
return false;
}

int main() {
int t;
cin >> t;
while (t--) {
int num;
cin >> num;
if (isCowNum(num)) {
cout << "cow" << endl;
} else {
cout << "no" << endl;
}
}
return 0;
}



duhai668 注册会员

Publish Time
2023-02-27 16:23
Update Time
2023-02-27 16:23