gpt4 book ai didi

检查某个东西是否能被 3 整除

转载 作者:行者123 更新时间:2023-11-30 21:06:42 26 4
gpt4 key购买 nike

我有一些东西要读取一个文本文件,然后是一个像这样的函数文件

int Myiseven(int x)
{
int isOdd = 0;
if (x % 2 == 1) {
isOdd = 1;
}
}

这样所有奇数都会有 isodd = 1

我如何检查一个数字是否可以被三整除

原来的主文件是这个

#define _CRT_SECURE_NO_DEPRECATE


#include<stdio.h>
#include "ProblemHeader_4.h"
int main()
{
FILE *myfile = fopen("input.txt", "w");

for (int i = 1; i <= 33; i++)
{
fprintf(myfile, "%d\n", i);
}
fclose(myfile);

FILE *myfileRead = fopen("input.txt", "r");
FILE *myfileWrite = fopen("outputEven.txt", "w");

int readBuff;
while (!feof(myfileRead))
{
fscanf(myfileRead, "%d", &readBuff);
printf("These numbers were read: %d\n", readBuff);

int isOdd = Myiseven(readBuff);
if (isOdd == 1)
{
fprintf(myfileWrite, "%d\n", readBuff);
printf("This number is divisible by 3: %d\n", readBuff);
}
}
fclose(myfileWrite);
fclose(myfileRead);

return 0;
}

和标题

#ifndef MY_VAR
#define MY_VAR

#include<stdio.h>

int Myiseven(int x);

#endif

最佳答案

看起来您只想打印可被 3 整除的奇数。您可以按如下方式执行此操作:

if (isOdd == 1 && readBuff%3==0)
{
fprintf(myfileWrite, "%d\n", readBuff);
printf("This number is divisible by 3: %d\n", readBuff);
}

此外,您需要在 Myiseven() 函数中使用 return 语句才能成功执行代码:

int Myiseven(int x)
{
int isOdd = 0;
if (x % 2 == 1) {
isOdd = 1;
}
return isOdd;
}

关于检查某个东西是否能被 3 整除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47369643/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com