?
public class _求階乘和 {
public static void main(String[] args) {
// 根據(jù)已有的知識 可以知道的是,現(xiàn)在要求s的末尾九位數(shù)字,已知的是39之后的階乘他的后九位都是0;
//所以不需要計算到2023的階乘
//一個數(shù)求出來的階乘想要末尾有0
//數(shù)中必須要有2和5,已知的2的數(shù)目應該是遠大于5的,所以需要找出5的數(shù)目
// 5 10 15 20 25 30 35 40 注意到了25里面有兩個無,剛好四十的階乘,后九位都為0;
long ans=0;
for (int i = 1; i <=39; i++) {
long sum=1;
for (int j = 1; j <=i; j++) {
sum=j*sum;
sum= (long) (sum%1e9);
}
ans+=sum;
}
System.out.println(ans%1000000000);
}
}
?很巧妙的一道回溯算法的題目
class aMain {
static int count = 0;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int T = scanner.nextInt();
while (T-- > 0) {
int N = scanner.nextInt();
int[] A = new int[N];
for (int i = 0; i < N; i++) {
A[i] = scanner.nextInt();
}
count = 0;
backtrack(A, 0, 0, 0);
System.out.println(count % 1000000007);
}
}
static void backtrack(int[] A, int index, int sum1, int sum2) {
if (index == A.length) {
if (sum1 % 2 == 0 && sum2 % 2 == 0) {
count++;
}
return;
}
backtrack(A, index + 1, sum1 + A[index], sum2);
backtrack(A, index + 1, sum1, sum2 + A[index]);
}
}
只有兩種選擇,一個是加入到一集合中去,一個是加入到二集合中去,結束的條件是對應下標的索引值等于A.length的時候,同時滿足sum1和sum2都是偶數(shù)的情況下 count++;文章來源:http://www.zghlxwxcb.cn/news/detail-566402.html
后序還可以考慮適當?shù)募糁M行優(yōu)化,文章來源地址http://www.zghlxwxcb.cn/news/detail-566402.html
到了這里,關于7.10藍橋杯刷題的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!