当前位置: 首页 > news >正文

洛谷day3

B2053 

求一元二次方程 - 洛谷

掌握printf用法;

#include <iostream>
#include <cmath>
using namespace std;
double a,b,c;
double delta;
double x1,x2;int main() {cin>>a>>b>>c;delta = b*b-4*a*c;if(delta>0){x1 = (-b+sqrt(delta))/(2*a);x2 = (-b-sqrt(delta))/(2*a);if(x1>x2)printf("x1=%.5lf;x2=%.5lf",x2,x1);else{printf("x1=%.5lf;x2=%.5lf",x1,x2);}}else if(delta==0){x1 = (-b+sqrt(delta))/(2*a);printf("x1=x2=%.5lf",x1);}else{cout<<"No answer!";}
}

减少代码量,利用swap算法,交换x1,x2值;

#include <iostream>
#include <cmath>
using namespace std;
double a,b,c;
double delta;
double x1,x2;int main() {cin>>a>>b>>c;delta = b*b-4*a*c;if(delta>0){x1 = (-b+sqrt(delta))/(2*a);x2 = (-b-sqrt(delta))/(2*a);if(x1>x2)swap(x1,x2);printf("x1=%.5lf;x2=%.5lf",x1,x2);}else if(delta==0){x1 = (-b+sqrt(delta))/(2*a);printf("x1=x2=%.5lf",x1);}else{cout<<"No answer!";}
}

 B3854

注意必须得是long long型,不然会有部分测试不通过。

#include <iostream>
using namespace std;
long long m,n,a,b;
int main() {cin>>m>>n>>a>>b;if((m-a)*n>=b+1)cout<<"Program ends with return value 0.";elsecout<<"Segmentation fault.";
}

知识小拓展

在 C++ 中,`ios::sync_with_stdio(false);` 是一条指令,用来关闭 C++ 的 `iostream` 类库(如 `cin` 和 `cout`)与 C 的标准输入输出库(如 `scanf` 和 `printf`)之间的同步。默认情况下,这两个库是同步的,以保证你可以在同一个程序中混合使用它们,并按照预期的顺序进行输入和输出操作。

当你调用 `ios::sync_with_stdio(false);` 之后,这种同步被关闭,这样做可以提高 `iostream` 的输入输出效率,因为取消了与 C 标准库的额外同步操作。但是,一旦同步关闭,就不应该混用 `iostream` 和 C 的标准输入输出库,因为它们的输出可能不会按照你写代码时的顺序出现。

这条指令经常用在需要大量输入输出操作,且对程序运行时间要求很高的情况,比如算法竞赛,因为它可以显著减少输入输出所需的时间。

下面是一个例子,展示如何在程序中使用它:

#include <iostream>int main() {// 关闭同步std::ios::sync_with_stdio(false);// 取消 cin 与 stdin 的绑定,以提高读取效率std::cin.tie(NULL);// 你的代码逻辑std::cout << "Fast output without sync!\n";// 后续不要使用任何 C 的标准输入输出函数return 0;
}

请注意,调用 `ios::sync_with_stdio(false);` 之后,还应该调用 `cin.tie(NULL);` 来解除 `cin` 与 `cout` 之间的绑定,这样可以进一步提升输入输出的效率。这条指令会导致 `cin` 在每次从 `cout` 输出之前不再自动刷新缓冲区。

B3825

#include<iostream>
using namespace std;
int x,h;
int main(){ios::sync_with_stdio(0);//不能再混用 C 和 C++ 的输入输出功能,iostream 库的效率会提高cin.tie(0);cin>>x>>h;if(x<10) cout<<"Drizzle";else if(x>=10 && x<25) cout<<"Moderate Rain";else if(x>=25 && x<50) cout<<"Heavy Rain";	else if(x>=50) cout<<"Torrential Rain";if(h==1){if(x>=20) cout<<endl<<"YES";else cout<<endl<<"NO";}
}

B3814-可多看几次

for (auto it = direcion.begin(); it != direcion.end(); ++it) {pair<int, int>& move = *it;int nx = mx + move.first;int ny = my + move.second;
#include<iostream>
#include <vector>
#include <utility> 
#include<set>
using namespace std;//判断棋子是否在棋盘内
bool inBoard(int x,int y){return x >= 1 && x <= 10 && y >= 1 && y <= 9;
}int main(){int sx, sy, cx, cy, mx, my; cin >> sx >> sy >> cx >> cy >> mx >> my;//c++11之前不支持// std::vector<std::pair<int, int> > moves = {// {-2, 1}, {-2, -1}, {-1, 2}, {-1, -2},// {1, 2}, {1, -2}, {2, 1}, {2, -1}// };vector<pair<int, int> > direcion;direcion.push_back(make_pair(-2, 1));direcion.push_back(make_pair(-2, -1));direcion.push_back(make_pair(-1, 2));direcion.push_back(make_pair(-1, -2));direcion.push_back(make_pair(1, 2));direcion.push_back(make_pair(1, -2));direcion.push_back(make_pair(2, 1));direcion.push_back(make_pair(2, -1));// 存储马在移动后可以攻击的位置set<pair<int, int> >attackPositions;//遍历马的移动for (auto &move : direcion) {int nx = mx + move.first;int ny = my + move.second;// 如果移动后仍在棋盘内if (inBoard(nx, ny)) {// 遍历该位置马可以攻击的所有位置for (auto &attack : direcion) {int ax = nx + attack.first;int ay = ny + attack.second;// 如果攻击位置在棋盘内,则添加到攻击位置集合中if (inBoard(ax, ay)) {attackPositions.insert(make_pair(ax, ay));}}}}// 检查帅和车是否在马的攻击范围内if (attackPositions.count(make_pair(sx, sy)) > 0 && attackPositions.count(make_pair(cx, cy)) > 0) {cout << "Yes" << endl;} else {cout << "No" << endl;}return 0;}

B3720(可复看)

#include <cstddef>
#include <iostream>
#include <string>using namespace std;int main() {long long x;cin >> x;string dishes;cin >> dishes;// 检查是否购买了 B 菜和 C 菜bool hasB = dishes.find('B') != string::npos;//string::npos 是一个常量,表示未找到的位置。它的值通常是最大的有效索引值加一bool hasC = dishes.find('C') != string::npos;// 根据购买的菜品计算折扣if (hasB && hasC) {// 两种菜都买了,打六折x = x * 6 / 10;} else if (hasB) {// 只买了 B 菜,打八折x = x * 8 / 10;} else if (hasC) {// 只买了 C 菜,打七折x = x * 7 / 10;}cout << x << endl;return 0;
}

字符方法,进行简单判断: 

#include <iostream>using namespace std;int main() {long long x;cin >> x;char dish1, dish2;cin >> dish1 >> dish2;// 检查是否购买了 B 菜和 C 菜bool hasB = dish1 == 'B' || dish2 == 'B';bool hasC = dish1 == 'C' || dish2 == 'C';// 根据购买的菜品计算折扣if (hasB && hasC) {// 两种菜都买了,打六折x = x * 6 / 10;} else if (hasB) {// 只买了 B 菜,打八折x = x * 8 / 10;} else if (hasC) {// 只买了 C 菜,打七折x = x * 7 / 10;}cout << x << endl;return 0;
}

count方法

#include <iostream>
#include <string>
#include <algorithm> // 引入算法库,以使用 std::countusing namespace std;int main() {long long x;cin >> x;string dishes;cin >> dishes; // 读取两个字符作为一个字符串// 使用 count 统计 B 和 C 的出现次数int countB = count(dishes.begin(), dishes.end(), 'B');int countC = count(dishes.begin(), dishes.end(), 'C');// 检查是否购买了 B 菜和 C 菜bool hasB = countB > 0;bool hasC = countC > 0;// 根据购买的菜品计算折扣if (hasB && hasC) {// 两种菜都买了,打六折x = x * 6 / 10;} else if (hasB) {// 只买了 B 菜,打八折x = x * 8 / 10;} else if (hasC) {// 只买了 C 菜,打七折x = x * 7 / 10;}cout << x << endl;return 0;
}

B3677

简单分类讨论即可。

#include<iostream>
#include<cmath>
using namespace std;int main(){long long a,b;cin>>a>>b;if(b==0)cout<<"NO"<<endl<<"YES"<<endl;else{if(a>=0){cout<<"NO"<<endl;}else{if(b%2==0){cout<<"NO"<<endl;}else{cout<<"YES"<<endl;}}if(abs(a)%2==1){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}}}

相关文章:

  • Redis是如何避免“数组+链表”的过长问题
  • React【Day1】
  • 【大模型】在VS Code(Visual Studio Code)上安装中文汉化版插件
  • [激光原理与应用-78]:激光加工(如打标)的各种笔参数与含义解读
  • MCGS学习——用户管理
  • XUbuntu22.04之安装Plantuml(二百二十三)
  • Camera入门基础知识
  • UGUI源码分析与研究2-从底层实现的角度去分析和调优UI的性能问题和疑难杂症
  • 加载三维模型,加载时黑的?
  • 前端视角如何理解“时间复杂度O(n)”
  • 【算法】小强爱数学(迭代公式+数论取模)
  • Unity学习笔记 6.2D换帧动画
  • Java后端八股----JVM篇
  • RuoYi-Vue-Plus(基础知识点jackson、mybatisplus、redis)
  • 十.pandas方法总结Numpy
  • .pyc 想到的一些问题
  • JavaScript 事件——“事件类型”中“HTML5事件”的注意要点
  • JavaSE小实践1:Java爬取斗图网站的所有表情包
  • Laravel Telescope:优雅的应用调试工具
  • SOFAMosn配置模型
  • SwizzleMethod 黑魔法
  • Web设计流程优化:网页效果图设计新思路
  • 闭包,sync使用细节
  • 程序员最讨厌的9句话,你可有补充?
  • 创建一种深思熟虑的文化
  • 关于字符编码你应该知道的事情
  • 如何学习JavaEE,项目又该如何做?
  • 不要一棍子打翻所有黑盒模型,其实可以让它们发挥作用 ...
  • ​创新驱动,边缘计算领袖:亚马逊云科技海外服务器服务再进化
  • ​中南建设2022年半年报“韧”字当头,经营性现金流持续为正​
  • # centos7下FFmpeg环境部署记录
  • # 达梦数据库知识点
  • #LLM入门|Prompt#3.3_存储_Memory
  • #大学#套接字
  • #我与Java虚拟机的故事#连载09:面试大厂逃不过的JVM
  • $.ajax中的eval及dataType
  • (4) PIVOT 和 UPIVOT 的使用
  • (echarts)echarts使用时重新加载数据之前的数据存留在图上的问题
  • (LeetCode C++)盛最多水的容器
  • (Python) SOAP Web Service (HTTP POST)
  • (Redis使用系列) Springboot 使用redis实现接口Api限流 十
  • (八)五种元启发算法(DBO、LO、SWO、COA、LSO、KOA、GRO)求解无人机路径规划MATLAB
  • (附源码)springboot 校园学生兼职系统 毕业设计 742122
  • (全注解开发)学习Spring-MVC的第三天
  • (十一)JAVA springboot ssm b2b2c多用户商城系统源码:服务网关Zuul高级篇
  • (学习日记)2024.02.29:UCOSIII第二节
  • (一) springboot详细介绍
  • (转)菜鸟学数据库(三)——存储过程
  • .net core webapi 大文件上传到wwwroot文件夹
  • .net core开源商城系统源码,支持可视化布局小程序
  • .Net Core与存储过程(一)
  • .NET 简介:跨平台、开源、高性能的开发平台
  • .net 受管制代码
  • .NET/C# 检测电脑上安装的 .NET Framework 的版本
  • .NET国产化改造探索(三)、银河麒麟安装.NET 8环境