P5735 [深基7.例1] 距离函数
#include <cstdio>
#include <cmath>
double distance(double x1, double y1, double x2, double y2) {
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
int main()
{
double x1, y1, x2, y2, x3, y3;
scanf("%lf%lf%lf%lf%lf%lf", &x1, &y1, &x2, &y2, &x3, &y3);
printf("%.2f\n", distance(x1, y1, x2, y2) + distance(x2, y2, x3, y3) + distance(x1, y1, x3, y3));
return 0;
}
P5737 [深基7.例3] 闰年展示
#include <cstdio>
int ans[2000];
bool isLeap(int y) {
return y % 400 == 0 || y % 4 == 0 && y % 100;
}
int main()
{
int x, y;
scanf("%d%d", &x, &y);
for (int i = x; i <= y; ++i) {
if (isLeap(i)) { ans[0]++; ans[ans[0]] = i;}
}
printf("%d\n", ans[0]);
for (int i = 1; i <= ans[0]; ++i)
printf("%d%c", ans[i], i == ans[0] ? '\n' : ' ');
return 0;
}
P1304 哥德巴赫猜想
#include <cstdio>
bool is_prime(int x) {
if (x < 2) return false;
for (int i = 2; i * i <= x; i++)
if (x % i == 0) return false;
return true;
}
void goldbach(int n) {
for (int i = 2; i <= n / 2; i++)
if (is_prime(i) && is_prime(n - i)) {
printf("%d=%d+%d\n", n, i, n - i);
return;
}
}
int main()
{
int n;
scanf("%d", &n);
for (int i = 4; i <= n; i += 2) goldbach(i);
return 0;
}
P5744 [深基7.习9] 培训
#include <iostream>
#include <string>
using namespace std;
struct Student {
string name;
int age, noip;
Student() {}
Student(string s, int a, int n) {
name = s; age = a; noip = n;
}
};
Student train(Student s) {
s.age++; s.noip = s.noip / 5 * 6;
if (s.noip > 600) s.noip = 600;
return s;
}
int main()
{
int n; cin >> n;
for (int i = 1; i <= n; i++) {
string name; int age, noip;
cin >> name >> age >> noip;
Student one_student(name, age, noip);
one_student = train(one_student);
cout << one_student.name << " " << one_student.age << " " << one_student.noip << "\n";
}
return 0;
}
P5736 [深基7.例2] 质数筛
#include <cstdio>
bool isPrime(int x) {
if (x < 2) return false;
for (int i = 2; i * i <= x; ++i)
if (x % i == 0) return false;
return true;
}
int main()
{
int n;
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
int x;
scanf("%d", &x);
if (isPrime(x)) printf("%d ", x);
}
return 0;
}
P5740 [深基7.例9] 最厉害的学生
#include <iostream>
#include <string>
using namespace std;
struct Info {
string name;
int chinese, math, english;
void read() {
cin >> name >> chinese >> math >> english;
}
int sum() {
return chinese + math + english;
}
void print() {
cout << name << " " << chinese << " " << math << " " << english << "\n";
}
};
Info info[1005];
int main()
{
int n;
cin >> n;
int ans = 1, sum = 0;
for (int i = 1; i <= n; ++i) {
info[i].read();
if (info[i].sum() > sum) {
sum = info[i].sum();
ans = i;
}
}
info[ans].print();
return 0;
}
- 注意到题目中“语文、数学、英语成绩均为不超过 150 的自然数”,因此要考虑到输入的成绩可能最高分为 0 分的情况,可以将求最高分的变量初始值设成负数
P5741 [深基7.例10] 旗鼓相当的对手 - 加强版
#include <cstdio>
#include <cmath>
struct Info {
char name[10];
int s[4]; // s[0]: chinese, s[1]: math, s[2]: english, s[3]: sum
};
Info a[1005];
bool check(int delta, int threshold) {
return abs(delta) <= threshold;
}
bool near(int i, int j) {
for (int k = 0; k < 3; k++)
if (!check(a[i].s[k] - a[j].s[k], 5)) return false;
return check(a[i].s[3] - a[j].s[3], 10);
}
int main()
{
int n;
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%s", a[i].name);
a[i].s[3] = 0;
for (int j = 0; j < 3; j++) {
scanf("%d", &a[i].s[j]); a[i].s[3] += a[i].s[j];
}
}
for (int i = 0; i < n; ++i)
for (int j = i + 1; j < n; ++j)
if (near(i, j)) printf("%s %s\n", a[i].name, a[j].name);
return 0;
}
P5742 [深基7.例11] 评等级
#include <iostream>
#include <string>
using namespace std;
struct Student {
int id, academic, quality;
int overall; //为了减小精度误差,此处overall为真实值的10倍
Student() {}
Student(int _id, int _ac, int _qu) {
id = _id; academic = _ac; quality = _qu;
overall = 7 * _ac + 3 * _qu;
}
int sum() {
return academic + quality;
}
int is_excellent() {
return overall >= 800 && sum() > 140;
}
};
int main()
{
int n; cin >> n;
for (int i = 1; i <= n; i++) {
int tmp_id, tmp_ac, tmp_qu;
cin >> tmp_id >> tmp_ac >> tmp_qu;
Student one_student(tmp_id, tmp_ac, tmp_qu);
if (one_student.is_excellent()) cout << "Excellent\n";
else cout << "Not excellent\n";
}
return 0;
}
标签:return,noip,参考,int,代码,Student,2.3,include,sum
From: https://www.cnblogs.com/ronchen/p/17579850.html