当前位置:网站首页>蓝桥杯31天冲刺 Day8
蓝桥杯31天冲刺 Day8
2022-04-22 05:38:00 【柴可拉夫斯基】
神奇算式
链接: 神奇算式.

思路比较简单,无非就是列出所有位数的不同数字的组合(4个for循环),再用哈希表查重结果的每位数字,再统计
但是还是踩坑了,特别要注意:作为开头的数字不能为0!!!
代码:
public class 神奇算式 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int count1 = 0;
int count2=0;
int n=0;
//所有四个数字不同的情况(i作为开头的数不能为0)
for(int i =1;i<=9;i++) {
for(int j =0;j<=9;j++) {
if(j==i)
j++;
if(j>9)
break;
for(int k =0;k<=9;k++) {
while(k==i||k==j)
k++;
if(k>9)
break;
for(int t =0;t<=9;t++) {
while(t==i||t==j||t==k)
t++;
if(t>9)
break;
//1位数*3位数
if(j!=0) {
n=i*(100*j+10*k+t);
if(n>1000&&n<9999&&isDifferent(n,i,j,k,t))
count1++;
}
//2位数*2位数
if(k!=0) {
n = (i*10+j)*(k*10+t);
if(n>1000&&n<9999&&isDifferent(n,i,j,k,t))
count2++;
}
}
}
}
}
//count2除2是为了除重
System.out.println(count1+count2/2);
}
public static boolean isDifferent(int n,int i ,int j,int k,int t) {
int[] hash = new int[10];
hash[n/1000]++;
hash[n%1000/100]++;
hash[n%100/10]++;
hash[n%10]++;
if(hash[i]==1&&hash[j]==1&&hash[k]==1&&hash[t]==1)
return true;
return false;
}
}
缩位求和
链接: 缩位求和.


由于输入不会超过1000位,最大不会超过9999,可以放心用int计算。直接把String转成char数组计算就好
代码:
import java.util.*;
public class 缩位求和 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
String s = sc.nextLine();
int n=0;
for(char c : s.toCharArray()) {
n+=c-'0';
}
while(n>=10) {
int num=0;
char[] chars =Integer.toString(n).toCharArray();
for(int i=0;i<chars.length;i++) {
num+=chars[i]-'0';
}
n=num;
}
System.out.println(Integer.toString(n));
}
}
}
积木大赛
链接: 积木大赛.

版权声明
本文为[柴可拉夫斯基]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_53421929/article/details/123500488
边栏推荐
- 【机器学习】Scikit-learn介绍
- 线性筛法(素数筛)
- LeetCode 898. 子数组按位或操作 - set
- 7-10 最长对称子串 (25 分)(暴力题解)C语言
- The breakpoint will not currently be hit No symbols have been loaded for this document.
- codeforces div2 777 A
- ‘PdfFileWriter‘ object has no attribute ‘stream‘
- 数据挖掘——朴素贝叶斯分类
- raspberry keras-ocr can‘t allocate memory in static TLS block
- 全排列(回溯法)模板
猜你喜欢
随机推荐
Raspberry pie 4B SSH connection refused
codeforces div2 777 A
Coordinate conversion using Gaode map API: WGS84 → gcj02
Getting started with I / O Basics
Interpretation of imencode source code
stl alloc 空间分配器 代码解析
Data mining clustering
通过设置关联菜单建立excel记账本
List stream: usage instance of reduce
【2022阿里安全】真实场景篡改图像检测挑战赛 决赛rank17方案分享
数据挖掘——聚类
0/1背包问题(动态规划+动规优化)
Opencv skeleton extraction / image thinning code
Longest ascending subsequence (LIS)
Usage of JSON type field in MySQL
动态创建数组(c6385 正在从“a”读取无效数据)
代码整洁之道学习
Data mining -- association rule mining
conda命令
Redis setting and obtaining expiration time









