#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int main (){
double x1 , y1 ,x2 , y2;
cin >> x1 >> y1 >> x2 >> y2;
printf("%.4f",sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)));
return 0;
}
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int main (){
double x1 , y1 ,x2 , y2;
cin >> x1 >> y1 >> x2 >> y2;
printf("%.4f",sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)));
return 0;
}
#include <iostream>
#include <cstdio>
using namespace std;
int main (){
int x;
double y;
cin >> x >> y;
printf("%.3f km/l", x/y);
return 0;
}
#include <iostream>
#include <cstdio>
using namespace std;
int main (){
int number,hour;
double money;
cin>> number >> hour >> money;
printf("NUMBER = %d\nSALARY = U$ %.2lf\n", number ,hour*money );
return 0;
}
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
double a,b;
cin >> a >> b;
printf("MEDIA = %.5lf",(3.5*a + 7.5*b)/(3.5 + 7.5));
return 0;
}
#include <iostream>
#include <cstdio>
using namespace std;
int main (){
double r;
cin >> r;
//cout << "A=" << 3.14159*R*R << endl;
printf("A=%.4f\n",3.14159*r*r); //用浮点数表示,保留四位小数。
return 0;
}
出现这样的问题是因为精度不够
在C环境下写了如下代码:
#include <cstdio>
int main (){
int a,b,c,d;
scanf("%d%d%d%d",&a,&b,&c,&d);
printf("DIFERENCA = %d\n",a * b - c*d);
return 0;
}
报错:
fatal error: cstdio: No such file or directory
compilation terminated.
原因:
cstdio是C库中的头文件,不存在C的编译环境中,此处应将 cstdio 改成 stdio.h.
故:该代码在C的环境下可以运行,但是在C的环境下不可以运行
题目链接 (https://www.acwing.com/problem/content/610/)
我遇到了cin后加 endl; 报错问题。
错误的代码:
#include <iostream>
using namespace std;
int main(){
int A , B , C , D;
cin >> A >> B >> C >> D >> endl ;
cout<< "DIFERENCA = " << A * B - C * D << endl;
return 0;
}
编译器报了什么错误?
a.cpp: In function 'int main()':
a.cpp:8:29: error: no match for 'operator>>' (operand types are 'std::basic_istream<char>::__istream_type' {aka 'std::basic_istream<char>'} and '<unresolved overloaded function type>')
8 | cin >> A >> B >> C >> D >> endl ;
百度后得到解答:
cin代表标准输入设备,使用提取运算符 “>>” 从设备键盘取得数据,送到输入流对象cin中,然后送到内存。使用cin可以获得多个从键盘的输入值,其具体使用格式如下:
cin >> 表达式1 >>表达式2 … >> 表达式n;
这个是c++中做的语法规定。
C++写法
#include <iostream>
using namespace std;
int main(){
int A , B , C , D;
cin >> A >> B >> C >> D ;
cout<< "DIFERENCA = " << A * B - C * D << endl;
return 0;
}
}
C在C++环境下的写法
#include <cstdio>
int main (){
int a,b,c,d;
scanf("%d%d%d%d",&a,&b,&c,&d);
printf("DIFERENCA = %d\n",a * b - c*d);
return 0;
}
C编译环境下的C语言写法。
#include <stdio.h>
//C++中用的 include <cstdio> 这个头文件在C中没有,要注意C不能用,会报fatal错误
int main (){
int a,b,c,d;
scanf("%d%d%d%d",&a,&b,&c,&d);
printf("DIFERENCA = %d\n",a * b - c*d);
return 0;
}
#include <iostream>
using namespace std;
int main(){
int A , B;
cin >> A >> B;
cout << A + B <<endl;
return 0;
}