洛谷day3

文章介绍了C++中解决一元二次方程的代码,展示了printf用法,以及如何通过`swap`算法简化代码。同时讨论了`ios::sync_with_stdio`和`cin.tie`在提高输入输出效率中的作用,并给出了几个实际问题的代码片段,如判断是否满足特定条件和计算折扣。
摘要由CSDN通过智能技术生成

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.";
	else
		cout<<"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::count

using 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;
        }
    }
    
}

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力的小羽儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值