今日「每日一题」来啦!历年真题题目:杨辉三角形。还没有加入专属刷题群的小伙伴,记得扫码哟~(每天会在群里发蓝桥杯历年真题)
1、C/C++组
using namespace std;
int n;
int C(int a , int b){
int res = 1;
for(int i = a , j = 1 ; j <= b ; i -- , j ++){
res = res * i / j;
if(res > n) return res;
}
return res;
}
signed main()
{
cin >> n;
for(int k = 16 ; ~k ; k --){
int l = 2 * k , r = max(n , l) , res = -1;
while(l <= r){
int mid = l + r >> 1;
if(C(mid , k) >= n) res = mid , r = mid - 1;
else l = mid + 1;
}
if(C(res , k) == n) return cout << (res + 1) * res / 2 + k + 1 << 'n' , 0;
}
return 0;
}
2、Java组
import java.util.*;
public class Main {
private static int n;
private static long C(long a, long b) {
long res = 1;
for (long i = a, j = 1; j <= b; i--, j++) {
res = res * i / j;
if (res > n) {
return res;
}
}
return res;
}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
n = cin.nextInt();
for (int k = 16; k >= 0; k--) {
int l = 2 * k, r = Math.max(n, l), res = -1;
while (l <= r) {
int mid = l + r >> 1;
if (C(mid, k) >= n) {
res = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
if (C(res, k) == n) {
System.out.println((long) (res + 1) * res / 2 + k + 1);
break;
}
}
cin.close();
}
}
3、Python组
import os
import sys
n = int(input())
def C(a, b):
res = 1
i = a
j = 1
while j <= b:
res = int(res * i / j)
if res > n:
return int(res)
i -= 1
j += 1
return int(res)
for k in range(16, -1, -1):
l = 2 * k
r = max(n, l)
res = int(-1)
while l <= r:
mid = l + r >> 1
if C(mid, k) >= n:
res = mid
r = mid - 1
else:
l = mid + 1
if C(res, k) == n:
+ 1) * res // 2 + k + 1)
break
今日的「每日一题」就结束啦~
推荐站内搜索:最好用的开发软件、免费开源系统、渗透测试工具云盘下载、最新渗透测试资料、最新黑客工具下载……
还没有评论,来说两句吧...