我的朋友说"mod"和"rest"之间存在差异.
如果是这样,那么C和C++的差异是什么?"%"是指C中的"mod"还是"rem"?
#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)
我不知道为什么这个'^'不起作用.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int n,i,ele;
n=5;
ele=pow(n,2);
printf("%d",ele);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是24.
我在Code :: Blocks中使用GNU/GCC.
怎么了?
我知道pow函数返回一个double,但是25适合一个int类型所以为什么这个代码打印24而不是25?如果n=4; n=6; n=3; n=2;代码有效,但有五个代码没有.
最近我写了一段代码:
const int sections = 10;
for(int t= 0; t < 5; t++){
int i = pow(sections, 5- t -1);
cout << i << endl;
}
Run Code Online (Sandbox Code Playgroud)
结果是错误的:
9999
1000
99
10
1
Run Code Online (Sandbox Code Playgroud)
如果我只使用这个代码:
for(int t = 0; t < 5; t++){
cout << pow(sections,5-t-1) << endl;
}
Run Code Online (Sandbox Code Playgroud)
问题不再发生:
10000
1000
100
10
1
Run Code Online (Sandbox Code Playgroud)
有没有人给我解释?非常感谢你!
#include <iostream.h>
#include <math.h>
int main()
{
int j=2;
int output;
output=pow(10,j);
cout<<output;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我把上面的代码写到gcc 12编译器,得到输出99而不是100.我在搜索各种网站时没有得到正确的理由.有编译器问题吗?
我正在用c制作一个新项目,功能"不起作用".我该怎么做才能解决?
它的功能实际为2到10的幂,但10的功率为1
#include <stdio.h>
#include <math.h>
int main(){
int res=2;
int base=10;
int exp=2;
res= pow(base,exp);
printf("%d",res);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我希望输出为100,但输出为99
我正在制作一个简单的计算器,你可以选择一个功能,然后2个输入来得到你的答案.这是一个简洁的小程序,除了权力之外,一切都运行顺畅.每个号码都正常工作.
但根据这个:5^2=24, 5^3=624.我在用pow(number1,number2).
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
for(;;){
int func;
int number1;
int number2;
cout << "Input your function (+,-,x,/,Square,Power)(1,2,3,4,5,6) ";
cin >> func;
cout << "input #1: ";
cin >> number1;
cout << "input #2: ";
cin >> number2;
if (func==1){
int answer;
answer = number1 + number2;
cout << number1 << " + " << number2 << " = " << answer << endl;
}
else {
if (func==2){ …Run Code Online (Sandbox Code Playgroud)