- helps you find a similar problem, you can look at: https://ask.csdn.net/questions/7663299
Let's study the properties of integers. We know that prime numbers only have two factors of 1 and itself, and composite numbers at least have other factors besides 1 and itself. We also know that "big cat number" can only be decomposed into the product of two prime numbers, then what about the number that can be decomposed into two composite numbers? We call this the "cattle count." The following programming to determine whether the integer is "cow number".
Input
The first behavior t(1≤t≤100) indicates the number of test data sets.
Next t rows, each with a positive integer x.
Output
For each input data x, determine whether it is "cow count" and print a string: if it is "cow count", print "no" otherwise. < br / > < br / > 2 sample input 15 < br / > < br / > < br / > 36 sample output < br / > no < br / > "
OJ indicates that the answer is wrong. How to correct
?#include <stdio.h>
#include <math.h>
int a[10000];
int prime(int x)
{
int i,l;
l=sqrt(x);
for(i=2;i<=l;i++)
{
if(x%i==0) return 0;
}
return 1;
}
int main()
{
int n,cow,i,flag,j;
scanf("%d",&n);
for(j=0;j<n;j++)
{
scanf("%d",&a[j]);
}
for(j = 0;j < n; j++){
flag=0;
for(i=4;i<=(sqrt(a[j]));i++)
{
if(a[j]%i==0 && prime(i)==0 && prime(a[j]/i)==0)
{
printf("cow\n");
flag=1;
break;
}
}
if(flag==0) printf("no\n");
}
return 0;
}
0 Answer
This answer quotes ChatGPT
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;
}
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;
}
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;
}
这家伙很懒,什么都没留下...