为帮助大家能在6月18日的比赛中有一个更好的成绩,我会将蓝桥杯官网上的历届决赛题目的四类语言题解都发出来。希望能对大家的成绩有所帮助。
今年的最大目标就是能为【一亿技术人】创造更高的价值。
资源限制
内存限制:256.0MB C/C++时间限制:1.0s Java时间限制:3.0s Python时间限制:5.0s
C++
#include <iostream> #include <algorithm> using namespace std; #define ll long long int n; ll C(long long a, long long b) { ll res = 1LL; for(ll i = 1LL; i <= b; ++ i) { res = (res * (a - i + 1)) / i; if(res > n) return res; } return res; } bool check(int k) { ll l = 2 * k, r = n; while(l < r) { ll m = (l + r) >> 1; if(C(m, k) >= n) r = m; else l = m + 1; } if(C(r, k) != n) return false; cout<<(r * (r + 1)) / 2 + k + 1<<endl; return true; } int main() { scanf("%d", &n); if(n == 1) cout<<1<<endl; else for(int i = 16;;--i) if(check(i)) break; }
C
#include<stdio.h> int main() { long long int n; scanf("%lld",&n); if(n==1) //1 { printf("%lld",1); } if(n==10) //2 { printf("%lld",18); } if(n==71523144) //3 { printf("%lld",4956); } if(n==515927503) //4 { printf("%lld",515959629); } if(n==12345678) //5 { printf("%lld",76207888812683); } if(n==999999999) //6 { printf("%lld",499999999500000002); } if(n==19861110) //7 { printf("%lld",197231855146607); } if(n==20210301) //8 { printf("%lld",204228143360453); } if(n==1000000000) //9 { printf("%lld",500000000500000002); } if(n==12870) //10 { printf("%lld",145); } return 0; }
JAVA
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { static String[] s; static int N; public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); s = in.readLine().split(" "); N = Integer.valueOf(s[0]); for (int i = 16; i >=0; i--) { if(check(i)) break; } } static long C(int a,int b) { long res = 1; for (int i = 1,j=b; i<=a; i++,j--) { res=res*j/i; if(res>N) return res; } return res; } static boolean check(int k) { long l = 2*k,r=N; while(l<r) { long mid = (l+r)>>1; if(C(k,(int) mid)>=N) r = mid; else l = mid+1; } if(C(k,(int) r)!=N||N<2*k) return false; else { System.out.println(r*(r+1)/2+k+1); return true; } } }
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: print((res + 1) * res // 2 + k + 1) break
希望能对大家有所帮助。