除法的 C++ 问题

问题描述

我需要用 C++ 编写一个程序,在其中插入一个 4 位数的数字“n”,这是一个简单的数字,在控制台中,控制台向我显示某个数字与 4 位数的乘法,我首先需要写。 我尝试编写 (n/!2) 以使程序仅在数字不能被 2 整除且控制台显示 helm.go:81: [debug] error converting YAML to JSON: yaml: line 2: mapping values are not allowed in this context YAML parse error on argo-apps/templates/microservices.yml helm.sh/helm/v3/pkg/releaseutil.(*manifestFile).sort /home/circleci/helm.sh/helm/pkg/releaseutil/manifest_sorter.go:146 helm.sh/helm/v3/pkg/releaseutil.SortManifests /home/circleci/helm.sh/helm/pkg/releaseutil/manifest_sorter.go:106 helm.sh/helm/v3/pkg/action.(*Configuration).renderResources /home/circleci/helm.sh/helm/pkg/action/action.go:165 helm.sh/helm/v3/pkg/action.(*Install).Run /home/circleci/helm.sh/helm/pkg/action/install.go:239 main.runInstall /home/circleci/helm.sh/helm/cmd/helm/install.go:241 main.newTemplateCmd.func2 /home/circleci/helm.sh/helm/cmd/helm/template.go:70 github.com/spf13/cobra.(*Command).execute /go/pkg/mod/github.com/spf13/cobra@v1.0.0/command.go:842 github.com/spf13/cobra.(*Command).ExecuteC /go/pkg/mod/github.com/spf13/cobra@v1.0.0/command.go:950 github.com/spf13/cobra.(*Command).Execute /go/pkg/mod/github.com/spf13/cobra@v1.0.0/command.go:887 main.main /home/circleci/helm.sh/helm/cmd/helm/helm.go:80 runtime.main /usr/local/go/src/runtime/proc.go:203 runtime.goexit /usr/local/go/src/runtime/asm_amd64.s:1373``` Any ideas?? 我试过删除 (n /! 2) 并且代码执行没有问题。如果有人能告诉我如何修复它,我会很高兴。

"main.cpp:22:36: warning: division by zero [-Wdiv-by-zero]".

解决方法

在计算 n / !2 中,您有效地执行了 n / 0

!not 的缩写,not 2 是一个布尔表达式,其中 2(除 0 之外的任何东西)是 true。>

not truefalse

false 在计算中被提升为 intfalse 变成 0

解决方案:使用 n % 2 != 0n & 1 检查奇数。

,

您正在寻找模运算。如果数字可以被另一个整除,则模数(除法的余数)为零,否则为非零。

if (n >=1000 && n <= 9999 && (n % 2) != 0)
,

尝试编写 (n/!2) 以使程序仅在不能被 2 整除的情况下执行

n / !2

计算结果为 n / 0。这会触发您的警告。

要检查 n 是否为奇数,您应该这样做:

n % 2 != 0
,

!运算符的优先级高于 / ,因此您的表达式变为

=> n/(!2) => n/0

相反,您应该使用 (n%2==0) 来检查偶数。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...