函数与结构体(入门6)

news/2025/1/19 13:39:03/

【深基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;
}


http://www.ppmy.cn/news/1554820.html

相关文章

STM32模拟I2C通讯的驱动程序

目录 STM32模拟I2C通讯的驱动程序 开发环境 引脚连接 驱动程序 STM32模拟I2C通讯的驱动程序 开发环境 立创天空星开发板、主控芯片为STM32F407VxT6 引脚连接 使用stm32的PB9引脚模拟I2C时钟线SCL、PB8引脚模拟I2C数据线SDA 驱动程序 i2c.h文件如下&#xff1a;#ifndef…

MySQL | 尚硅谷 | 第10章_创建和管理表

MySQL笔记&#xff1a;第10章_创建和管理表 文章目录 MySQL笔记&#xff1a;第10章_创建和管理表MySQL笔记&#xff1a;第10章_创建和管理表 1. 基础知识1.1 一条数据存储的过程1.2 标识符命名规则1.3 MySQL中的数据类型 2. 创建和管理数据库2.1 创建数据库2.2 使用数据库2.3 修…

EasyPlayer.js播放器如何在iOS上实现低延时直播?

随着流媒体技术的迅速发展&#xff0c;H5流媒体播放器已成为现代网络视频播放的重要工具。其中&#xff0c;EasyPlayer.js播放器作为一款功能强大的H5播放器&#xff0c;凭借其全面的协议支持、多种解码方式以及跨平台兼容性&#xff0c;赢得了广泛的关注和应用。 那么要在iOS上…

178K⭐排名第一计算机面试笔记

&#x1f389;据说字节大佬于18年找工作时做的面试笔记&#xff0c;且斩获了bat、字节、华为、网易等大厂offer&#xff0c;该笔记被认为是极具价值的实战经验分享&#xff01; &#x1f310; CS-Notes&#xff0c;它为像我这样的程序员提供了一个全面的计算机科学知识库。这个…

STM32串口接收与发送(关于为什么接收不需要中断而发生需要以及HAL_UART_Transmit和HAL_UART_Transmit_IT的区别)

一、HAL_UART_Transmit和HAL_UART_Transmit_IT的区别 1. HAL_UART_Transmit_IT&#xff08;非阻塞模式&#xff09;&#xff1a; HAL_UART_Transmit_IT 是非阻塞的传输函数&#xff0c;也就是说&#xff0c;当你调用 HAL_UART_Transmit_IT 时&#xff0c;它不会等到数据完全发…

基于微信小程序的校园二手交易平台系统设计与开发ssm+论文源码调试讲解

第2章 程序开发技术 2.1 Mysql数据库 为了更容易理解Mysql数据库&#xff0c;接下来就对其具备的主要特征进行描述。 &#xff08;1&#xff09;首选Mysql数据库也是为了节省开发资金&#xff0c;因为网络上对Mysql的源码都已进行了公开展示&#xff0c;开发者根据程序开发需…

Pyside6 --Qt设计师--简单了解各个控件的作用之:Buttons

目录 一、BUttons1.1 包含1.2 不同按钮的解释 二、具体应用2.1 Push Button2.2 Tool Button2.3 Radio Button2.4 Check Box2.5 Command Link Button2.6 Dialog Button Box2.6.1 直接显示代码如下2.6.2 可以修改ok&#xff0c;cancel 的内容 今天学习一下工具箱里面的Buttons&am…

(SAST检测规则-8)连接字符串中的硬编码密码

严重等级&#xff1a;高危 缺陷详解&#xff1a; 在构建数据驱动的应用程序时&#xff0c;开发者通常需要通过数据库连接字符串与数据库进行交互。将敏感信息&#xff08;如密码、服务器IP地址或加密密钥&#xff09;硬编码在源代码中会带来以下风险&#xff1a; 信息暴露&a…