1.1.1 A+B for Input-Output Practice (I)php
#include <stdio.h> int main() { int a,b; while(scanf("%d %d",&a,&b)==2) printf("%d\n",a+b); return 0; }
1.1.2 A+B for Input-Output Practice (II)java
#include <stdio.h> int main() { int n; scanf("%d",&n); while(n--) { int a,b; scanf("%d %d",&a,&b); printf("%d\n",a+b); } return 0; }
1.1.3 A+B for Input-Output Practice (III)ios
#include <iostream> using namespace std; int main(){ int a, b; while(cin >> a >> b && (a || b)) cout << a + b << endl; return 0; }
1.1.4 A+B for Input-Output Practice (IV)c++
#include <iostream> using namespace std; int main(){ int n, t; while(cin >> n && n){ int ans = 0; while(n--){ cin >> t; ans += t; } cout << ans << endl; } return 0; }
1.1.5 A+B for Input-Output Practice (V)git
#include <iostream> using namespace std; int main(){ int t; cin >> t; while(t--){ int n; cin >> n; int ans = 0; while(n--){ int t; cin >> t; ans += t; } cout << ans << endl; } return 0; }
1.1.6 A+B for Input-Output Practice (VI)數組
#include <iostream> using namespace std; int main(){ int n; while(cin >> n){ int ans = 0; while(n--){ int t; cin >> t; ans += t; } cout << ans << endl; } return 0; }
1.1.7 A+B for Input-Output Practice (VII)app
#include <iostream> using namespace std; int main(){ int a, b; while(cin >> a >> b) cout << a + b << endl << endl; return 0; }
1.1.8 A+B for Input-Output Practice (VIII)ide
#include <iostream> using namespace std; int main(){ int t; cin >> t; for(int i = 0; i < t; ++i){ if(i) cout << endl; int n; cin >> n; int ans = 0; while(n--){ int tmp; cin >> tmp; ans += tmp; } cout << ans << endl; } return 0; }
1.2.1 Text Reverse函數
#include <stack> #include <cstdio> #include <iostream> #include <algorithm> using namespace std; int main(){ int n; cin >> n; cin.get(); stack<char> s; while(n--){ char ch; while(true){ ch = cin.get(); if(ch == ' ' || ch == '\n'){ while(s.size()){ cout << s.top(); s.pop(); } if(ch == '\n'){ cout << endl; break; } else cout << " "; } else s.push(ch); } } return 0; }
1.2.2 find your present (2)ui
#include <iostream> using namespace std; int main(){ ios::sync_with_stdio(false); // 取消同步 or 別用iostream or TLE int n; while(cin >> n && n){ int num, ans = 0; while(n--){ cin >> num; ans ^= num; } cout << ans << endl; } return 0; }
1.2.3 Quicksum
#include <iostream> using namespace std; int main(){ ios::sync_with_stdio(false); string s; while(getline(cin, s) && s != "#"){ int ans = 0; for(int i = 1; i <= s.length(); ++i){ if(s[i-1] == ' ') continue; ans += i * (s[i-1] - 'A' + 1); } cout << ans << endl; } return 0; }
1.2.4 IBM Minus One
#include <string> #include <iostream> using namespace std; int main(){ int n; cin >> n; for(int i = 0; i < n; ++i){ string s; cin >> s; cout << "String #" << i + 1 << endl; for(int i = 0; i < s.length(); ++i) cout << char(s[i] == 'Z' ? 'A' : s[i] + 1); cout << endl << endl; } return 0; }
1.2.5 Lowest Bit
#include <iostream> using namespace std; int main(){ int n; while(cin >> n && n) cout << (n & (-n)) << endl; // 位運算求二進制最低位1(樹狀數組lowbit函數) return 0; }
1.2.6 ASCII
#include <iostream> using namespace std; int main(){ int n; while(cin >> n){ int num; while(n--){ cin >> num; cout << char(num); } } return 0; }
1.2.7 Identity Card
#include <string> #include <iostream> using namespace std; typedef long long ll; int main(){ int n; cin >> n; while(n--){ string s; cin >> s; string region = "Shanghai"; if (s[0] == '3' && s[1] == '3') region = "Zhejiang"; else if (s[0] == '1' && s[1] == '1') region = "Beijing"; else if (s[0] == '7' && s[1] == '1') region = "Taiwan"; else if (s[0] == '8' && s[1] == '1') region = "Hong Kong"; else if (s[0] == '8' && s[1] == '2') region = "Macao"; else if (s[0] == '5' && s[1] == '4') region = "Tibet"; else if (s[0] == '2' && s[1] == '1') region = "Liaoning"; cout << "He/She is from " << region << ",and his/her birthday is on " << s[10] << s[11] << "," << s[12] << s[13] << "," << s[6] << s[7] << s[8] << s[9] << " based on the table." << endl; } return 0; }
1.2.8 AC Me
#include <map> #include <string> #include <iostream> using namespace std; typedef long long ll; int main(){ string s; while(getline(cin, s)){ map<char, int> ans; for(int i = 0; i < s.length(); ++i) ++ans[s[i]]; for(int i = 'a'; i <= 'z'; ++i) cout << char(i) << ":" << ans[char(i)] << endl; cout << endl; } return 0; }
1.3.1 Moving Tables
#include <cstring> #include <iostream> #include <algorithm> using namespace std; const int MAXN = 555; int cnt[MAXN]; int main(){ ios::sync_with_stdio(false); int t; cin >> t; while(t--){ memset(cnt, 0, sizeof(cnt)); int n; cin >> n; while(n--){ int s, e; cin >> s >> e; if(s > e) swap(s, e); if(s & 1 == 0) --s; if(e & 1 == 1) ++e; for(int i = s; i <= e; ++i) cnt[i] += 10; // 統計每一個房間被佔用的時間 } cout << *max_element(cnt, cnt+MAXN) << endl; } return 0; }
1.3.2 今年暑假不AC
#include <cstring> #include <iostream> #include <algorithm> using namespace std; struct show{ int s, e; bool operator < (show& other){ // 以 結束早,開始晚 爲優先 return e != other.e ? e < other.e : s > other.s; } }; const int MAXN = 111; show arr[MAXN]; int main(){ ios::sync_with_stdio(false); int n; while(cin >> n, n){ for(int i = 0; i < n; ++i) cin >> arr[i].s >> arr[i].e; sort(arr, arr+n); int ans = 0, flag = 0; // 遍歷統計不重疊區間數量 for(int i = 0; i < n; ++i){ if(arr[i].s >= flag){ ++ans; flag = arr[i].e; } } cout << ans << endl; } return 0; }
1.3.3 百步穿楊
#include <iostream> #include <algorithm> using namespace std; struct arrow{ int len, num; bool operator < (arrow& other){ return len < other.len; } }; const int MAXN = 55; arrow arr[MAXN]; int main(){ ios::sync_with_stdio(false); int t; cin >> t; while(t--){ int n; cin >> n; for(int i = 0; i < n; ++i) cin >> arr[i].len >> arr[i].num; sort(arr, arr + n); for(int i = 0; i < n; ++i){ int len = arr[i].len - 2, num = arr[i].num; while(num--){ cout << ">+"; for(int k = 0; k < len; ++k) cout << "-"; cout << "+>" << endl; } cout << endl; // 每種箭後面須要一個空行 } } return 0; }
1.3.4 What Is Your Grade?
好像寫的有點亂......QAQ
#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int MAXN = 111; struct stu{ int num, index, score; string time; bool operator < (const stu& other){ return num != other.num ? num > other.num : time < other.time; } }; bool cmp(stu a, stu b){ return a.index < b.index; } stu arr[MAXN]; int cnt[MAXN]; int main(){ int n; while(cin >> n, ~n){ memset(cnt, 0, sizeof(cnt)); for(int i = 0; i < n; ++i){ cin >> arr[i].num >> arr[i].time; arr[i].index = i; ++cnt[arr[i].num]; } sort(arr, arr + n); int prank = cnt[arr[0].num]; for(int i = 0; i < n; ++i){ if(!prank) prank = cnt[arr[i].num]; arr[i].score = arr[i].num * 20 + (5 - arr[i].num) * 10; if(arr[i].score == 50 || arr[i].score == 100){ --prank; continue; } if(cnt[arr[i].num] == 1 || prank > int(ceil(cnt[arr[i].num] / 2.0))) arr[i].score += 5; --prank; } sort(arr, arr + n, cmp); for(int i = 0; i < n; ++i) printf("%d\n", arr[i].score); puts(""); } return 0; }
1.3.5 第二小整數
#include <iostream> #include <algorithm> using namespace std; const int MAXN = 11; int arr[MAXN]; int main(){ ios::sync_with_stdio(false); int t; cin >> t; while(t--){ int n; cin >> n; for(int i = 0; i < n; ++i) cin >> arr[i]; sort(arr, arr + n); cout << arr[1] << endl; } return 0; }
1.3.6 考試排名
#include <cmath> #include <cstdio> #include <string> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int MAXN = 11111; struct info{ char name[22]; char status[15][11]; int solve, time; bool operator < (const info& o){ return solve != o.solve ? solve > o.solve : time < o.time; } }; info arr[MAXN]; int problem, punish; int ifind(char* str, char ch){ int pos = 0, len = strlen(str); for( ; pos < len; ++pos){ if(str[pos] == ch) return pos; } return pos; } void calc(int index){ arr[index].solve = arr[index].time = 0; info tmp = arr[index]; for(int i = 0; i < problem; ++i){ if(tmp.status[i][0] == '-' || tmp.status[i][0] == '0') continue; ++arr[index].solve; int lpos = ifind(tmp.status[i], '('); int tmptime = 0; for(int j = 0; j < lpos; ++j) tmptime = tmptime * 10 + tmp.status[i][j] - '0'; arr[index].time += tmptime; int rpos = ifind(tmp.status[i], ')'); if(rpos == strlen(tmp.status[i])) continue; tmptime = 0; for(int j = lpos + 1; j < rpos; ++j) tmptime = tmptime * 10 + tmp.status[i][j] - '0'; arr[index].time += (tmptime * punish); } } void read(int index, int n){ for(int i = 0; i < n; ++i) scanf("%s", arr[index].status[i]); calc(index); } int main(){ scanf("%d %d", &problem, &punish); char* name; int index = 0; while(~scanf("%s", arr[index].name)){ read(index, problem); ++index; } sort(arr, arr + index); for(int i = 0; i < index; ++i) printf("%-10s %2d %4d\n", arr[i].name, arr[i].solve, arr[i].time); return 0; }
1.3.7 排列2
排序後用STL的next_permutation生成排列進行遍歷檢查
#include <bits/stdc++.h> using namespace std; int main(){ int arr[4]; bool checkbegin = true; // 是不是第一組數據 while(cin >> arr[0] >> arr[1] >> arr[2] >> arr[3]){ if(!(arr[0] || arr[1] || arr[2] || arr[3])) break; if(!checkbegin) cout << endl; // 兩組數據×之間×空行 sort(arr, arr + 4); while(!arr[0]) next_permutation(arr, arr + 4); // 去掉首位爲0的數 int flag = arr[0]; // 記錄前一個數的千位 bool checkfirst = true; // 是不是千位爲flag的第一個數 do{ if(flag != arr[0]){ cout << endl; // 千位數不一樣,換行 flag = arr[0]; checkfirst = true; } if(!checkfirst) cout << " "; for(int i = 0; i < 4; ++i) cout << arr[i]; checkfirst = false; }while(next_permutation(arr, arr + 4)); cout << endl; checkbegin = false; } return 0; }
1.3.8 Crixalis's Equipment
#include <iostream> #include <algorithm> using namespace std; struct info{ int a, b; bool operator < (const info& other){ int val1 = b - a, val2 = other.b - other.a; // 按差值排序,儘可能讓剩餘空間大,差值相同的以搬運所需空間大的爲優先 return val1 == val2 ? b > other.b : val1 > val2; } }; info arr[1111]; int v, n; int main(){ int t; cin >> t; while(t--){ cin >> v >> n; for(int i = 0; i < n; ++i) cin >> arr[i].a >> arr[i].b; sort(arr, arr + n); bool flag = true; for(int i = 0; i < n; ++i){ if(arr[i].b > v){ flag = false; break; } v -= arr[i].a; } cout << (flag ? "Yes" : "No") << endl; } return 0; }
2.1.1 最小公倍數
#include <iostream> #include <algorithm> using namespace std; // int gcd(int a, int b){ reutrn b ? gcd(b, a % b) : a; } int main(){ int a, b; while(cin >> a >> b) cout << a / __gcd(a, b) * b << endl; // algorithm內置的__gcd() O(∩_∩)O return 0; }
2.1.2 How many prime numbers
這題......數據好水...... 0.0
#include <cmath> #include <iostream> #include <algorithm> using namespace std; bool checkprime(int num){ int lmt = sqrt(num); for(int i = 2; i <= lmt; ++i) if(num % i == 0) return false; return true; } int main(){ ios::sync_with_stdio(false); int n; while(cin >> n){ int ans = 0, tmp; while(n--){ cin >> tmp; if(checkprime(tmp)) ++ans; } cout << ans << endl; } return 0; }
2.1.3 Cake
先分 \(k = gcd(a, b)\) 塊,每份能夠當作 \(\dfrac{a}{k}\) 塊,也能夠當作 \(\dfrac{b}{k}\) 塊
按照切的刀數看,總共切了 \(a + b - k\) 刀,分紅 \(a + b - k\) 塊
#include <iostream> #include <algorithm> using namespace std; int main(){ int a, b; while(cin >> a >> b) cout << (a + b - __gcd(a, b)) << endl; return 0; }
2.1.4 又見GCD
#include <iostream> #include <algorithm> using namespace std; int main(){ int n; cin >> n; while(n--){ int a, b; cin >> a >> b; a = a / b; for(int i = 2; ; ++i) if(__gcd(a, i) == 1){ cout << i * b << endl; break; } } return 0; }
2.1.5 七夕節
#include <cmath> #include <iostream> #include <algorithm> using namespace std; int main(){ ios::sync_with_stdio(false); int t; cin >> t; while(t--){ int n; cin >> n; int ans = 1; int lmt = sqrt(n); for(int i = 2; i <= lmt; ++i) if(n % i == 0) ans += (i + n / i); if(n == lmt * lmt) ans -= lmt; cout << ans << endl; } return 0; }
2.1.6 整數對
將原來的數\(A\) 分爲低位\(a\)、去掉的一位\(b\)、高位\(c\),設\(b\)爲第\(k\)位,則有\(A = a + b * 10 ^ k + c * 10 ^ {k+1}\)
對於獲得的新數字\(B\) 有:\(B = a + c * 10 ^ k\)
對於輸入數據\(N\) 有:\(N = A + B = 2 * a + b * 10 ^ k + 11 * c * 10 ^ k\)
設 \(T = \dfrac{N}{10^k} = 11 * c + b\)
\(\dfrac{T}{11} = 11 * c \cdots\cdots b\)
則:
\(c = \dfrac{N}{11*10^k}\), \(b = \dfrac{N}{10^k} - 11 * c\), \(a = \dfrac{N - 11 * c * 10 ^ k - b * 10 ^ k}{2}\)
\(2 * a\) 可能會產生進位對 \(b\) 形成影響,可是對 \(a\) 無影響(b 最大爲10,計算\(c\)時爲除以11)
因此應對\(b\)分類討論
#include <set> #include <iostream> using namespace std; int main(){ int n; while(cin >> n, n){ set<int> ans; // 去除重複 + 排序 int a, b, c; for(int k = 1; k <= n; k *= 10){ c = n / 11 / k; b = (n / k) - 11 * c; if((b || c) && b < 10){ // 2*a 無進位 a = (n - 11 * c * k - b * k) >> 1; if(2 * a + b * k + 11 * k * c == n) ans.insert(a + b * k + c * k * 10); } --b; if((b || c) && b >= 0){ // 2*a 進位 a = (n - 11 * c * k - b * k) >> 1; if(2 * a + b * k + 11 * k * c == n) ans.insert(a + b * k + c * k * 10); } } if(ans.empty()) cout << "No solution." << endl; else{ for(set<int>::iterator it = ans.begin(); it != ans.end(); ++it){ if(it != ans.begin()) cout << " "; cout << *it; } cout << endl; } } return 0; }
2.1.7 The area
P1(x1, y1) P2(x2, y2) P3(x3, y3)
設直線 \(y_l = kx + h\), 拋物線 \(y_p = ax^2+bx+c\)
對於\(y_l\)有 \(k = \dfrac{y_2-y_3}{x_2-x_3}\), \(h = y_2 - k * x_2\)
對於\(y_p\): \(x_1 = -\dfrac{b}{2a}\), \(y_1 = \dfrac{4ac-b^2}{4a}\)
則: \(b = -2ax_1\)
又有: \(a{x_1}^2 + bx_1 + c = y_1\), \(a{x_2}^2 + bx_2 + c = y_2\)
\(\therefore a({x_2}^2 - {x_1}^2) - 2ax_1(x_2-x_1) = y_2-y_1\)
\(a = \dfrac{y_2-y_1}{{(x_2-x_1)}^2}\), \(b = -2ax_1\), \(c = y_1 - a{x_1}^2 - bx_1\)
設面積爲\(S\)
\(y_p - y_l = ax^2 + (b-k)x + (c-h)\)
\(\displaystyle{S = \int_{x_2}^{x_3}(y_p - y_l) \, dx = \int_{x_2}^{x_3}(ax^2 + (b-k)x + (c-h)) \, dx}\)
求個定積分 \(\left(\left(\dfrac{a}{3}\right)({x_3}^3-{x_2}^3)+\left(\dfrac{b-k}{2}\right)({x_3}^2-{x_2}^2)+(c-h)(x_3-x_2)\right)\Bigg|_{x_2}^{x_3}\)
#include <cstdio> int t; double x1, y1, x2, y2, x3, y3; int main(){ scanf("%d", &t); while(t--){ scanf("%lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3); double k = (y2-y3)/(x2-x3), h = y2-k*x2, a = (y2-y1)/(x2-x1)/(x2-x1), b = -2*a*x1, c = y1-a*x1*x1-b*x1; printf("%.2f\n", ((a/3)*(x3*x3*x3-x2*x2*x2)+((b-k)/2)*(x3*x3-x2*x2)+(c-h)*(x3-x2))); } return 0; }
2.1.8 Leftmost Digit
typedef long long ll;
科學計數法表示: \(n^n = a * 10^x\)
等號兩邊取對數: \(n*lg(n) = x + \lg(a)\) 因爲\(0\le \lg(n) \le 1\),因此\(x\)爲\(n*lg(n)\)的整數部分
\(\lg(a) = n*\lg(n) - x\)
\(\lg(a) = n*\lg(n) - ll(n*\lg(n))\)
\(a = 10^{n*\lg(n) - ll(n*\lg(n))}\)
#include <cmath> #include <iostream> using namespace std; typedef long long ll; int main(){ ios::sync_with_stdio(false); int t; cin >> t; while(t--){ ll n; cin >> n; cout << ll(pow(10, (n*log10(n) - ll(n*log10(n))))) << endl; } return 0; }
2.2.1 Fibonacci
Fibonacci公式: \(\displaystyle{F_n = \dfrac{1}{\sqrt5}\left[{\left(\dfrac{1+\sqrt5}{2}\right)}^n-{\left(\dfrac{1-\sqrt5}{2}\right)}^n\right]}\)
按照Leftmost Digit的方法求解
直接求\(F_n\)不太科學,考慮到用的是\(F_n\)的對數,對等號兩邊取對數,化簡:
\(\lg(F_n) = \lg\left(\dfrac{1}{\sqrt5}\right) + n\,\lg\left(\dfrac{1 + \sqrt5}{2}\right) + \lg\left[1-{\left(\dfrac{1-\sqrt5}{1+\sqrt5}\right)}^n\right]\)
#include <math.h> #include <stdio.h> int main(){ int n; int fib[233] = {0, 1}; for(int i = 2; i < 21; ++i) fib[i] = (fib[i-1] + fib[i-2]) % 10000; while(~scanf("%d", &n)){ if(n < 21) { printf("%d\n", fib[n]); continue; } double logFn = log10((1/sqrt(5))) + n*log10((1+sqrt(5))/2) + log10(1-pow((1-sqrt(5))/(1+sqrt(5)),n)); double a = pow(10, logFn - int(logFn)); printf("%d\n", int(a * 1000)); } return 0; }
2.2.2 Fibonacci
至關於每次將ban掉的人去掉
#include <iostream> using namespace std; int main(){ ios::sync_with_stdio(false); int arr[15]; for(int i = 1; i < 15; ++i) for(int ans = i+1; ; ++ans){ int p = 0, left; bool flag = true; for(left = 2*i; left > i; --left){ // 將從1開頭的編號對應到從0開頭的編號以知足取模的需求 p = (p + ans - 1) % left; // 無論ban了多少人,只要每次求出來的p比i小就表明殺死好人 if(p < i){ flag = false; break; } } if(flag && left == i){ arr[i] = ans; break; } } int n; while(cin >> n, n) cout << arr[n] << endl; return 0; }
2.2.3
相似於遞歸檢查是否知足條件
若是起始柱或目標柱上盤子數等於當前檢查的盤子總數則是知足條件
不然,檢查最大的盤子,若是在藉助柱則不知足條件,若是不在藉助柱則檢查當前盤子總數-1的狀況
#include <iostream> #include <algorithm> using namespace std; const int MAXN = 66; int t, N, n[3], id[3][MAXN], pos[3]; int main(){ ios::sync_with_stdio(false); cin >> t; while(t--){ int aa = 0, bb = 1, cc = 2; pos[aa] = pos[bb] = pos[cc] = 0; cin >> N; cin >> n[0]; for(int i = 0; i < n[0]; ++i) cin >> id[aa][i]; cin >> n[1]; for(int i = 0; i < n[1]; ++i) cin >> id[bb][i]; cin >> n[2]; for(int i = 0; i < n[2]; ++i) cin >> id[cc][i]; bool flag = true; while(true){ if(n[aa] == N || n[cc] == N){ flag = true; break; } if(n[bb] && id[bb][pos[bb]] == N){ flag = false; break; } if(n[aa] && id[aa][pos[aa]] == N){ --N; --n[aa]; ++pos[aa]; swap(bb, cc); continue; } if(n[cc] && id[cc][pos[cc]] == N){ --N; --n[cc]; ++pos[cc]; swap(aa, bb); continue; } } cout << (flag ? "true" : "false") << endl; } return 0; }
2.2.4 Wolf and Rabbit
n, m最大公約數爲1時表明wolf能從hole0走到hole1,也就能走到其餘全部的hole
#include <iostream> #include <algorithm> using namespace std; int main(){ ios::sync_with_stdio(false); int t, n, m; cin >> t; while(t--){ cin >> n >> m; cout << (__gcd(n, m) == 1 ? "NO" : "YES") << endl; } return 0; }
2.2.5 Number Sequence
#include <iostream> #include <algorithm> using namespace std; int main(){ ios::sync_with_stdio(false); int a, b, n; while(cin >> a >> b >> n, a || b || n){ int i, arr[55] = {0, 1, 1}; for(i = 3; i < 50; ++i){ arr[i] = (a * arr[i-1] + b * arr[i-2]) % 7; if(arr[i] == 1 && arr[i-1] == 1) break; // 找循環節,最多不會超過50 } n %= (i - 2); cout << (n ? arr[n] : arr[i-2]) << endl; } return 0; }
2.2.6 Train Problem II
Catalan公式: \(Catalan_{n} = \dfrac{C_{2n}^n}{n + 1}\)
Catalan遞推公式: \(Catalan_{n} = \dfrac{(4n-2)Catalan_{n-1}}{n+1}\)
import java.util.*; import java.math.*; public class Main{ public static void main(String[] args){ Scanner scan = new Scanner(System.in); BigInteger[] ans = new BigInteger[111]; ans[0] = ans[1] = new BigInteger("1"); for(int i = 2; i < 111; ++i) ans[i] = ans[i-1].multiply(new BigInteger(Integer.toString(4*i-2))).divide(new BigInteger(Integer.toString(i+1))); while(scan.hasNext()){ int n = scan.nextInt(); System.out.println(ans[n]); } scan.close(); } }
2.2.8 Big Number
對於一個數k,求位數:\(\lg(k) + 1\)
\(1 \le n \le 10^7\)範圍內能夠直接用循環求: \(\lg(n!) = \lg(1) + \lg(2) + \cdots + \lg(n)\)
對於大數階乘的值\(n!\)還能夠用斯特林公式: \(n! \approx \sqrt{2{\pi}n}\left(\dfrac{n}{e}\right)^n\) 即便在n較小時取值也很是準確
則: \(\lg(n!) = \lg\left(\sqrt{2{\pi}n}\left(\dfrac{n}{e}\right)^n\right) = \dfrac{1}{2}\lg(2{\pi}n) + n\lg\left(\dfrac{n}{e}\right)\)
#include <cmath> #include <iostream> #include <algorithm> using namespace std; const double pi = acos(-1.0); const double e = exp(1.0); int main(){ ios::sync_with_stdio(false); // 直接運算 //int t, n; //cin >> t; //while(t--){ // cin >> n; // double ans = 0.0; // for(int i = 1; i <= n; ++i) ans += log10(i); // cout << int(ans) + 1 << endl; //} // 運用斯特林公式運算 int t, n; cin >> t; while(t--){ cin >> n; cout << int(0.5*log10(2*pi*n)+n*log10(n/e)+1) << endl; } }