【深基7.例1】距离函数
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{double x1, x2, x3, y1, y2, y3;cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;double d1 = pow(pow(x2 - x1, 2) + pow(y2 - y1, 2), 0.5);double d2 = pow(pow(x3 - x1, 2) + pow(y3 - y1, 2), 0.5);double d3 = pow(pow(x3 - x2, 2) + pow(y3 - y2, 2), 0.5);cout <<fixed<< setprecision(2) << d1 + d2 + d3 << endl;return 0;
}
注意:
头文件cmath(pow的头文件)以及输出两位小数的方式
【深基7.例2】质数筛
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;bool fun(int x)
{if (x == 0 || x == 1)return false;if (x == 2)return true;if (x % 2 == 0)return false;else {for (int i = 3; i <= sqrt(x); i++) {if (x % i == 0)return false;}}return true;
}int main()
{int n;cin >> n;vector<int>vec(n);for (int i = 0; i < n; i++) {cin >> vec[i];}bool flag= true;for (int i = 0; i < n; i++) {if (fun(vec[i])) {cout << vec[i] << ' ';}}return 0;
}
注意:
在判断是否为素数的函数中,for循环内应该是i<=sqrt(x)
【深基7.例3】闰年展示
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;bool solve(int x)
{if (x % 100 == 0) {if (x % 400 == 0)return true;else return false;}else {if (x % 4 == 0)return true;else return false;}
}int main()
{int m, n;cin >> m >> n;vector<int>vec;int count = 0;for (int i = m; i <= n; i++) {if (solve(i)) {count++;vec.push_back(i);}}cout << count << endl;for (int i = 0; i < vec.size(); i++) {cout << vec[i] << " ";}return 0;
}
【深基7.例4】歌唱比赛
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;int main()
{int m, n;cin >> n >> m;vector<int>vec;for (int i = 0; i < n; i++) {int maxn = -1;int minn = 11;int x;int sum = 0;for (int j = 0; j < m; j++) {cin >> x;if (maxn < x)maxn = x;if (minn > x)minn = x;sum += x;}sum = sum - minn - maxn;vec.push_back(sum);}int maxn = -1;for (int i = 0; i < vec.size(); i++) {if (vec[i] > maxn)maxn = vec[i];}cout << fixed << setprecision(2) << 1.0 * maxn / (m - 2) << endl;return 0;
}
【深基7.例7】计算阶乘
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;long long sum = 1;
int n;
void solve(int x)
{sum *= x;if (x == n)return;solve(++x);
}int main()
{cin >> n;solve(1);cout << sum << endl;return 0;
}
注意:
在递归的时候,sum*=x应该写在判断x是否等于n之前,感觉是我递归不熟练,导致了错误
赦免战俘
#include <iostream>
#include <cstring>
using namespace std;bool a[2000][2000];void dfs(int x, int y, int n)
{if (n) {for (int i = x; i < n / 2 + x; i++) {for (int j = y; j < n / 2 + y; j++) {a[i][j] = 0;}}dfs(n / 2 + x, y, n / 2);//右上角dfs(x, n / 2 + y, n / 2);//左下角dfs(n / 2 + x, n / 2 + y, n / 2);//右下角}
}int main()
{int n;cin >> n;n = 1 << n;memset(a, true, sizeof(a));dfs(0, 0, n);for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {cout << a[i][j] << " ";}cout << endl;}return 0;
}
注意:
这里我借鉴的别人代码,由于本人对指针实在是不太了解,所以没有用大佬方法的指针,不过发现不用指针也可以做;
主要是模拟,然后递归遍历右上角,左下角,右下角;此外,数组a设置成了int类型的话,然后memset(a,1,sizeof(a)),不知道为什么在输出的时候出现了乱码,我现在还无法解释;后面把它设置成了bool类型,memset(a,true,sizeof(a))就没问题。
【深基7.例9】最厉害的学生
#include <iostream>
#include <cstring>
#include <string>
using namespace std;struct score
{string name;int c;int m;int e;int sum;
}a[1005];int main()
{int n;cin >> n;for (int i = 0; i < n; i++) {cin >> a[i].name >> a[i].c >> a[i].m >> a[i].e;a[i].sum = a[i].c + a[i].m + a[i].e;}int maxn = 0;int index = 0;for (int i = 0; i < n; i++) {if (a[i].sum > maxn) {maxn = a[i].sum;index = i;}}cout << a[index].name << " " << a[index].c << " " << a[index].m << " " << a[index].e << endl;return 0;
}
【深基7.例10】旗鼓相当的对手 - 加强版
#include <iostream>
#include <cstring>
#include <string>
using namespace std;struct score
{string name;int c;int m;int e;int sum;
}a[1005];int main()
{int n;cin >> n;for (int i = 0; i < n; i++) {cin >> a[i].name >> a[i].c >> a[i].m >> a[i].e;a[i].sum = a[i].c + a[i].m + a[i].e;}for (int i = 0; i < n; i++) {for (int j = i + 1; j < n; j++) {if (abs(a[i].sum - a[j].sum) <= 10&& abs(a[i].m - a[j].m) <= 5&& abs(a[i].c - a[j].c) <= 5&& abs(a[i].e - a[j].e) <= 5) {cout << a[i].name << " " << a[j].name << endl;}}}return 0;
}
【深基7.例11】评等级
#include <iostream>
#include <cstring>
#include <string>
using namespace std;struct score
{int num;int score;int expand;int sum;void solve() {if (this->expand + this->score > 140 && this->sum >= 800) {cout << "Excellent" << endl;}else {cout << "Not excellent" << endl;}}
}a[1005];int main()
{int n;cin >> n;for (int i = 0; i < n; i++) {cin >> a[i].num >> a[i].score >> a[i].expand ;a[i].sum = 7*a[i].score + 3*a[i].expand ;}for (int i = 0; i < n; i++) {a[i].solve();}return 0;
}
[NOIP2012 普及组] 质因数分解
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
using namespace std;bool isPrime(int x)
{if (x < 2)return false;if (x == 2)return true;for (int i = 2; i <= sqrt(x); i++) {if (x % i == 0)return false;}return true;
}int main()
{int n;cin >> n;for (int i = 2; i < n; i++) {if (n % i == 0) {int temp = n / i;if (isPrime(i) && isPrime(temp)) {cout << max(i, temp) << endl;break;}}}return 0;
}
哥德巴赫猜想
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
using namespace std;bool isPrime(int x)
{if (x < 2)return false;if (x == 2)return true;for (int i = 2; i <= sqrt(x); i++) {if (x % i == 0)return false;}return true;
}int main()
{int n;cin >> n;for (int i = 4; i <= n; i+=2) {for (int j = 2; j < n; j++) {int temp = i - j;if (isPrime(j) && isPrime(temp)) {cout << i << "=" << j << "+" << temp << endl;break;}}}return 0;
}
[USACO1.5] 回文质数 Prime Palindromes
如果用字符串来判断是否为回文数字,会超时:
bool isPalindrome(string x)
{for (int i = 0, j = x.length() - 1; i < j; i++, j--) {if (x[i] != x[j])return false;}return true;
}
解决方法一:把数字反转,而不是用字符串
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
using namespace std;bool isPalindrome(int x)
{int n = x;int reverse = 0;while (n > 0) {reverse = reverse * 10 + n % 10;n /= 10;}if (reverse == x)return true;return false;
}bool isPrime(int x)
{for (int i = 2; i <= sqrt(x); i++) {if (x % i == 0)return false;}return true;
}int main()
{int low, high;cin >> low >> high;for (int i = low; i <= high; i++) {if (isPalindrome(i) && isPrime(i)) {cout << i << endl;}}return 0;
}
解决方法二:判断素数的方法换成埃拉托斯特尼筛法
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
using namespace std;bool isPalindrome(string x)
{for (int i = 0, j = x.length() - 1; i < j; i++, j--) {if (x[i] != x[j])return false;}return true;
}// 普通埃拉托斯特尼筛法,找出 2 到 sqrt(b) 范围内的所有质数
void sieve(int limit, vector<int>& primes) {vector<bool> isPrime(limit + 1, true);isPrime[0] = isPrime[1] = false; // 0和1不是质数for (int i = 2; i <= limit; i++) {if (isPrime[i]) {primes.push_back(i);for (int j = i * i; j <= limit; j += i) {isPrime[j] = false;}}}
}// 分段筛法,找出区间 [low, high] 内的质数
void segmentedSieve(int low, int high) {// 计算最大值 high 的平方根int limit = sqrt(high) + 1;// 获取小于等于 sqrt(high) 的所有质数vector<int> primes;sieve(limit, primes);// 创建一个区间 [low, high] 的布尔数组,初始化为 true(表示是质数)vector<bool> isPrime(high - low + 1, true);// 对于每一个小于等于 sqrt(high) 的质数,标记出在 [low, high] 范围内的倍数for (int p : primes) {// 找到 [low, high] 范围内的 p 的第一个倍数int start = max(p * p, (low + p - 1) / p * p); // p^2 或者从低范围开始的倍数// 标记 [low, high] 范围内的 p 的倍数为非质数for (int j = start; j <= high; j += p) {isPrime[j - low] = false;}}// 输出 [low, high] 范围内的回文质数for (int i = 0; i <= high - low; i++) {if (isPrime[i] && (low + i) >= 2 && isPalindrome(to_string(low + i))) {cout << low + i << endl;}}
}int main()
{int low, high;cin >> low >> high;// 使用分段筛法查找 [a, b] 范围内的回文质数segmentedSieve(low, high);return 0;
}
集合求和
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
using namespace std;int a[31];int main()
{int i = 0;long long sum = 0;while (cin >> a[i++]);for (int j = 0; j < i; j++) {sum += a[j];}sum *= pow(2, i - 2);cout << sum << endl;return 0;
}
注意:
这里需要用到一个推导,,推导过程大家可以自己去网上查找
【深基7.习8】猴子吃桃
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
using namespace std;int main()
{int day;cin >> day;int n = 1;for (int i = 1; i < day; i++) {n = (n + 1) * 2;}cout << n << endl;return 0;
}
【深基7.习9】培训
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
using namespace std;struct info
{string name;int age;int score;
}a[10];int main()
{int n;cin >> n;for (int i = 0; i < n; i++) {cin >> a[i].name >> a[i].age >> a[i].score;if (1.2 * a[i].score <= 600)a[i].score *= 1.2;else a[i].score = 600;}for (int i = 0; i < n; i++) {cout << a[i].name << " " << a[i].age + 1 << " " << a[i].score << endl;}return 0;
}