濰坊網(wǎng)站開發(fā)公司秒收錄關(guān)鍵詞代發(fā)
難度參考
????????難度:簡(jiǎn)單
????????分類:熟悉OJ與IDE的操作
????????難度與分類由我所參與的培訓(xùn)課程提供,但需要注意的是,難度與分類僅供參考。以下內(nèi)容均為個(gè)人筆記,旨在督促自己認(rèn)真學(xué)習(xí)。
題目
A + B | 1. A + B - AcWing題庫(kù) |
財(cái)務(wù)管理 | 1004:財(cái)務(wù)管理 |
實(shí)現(xiàn)四舍五入 | 實(shí)現(xiàn)四舍五入 |
牛牛的字符菱形 | 牛牛的字符菱形 |
題解
A+B

????????
#include <iostream>
#include <cstdio>using namespace std;int a,b;
int main(){cin>>a>>b;cout<<a+b<<endl;return 0;
}
????????時(shí)間復(fù)雜度為O(1)。
????????空間復(fù)雜度也是O(1)。
????????本題據(jù)說還有十幾種其他惡搞版題解,待學(xué)成之后仔細(xì)研究一下。
財(cái)務(wù)管理

#include <iostream>
#include <iomanip> // 用于設(shè)置輸出格式using namespace std;int main() {double sum = 0.0; // 初始化總結(jié)余為0double balance; // 用于存儲(chǔ)每個(gè)月的結(jié)余for (int i = 0; i < 12; i++) {cin >> balance; // 輸入每個(gè)月的結(jié)余sum += balance; // 累加到總結(jié)余中}double average = sum / 12.0; // 計(jì)算平均月末結(jié)余// 設(shè)置輸出格式,保留兩位小數(shù),并在前面加上"$"符號(hào)cout << fixed << setprecision(2) << "$" << average << endl;return 0;
}
????????時(shí)間復(fù)雜度為O(1)。(循環(huán)次數(shù)固定)
????????空間復(fù)雜度也是O(1)。
????????使用iomanip庫(kù),可以直接用fixed去保留小數(shù)個(gè)數(shù)。
實(shí)現(xiàn)四舍五入
????????使用cmath庫(kù)的話,里面有直接的四舍五入的函數(shù)。
#include <iostream>
#include <cmath>
using namespace std;int main() {double floatingNumber;cin >> floatingNumber;// 將浮點(diǎn)數(shù)四舍五入并轉(zhuǎn)換為整數(shù)int roundedNumber = round(floatingNumber);cout << roundedNumber << endl;return 0;
}
?????????時(shí)間復(fù)雜度為O(1)。
????????空間復(fù)雜度也是O(1)。
????????不過常見的做法是利用加減0.5后強(qiáng)制轉(zhuǎn)化后實(shí)現(xiàn)四舍五入。加減的原因是正數(shù)為加,負(fù)數(shù)為減。

#include <iostream>
using namespace std;int main() {float a;int b;cin >> a;// 判斷浮點(diǎn)數(shù)的小數(shù)部分是否大于等于0.5if (a >= 0) {b = a + 0.5;} else {b = a - 0.5;}cout << b << endl;return 0;
}
?????????時(shí)間復(fù)雜度為O(1)。
????????空間復(fù)雜度也是O(1)。
牛牛的字符菱形
????????明顯的懶鬼做法。
#include <iostream>
using namespace std;int main() {char a;cin >> a;cout << " " << " " << a << " " << " " << endl;cout << " " << a << a << a << " " << endl;cout << a << a << a << a << a << endl;cout << " " << a << a << a << " " << endl;cout << " " << " " << a << " " << " " << endl;
}
????????時(shí)間復(fù)雜度為O(1)。
????????空間復(fù)雜度也是O(1)。
????????動(dòng)點(diǎn)腦子。

#include <iostream>
using namespace std;int main() {char ch;cin >> ch;// 上半部分菱形for (int i = 1; i <= 3; i++) {for (int j = 1; j <= 3 - i; j++) {cout << " ";}for (int j = 1; j <= 2 * i - 1; j++) {cout << ch;}cout << endl;}// 下半部分菱形for (int i = 2; i >= 1; i--) {for (int j = 1; j <= 3 - i; j++) {cout << " ";}for (int j = 1; j <= 2 * i - 1; j++) {cout << ch;}cout << endl;}return 0;
}
思路
-
首先,我們從用戶那里接收一個(gè)字符作為輸入,并將其存儲(chǔ)在變量
ch
中。 -
然后,我們使用兩個(gè)循環(huán)來打印菱形圖案。上半部分的循環(huán)用于打印每一行的空格和字符,下半部分的循環(huán)用于打印下半部分的空格和字符。
-
對(duì)于上半部分的循環(huán),我們使用一個(gè)外層循環(huán)來迭代打印每一行。循環(huán)變量
i
的范圍是從1到3,即打印3行。每一行前面的空格的數(shù)量是3 - i
,通過內(nèi)層循環(huán)來輸出。 -
內(nèi)層循環(huán)使用變量
j
從1遞增到(2 * i - 1)
來打印每一行的字符(ch
)。這是因?yàn)槊恳恍械淖址麛?shù)量符合等差數(shù)列的規(guī)律,首項(xiàng)是 1,公差是 2。 -
上半部分的循環(huán)結(jié)束后,我們開始打印下半部分的菱形圖案。這里我們使用一個(gè)外層循環(huán)來迭代打印每一行。循環(huán)變量
i
的范圍是從2到1,即打印2行。每一行前面的空格的數(shù)量是3 - i
,通過內(nèi)層循環(huán)來輸出。 -
內(nèi)層循環(huán)同樣使用變量
j
從1遞增到(2 * i - 1)
來打印每一行的字符(ch
)。 -
最后,我們通過輸出換行符來確保每一行之后都換行。
打卡
? ? ? ? A+B
????????財(cái)務(wù)管理
????????實(shí)現(xiàn)四舍五入
? ? ? ? 牛牛的字符菱形