java实现取字母组成串
2024-10-07 13:31:37
** 取字母组成串**
A B C D中取5次,每个字母都可以重复取出,形成一个串。
现在要求,串中A出现的次数必须为偶数(0次也算偶数)。
求可以形成多少种可能的串。
参考答案:
528
public class Main1 {
public static boolean judge(int[] A) {
int count = 0;
for(int i = 0;i < A.length;i++) {
if(A[i] == 1)
count++;
}
if(count == 0 || count % 2 == 0)
return true;
return false;
}
public static void main(String[] args) {
int[] A = {1,2,3,4}; //1234分别代表ABCD
int[] tempA = new int[5];
int count = 0;
for(int a = 0;a < 4;a++) {
tempA[0] = A[a];
for(int b = 0;b < 4;b++) {
tempA[1] = A[b];
for(int c = 0;c < 4;c++) {
tempA[2] = A[c];
for(int d = 0;d < 4;d++) {
tempA[3] = A[d];
for(int e = 0;e < 4;e++) {
tempA[4] = A[e];
if(judge(tempA))
count++;
}
}
}
}
}
System.out.println(count);
}
}
最新文章
- 利用反射模拟一个spring的内部工作原理
- android 8种对话框(Dialog)使用方法汇总
- ajax传值方式为数组
- (转载)前端构建工具gulp使用
- ios 移动应用通用逻辑流程
- 如果Android和C#在一起?
- cocos2dx 3.1创建工 mac
- 关于css中透明度继承的问题
- ftp服务器端的安装及配置
- Is it possible to run native sql with entity framework?
- windows 定时备份linux 上oracle 数据库
- 《Mem2Seq: Effectively Incorporating Knowledge Bases into End-to-EndTask-Oriented Dialog Systems》
- header 和http状态码
- php null
- HDU 2058:The sum problem(数学)
- 【读书笔记】iOS-网络-理解错误源
- 【大数据】SparkStreaming学习笔记
- UI设计必用工具 — AI快捷键大全
- 允许使用root远程ssh登录(Ubuntu 16.04)
- iOS推送证书从申请到使用