#include <stdio.h>
void main(void)
{
int a;
int result;
int sum = 0;
printf("Enter a number: ");
scanf("%d", &a);
for( int i = 1; i <= 4; i++ )
{
result = a ^ i;
sum += result;
}
printf("%d\n", sum);
}
Run Code Online (Sandbox Code Playgroud)
我不知道为什么这个'^'不起作用.
Ser*_*nov 73
好吧,首先,^
C/C++中的运算符是逐位XOR.它与权力无关.
现在,关于使用该pow()
函数的问题,一些谷歌搜索显示将其中一个参数转换为double有助于:
result = (int) pow((double) a,i);
Run Code Online (Sandbox Code Playgroud)
请注意,我也将结果转换int
为所有pow()
重载返回double,而不是int
.我没有可用的MS编译器,所以我无法检查上面的代码.
由于C99,也有float
和long double
调用函数powf
和powl
分别,如果这是任何帮助.
peo*_*oro 63
在C中^
是按位异或:
0101 ^ 1100 = 1001 // in binary
Run Code Online (Sandbox Code Playgroud)
没有操作员的电源,你需要使用pow
math.h中的函数(或其他类似的函数):
result = pow( a, i );
Run Code Online (Sandbox Code Playgroud)