柚子快報邀請碼778899分享:算法 洛谷day3
柚子快報邀請碼778899分享:算法 洛谷day3
B2053?
求一元二次方程 - 洛谷
掌握printf用法;
#include
#include
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
#include
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
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);` 是一條指令,用來關(guān)閉 C++ 的 `iostream` 類庫(如 `cin` 和 `cout`)與 C 的標(biāo)準(zhǔn)輸入輸出庫(如 `scanf` 和 `printf`)之間的同步。默認情況下,這兩個庫是同步的,以保證你可以在同一個程序中混合使用它們,并按照預(yù)期的順序進行輸入和輸出操作。
當(dāng)你調(diào)用 `ios::sync_with_stdio(false);` 之后,這種同步被關(guān)閉,這樣做可以提高 `iostream` 的輸入輸出效率,因為取消了與 C 標(biāo)準(zhǔn)庫的額外同步操作。但是,一旦同步關(guān)閉,就不應(yīng)該混用 `iostream` 和 C 的標(biāo)準(zhǔn)輸入輸出庫,因為它們的輸出可能不會按照你寫代碼時的順序出現(xiàn)。
這條指令經(jīng)常用在需要大量輸入輸出操作,且對程序運行時間要求很高的情況,比如算法競賽,因為它可以顯著減少輸入輸出所需的時間。
下面是一個例子,展示如何在程序中使用它:
#include
int main() {
? ? // 關(guān)閉同步
? ? std::ios::sync_with_stdio(false);
? ??
? ? // 取消 cin 與 stdin 的綁定,以提高讀取效率
? ? std::cin.tie(NULL);
? ? // 你的代碼邏輯
? ? std::cout << "Fast output without sync!\n";
? ??
? ? // 后續(xù)不要使用任何 C 的標(biāo)準(zhǔn)輸入輸出函數(shù)
? ? return 0;
}
請注意,調(diào)用 `ios::sync_with_stdio(false);` 之后,還應(yīng)該調(diào)用 `cin.tie(NULL);` 來解除 `cin` 與 `cout` 之間的綁定,這樣可以進一步提升輸入輸出的效率。這條指令會導(dǎo)致 `cin` 在每次從 `cout` 輸出之前不再自動刷新緩沖區(qū)。
B3825
#include
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< else cout< } } B3814-可多看幾次 for (auto it = direcion.begin(); it != direcion.end(); ++it) { pair int nx = mx + move.first; int ny = my + move.second; #include #include #include #include using namespace std; //判斷棋子是否在棋盤內(nèi) 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 // {-2, 1}, {-2, -1}, {-1, 2}, {-1, -2}, // {1, 2}, {1, -2}, {2, 1}, {2, -1} // }; vector 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 //遍歷馬的移動 for (auto &move : direcion) { int nx = mx + move.first; int ny = my + move.second; // 如果移動后仍在棋盤內(nèi) if (inBoard(nx, ny)) { // 遍歷該位置馬可以攻擊的所有位置 for (auto &attack : direcion) { int ax = nx + attack.first; int ay = ny + attack.second; // 如果攻擊位置在棋盤內(nèi),則添加到攻擊位置集合中 if (inBoard(ax, ay)) { attackPositions.insert(make_pair(ax, ay)); } } } } // 檢查帥和車是否在馬的攻擊范圍內(nèi) 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(可復(fù)看) #include #include #include 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; // 根據(jù)購買的菜品計算折扣 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 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'; // 根據(jù)購買的菜品計算折扣 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 #include #include using namespace std; int main() { long long x; cin >> x; string dishes; cin >> dishes; // 讀取兩個字符作為一個字符串 // 使用 count 統(tǒng)計 B 和 C 的出現(xiàn)次數(shù) 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; // 根據(jù)購買的菜品計算折扣 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 #include using namespace std; int main(){ long long a,b; cin>>a>>b; if(b==0) cout<<"NO"< else{ if(a>=0){ cout<<"NO"< }else{ if(b%2==0){ cout<<"NO"< }else{ cout<<"YES"< } } if(abs(a)%2==1){ cout<<"YES"< }else{ cout<<"NO"< } } } 柚子快報邀請碼778899分享:算法 洛谷day3 精彩內(nèi)容
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。
轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。