C++信息学奥赛一本通-第一部分-基础一-第2章-第5节
C++信息学奥赛一本通-第一部分-基础一-第2章-第5节
2070 数字对调
#include <iostream>using namespace std;int main() {int number; cin >> number;int a = number / 100;int b = number / 10 % 10;int c = number % 100 % 10;int target = c * 100 + b * 10 + a;cout << target;
}
2071 平均分
#include <iostream>using namespace std;int main() {double x, y; cin >> x >> y;double sum_x = x * 87, sum_y = y * 85;double result = (sum_x + sum_y) / (x + y);printf("%.4f", result);
}
2072 歌手大奖赛-增幅减幅都一样那平均不还是一样
#include <iostream>using namespace std;int main() {double a = 9.6;printf("%5.2f", a);
}
2073 三角形面积
#include <iostream>
#include <cmath>using namespace std;int main() {double x, y, z; cin >> x >> y >> z;double p = (x+y+z) / 2.0;double temp = p * (p - x) * (p - y) * (p - z);double s = pow(temp, 0.5);printf("%.3f", s);
}
1029 计算浮点数相除的余
#include <iostream>using namespace std;int main() {double x, y; cin >> x >> y ;double nums = x / y;double remainder = x - (int)nums * y;cout << remainder;
}
1030 计算球的体积-保留小数会四舍五入
#include <iostream>
#define PI 3.14using namespace std;int main() {double r; cin >> r;double V = (PI * r * r *r) * 4 / 3;printf("%.2f", V);
}
1031 反向输出一个三位数-和2070的区别就是补0
#include <iostream>using namespace std;int main() {int number; cin >> number;int a = number / 100;int b = number / 10 % 10;int c = number % 100 % 10;int target = c * 100 + b * 10 + a;printf("%03d", target);
}
1032 大象喝水查
#include <iostream>
#define PI 3.14using namespace std;int main() {double h, r; cin >> h >> r;double V = (PI * r * r) * h;int nums = (int)(20000 / V);printf("%d", nums + 1);
}
1033 计算线段长度
#include <iostream>
#include <cmath>
using namespace std;int main() {double xofa, yofa, xofb, yofb; cin >> xofa >> yofa >> xofb >> yofb;double result = pow(pow(yofb - yofa ,2)+pow(xofb - xofa , 2), 0.5);printf("%.3f",result);
}
1034 计算三角形面积
使用行列式公式计算三角形面积-背的出来就用吧
xa:yb-yc xb:yc - ya xc: ya - yb
#include <iostream>
#include <cmath>using namespace std;
double calculateTriangleArea(double xofa, double yofa, double xofb, double yofb, double xofc, double yofc) {double area = 0.5 * abs((xofa*(yofb - yofc) + xofb*(yofc - yofa) + xofc*(yofa - yofb)));return area;
}
int main() {double xofa, yofa, xofb, yofb, xofc, yofc; cin >> xofa >> yofa >> xofb >> yofb >> xofc >> yofc;double result = calculateTriangleArea(xofa, yofa, xofb, yofb, xofc, yofc);printf("%.2f", result);
}
使用向量-推荐
#include <iostream>
#include <cmath>
using namespace std;
double calculateTriangleArea(double xofa, double yofa, double xofb, double yofb, double xofc, double yofc) {double vecAB_x = xofb - xofa;double vecAB_y = yofb - yofa;double vecAC_x = xofc - xofa;double vecAC_y = yofc - yofa;double cross_product = vecAB_x * vecAC_y - vecAB_y * vecAC_x;return 0.5 * fabs(cross_product);
}int main() {double xofa, yofa, xofb, yofb, xofc, yofc; cin >> xofa >> yofa >> xofb >> yofb >> xofc >> yofc;double result = calculateTriangleArea(xofa, yofa, xofb, yofb, xofc, yofc);printf("%.2f", result);
}
1035 等差数列末项计算
#include <iostream>using namespace std;int main() {int n_of_1, n_of_2, n; cin >> n_of_1 >> n_of_2 >> n;int result = (n_of_2 - n_of_1) * (n-1) + n_of_1;cout << result;
}
1036 A∗BA*BA∗B问题
unsigned int 最高42亿
#include <iostream>using namespace std;int main() {unsigned int a, b; cin >> a >> b;unsigned int result = a * b;cout << result;
}
1037 计算2的幂
#include <iostream>
#include <cmath>
using namespace std;int main() {unsigned int n; cin >> n;unsigned int result = pow(2,n);cout << result;
}
1038 苹果和虫子
#include <iostream>using namespace std;int main() {int n, x, y; cin >> n >> x >> y;int result = (y % x == 0) ? (n - (y / x)) : (n - (y / x) - 1);if (result < 0) result = 0;cout << result;
}