Prime Solutions
以下是一段中学时代的惨痛回忆…每当学到排列组合的单元时,最痛苦的不是分析题目,也不是带错公式或计算错误,而是所谓的「苦工题」,以下这题是个例子:
给定正整数N与S,求出方程式(1)的所有质数解(全为质数)。

遇到这题,通常只能硬着头皮将每一组以「土法炼钢」的方式一一列出,然而到了大学修过程式设计与演算法课,俾使我们能在电脑上撰写程式轻松解决此问题。

INPUT
第一行一样为测资个数
每一行输入正整数与正整数,N与S之间相隔一个空白键。
(提示: 计算之前先考虑方程式有没有解可以加速程式执行。)

OUTPUT
N个质数相加刚好等於S有哪些可能的组合,质数可以重复,但请由小到大排列,若无解(例如100个质数相加等於23无解)则输出0,解可能不只一组,若有多组解时,靠左边的数字较小的那组解则优先输出,请参照SAMPLE OUTPUT。每一笔测资的输出之间也换一行。

SAMPLE INPUT
4
2 5
100 23
3 8
4 25

SAMPLE OUTPUT
2 3

0

2 3 3

2 2 2 19
2 3 3 17
2 3 7 13
2 5 5 13
2 5 7 11


答案

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.util.Stack; public class Prime {
static List<Integer> primeList = new ArrayList<Integer>(); static List<Integer> starttList = new ArrayList<Integer>();
static List<Integer[]> engtList = new ArrayList<Integer[]>(); static Integer[] iArr ;
static boolean flage; //static int a;
static int size; static Stack<Integer> stack = new Stack<Integer>(); static Stack<Integer> stackResult = new Stack<Integer>();
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int z = scan.nextInt();
while(z>0){
flage = false;
stack.clear();
stackResult.clear();
iArr =null;
primeList =new ArrayList<Integer>();
engtList = new ArrayList<Integer[]>(); // System.out.println("请输入您要结果的size:"); size = scan.nextInt(); // System.out.println("请输入您要结果数为:");
int num = scan.nextInt(); for(int i=2;i<=num;i++){
if(isPrime(i)){
primeList.add(i);
}
} iArr = primeList.toArray(new Integer[0]);
sort(iArr); Calc(num, iArr);
int j=0;
if(!flage){
System.out.println(j);
}
z--; Comparator<Integer[]> comparator = new Comparator<Integer[]>() {
public int compare(Integer[] s1, Integer[] s2) {
for(int i=0;i<s1.length;i++){
if(s1[i]!=s2[i]){
return s1[i]-s2[i];
}
}
return 1; }
}; //这里就会自动根据规则进行排序
Collections.sort(engtList,comparator);
for(int i=0;i<engtList.size();i++){
Integer[] stu=engtList.get(i);
for(int ii:stu){
System.out.print(ii+" ");
}
System.out.println();
}
if(z>0){
System.out.println();
} } } private static void Calc(int a,Integer[] iArr)
{
for (int i = 0; i < iArr.length; i++)
{
if (iArr[i] == a && size==1)
{
//输出这个数
System.out.println(iArr[i]);
flage=true;
continue;
}
// else if (iArr[i] > a)
// {
// continue;
// }
if (iArr[i] > a)
{
continue;
}
stack.clear();
stack.push(iArr[i]);
Func(i, a - iArr[i]);
}
} private static void Func(int i, int iValue)
{
for (int j = i ; j < iArr.length; j++)
{
if (iArr[j] > iValue)
{
continue;
}
else if (iValue == iArr[j])
{
stack.push(iArr[j]);
//输出stack 这一步略..
if(stack.size()==size){
iteratorThroughIterator(stack); }
stack.pop();
}
else if (iValue > iArr[j])
{ stack.push(iArr[j]);
Func(j, iValue - iArr[j]);
stack.pop();
}
}
} /**
* 通过迭代器遍历Stack
*/
public static void iteratorThroughIterator(List list) { Integer val = null;
for(Iterator iter = list.iterator(); iter.hasNext(); ) {
val = (Integer) iter.next();
stackResult.push(val);
// System.out.print(val+" ");
}
// System.out.println();
playResult(stackResult);
stackResult.clear();
} /**
* 通过迭代器遍历Stack
*/
public static void playResult(Stack stack) {
flage = true;
while(!stack.empty()){ starttList.add((Integer) stack.pop());
}
Integer[] a= starttList.toArray(new Integer[0]);
starttList.clear();
engtList.add(a.clone());
a=null; // System.out.println(); } public static boolean isPrime(int num){ for (int i = 2; i < num; i++) {//运行效率不高
if ((num % i) == 0) { return false;
}
}
return true;
}

public static void sort(Integer[] iArr){
for (int i = 0; i < iArr.length -1; i++){
for(int j = 0 ;j < iArr.length - i - 1; j++){
if(iArr[j] < iArr[j + 1]){
int temp = iArr[j];
iArr[j] = iArr[j + 1];
iArr[j + 1] = temp;
}
}
}
}
}

Prime Solutions的更多相关文章

  1. SPOJ Prime or Not - 快速乘 - 快速幂

    Given the number, you are to answer the question: "Is it prime?" Solutions to this problem ...

  2. Prime Ring Problem

    Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ... ...

  3. hdoj 1016 Prime Ring Problem

    Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ... ...

  4. hdu 1016 Prime Ring Problem(DFS)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  5. HDU 1016 Prime Ring Problem(经典DFS+回溯)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  6. 杭电oj 1016 Prime Ring Problem

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  7. hdu 1016 Prime Ring Problem(深度优先搜索)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  8. HDU1016 Prime Ring Problem(DFS回溯)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  9. HDU 1016 Prime Ring Problem (DFS)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  10. Jordan Lecture Note-6: The Solutions of Nonlinear Equation.

    The Solutions of Nonlinear Equation 本文主要介绍几种用于解非线性方程$f(x)=0$的一些方法. (1) Bisection Method. 算法: step 1: ...

随机推荐

  1. WinDbg 加载 dotnet core 的 sos.dll 辅助调试方法

    本文告诉大家如何加载 .NET Core 或 .NET 5 的 sos.dll 文件到 WinDbg 的方法 和 .NET Framework 或 dotnet core 2.0 不相同的是,当前的 ...

  2. SQL server 数据库巡检

    SELECT name FROM sysobjects where xtype='u' and name <>'XzryGzGrant' AND name LIKE 'XzryGzGran ...

  3. ChatGPT,我的 .NET 应用该选择哪种日志框架?

    When using .NET 6, compare different main stream third-party logging packages, and give suggestions ...

  4. Mybatis学习三(动态sql语句)

    动态sql语句主要为以下语句 1.动态SQL:if 语句2.动态SQL:if+where 语句3.动态SQL:if+set 语句4.动态SQL:choose(when,otherwise) 语句5.动 ...

  5. 《Effective C++》第三版-4. 设计与声明(Design and Declarations)

    目录 条款17:让接口容易被正确使用,不易被误用(Make interfaces easy to use correctly and hard to use incorrectly) 限制类型和值 规 ...

  6. AI编译器CINN v.s TVM 中CodeGen 源码解读

    如下的技术点梳理仅以「日常优化工作」为牵引点,涉及哪个模块,就具体去看哪个模块的代码. 一.CINN 框架 CINN 中CodeGen之后的代码编译主要交给了Compiler类来负责.核心的函数主要是 ...

  7. 报错:Client does not support authentication protocol requested by server; consider upgrading MySQL cli

    IDEA启动项目登录时显示用户或密码错误 或者 连接mysql数据库时报错 原因: mysql8 之前的版本中加密规则是mysql_native_password,而在mysql8之后,加密规则是ca ...

  8. VueJS和Javascript实现文字上下滚动效果

    一提到文字上下滚动,我们就会想到用不同的程序去实现,而且页面中有文字滚动会增加这个网页的互动和可信度. 1.Js最简单的方法是控制盒子的高度,使不断的重复添加 <html> <bod ...

  9. 鸿蒙stage模型

    app.json5全局的配置文件 icon和label是应用列表的 module.json5模块配置文件 中有一个abilities其中的icon和label才是桌面的图标和名称 日志的话就是hail ...

  10. Windows10 在Hyper-V安装lnmp环境docker方式

    1.启用win10虚拟化hyper-v 2.安装docker win10 https://hub.docker.com/editions/community/docker-ce-desktop-win ...