C++连续读入数据
在BestCoder網站上,常常是讓用戶連續讀入數據。C++可以用下面的方法:
# include<iostream> # include<iomanip> # include<cmath> using namespace std;int main() {double x1, y1, x2, y2;while (cin >> x1 >> y1 >> x2 >> y2)cout << setiosflags(ios::fixed) << setprecision(2) << sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)) << endl;return 0; }在bestcoder官網上說,C++的cin和cout的效率比printf和scanf的效率低,因此可以用下面的方法
# include<iostream>int main() {int a, b;while (scanf("%d%d", &a, &b) != EOF)printf("%d\n", a+b);return 0; }scanf和printf都在iostream中有定義
如果程序在visual studio中運行,可能會報錯,說scanf是過時的用法,不能使用。可以在visual stuido的【工程屬性】【C/C++】【命令行】中添加
/D _CRT_SECURE_NO_WARNINGS,這樣就可以運行了。
總結
- 上一篇: sublime配置运行python文件的
- 下一篇: 一个python网上文档