(2019-1) 相隔天数
输入日期格式:YYYYMMDD,求与20190205相隔天数。
#include<iostream>
using namespace std;
int day[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31}};
int leapyear(int y){
if((y%4==0 && y%100!=0)|| y%400==0){
return 1;
}
return 0;
}
int main(){
int t1;
scanf("%d",&t1);
int t2=20190205;
int temp;
if(t1>t2){ //保证t1<t2
temp=t1;
t1=t2;
t2=temp;
}
int y1=t1/10000;
int m1=t1%10000/100;
int d1=t1%100;
int y2=t2/10000;
int m2=t2%10000/100;
int d2=t2%100;
int count=0;
while(y1<y2 || m1<m2 || d1<d2 ){
if(d1<day[leapyear(y1)][m1]){
d1++;
count++;
}
else{
if(m1<12){ //m1=12
m1++;
d1=1;
count++; //1-12月月底
}
else{
y1++;
d1=1;
m1=1;
count++; //12月底
}
}
}
printf("%d\n",count);
return 0;
}
(2019-2) 最大连续子序列
给定一个数字序列A1,A2…An,求i,j(1<=i<=j<=n),使得Ai+…+Aj最大,输出这个最大和。
输入:
5
1 5 -3 2 4
6
1 -2 3 4 -10 6
输出:
9
7
#include<iostream>
#include<climits>
#include<cstring>
using namespace std;
const int MAXN=1005;
int dp[MAXN]; //dp[i]表示以i为末尾的元素的值
int main(){
int n;
int a[101];
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
memset(dp,0,sizeof(dp));
int maxmum=INT_MIN;
for(int i=0;i<n;i++){
dp[i]=max(a[i],dp[i-1]+a[i]);
maxmum=max(dp[i],maxmum);
}
printf("%d\n",maxmum);
return 0;
}
(2019-3)
题目:
输入:
3
输出:
5
在这道题里知道了一个概念叫卡特兰数 https://blog.csdn.net/han_hhh/article/details/108677202
但是要注意n的大小。用longlong只能算到14
//只能处理n<=14
#include<iostream>
using namespace std;
int a[101][101],b[101];
int main(){
int n;
scanf("%d",&n);
long long n1=1,n2=1;
for(int i=n+1;i<=2*n;i++){
n1*=i;
}
for(int i=1;i<=n;i++){
n2*=i;
}
printf("%lld\n",n1/n2/(n+1));
return 0;
}
C++大数乘法版
待补充……
java版
import java.*;
import java.util.*;
import java.math.BigInteger;
public class f2019_3 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n=in.nextInt();
BigInteger n1=BigInteger.valueOf(1);
BigInteger n2=BigInteger.valueOf(1);
for(int i=n+1;i<=2*n;i++) {
n1=n1.multiply(BigInteger.valueOf(i));
}
for(int i=1;i<=n;i++) {
n2=n2.multiply(BigInteger.valueOf(i));
}
BigInteger res=n1.divide(n2).divide(BigInteger.valueOf(n+1));
System.out.println(res);
}
}