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. ACMMM2021|在多模态训练中融入“知识+图谱”:方法及电商应用实践

    ​简介: 随着人工智能技术的不断发展,知识图谱作为人工智能领域的知识支柱,以其强大的知识表示和推理能力受到学术界和产业界的广泛关注.近年来,知识图谱在语义搜索.问答.知识管理等领域得到了广泛的应用. ...

  2. 技术干货 | 应用性能提升 70%,探究 mPaaS 全链路压测的实现原理和实施路径

    ​简介: 全链路压测方案下,非加密场景下至少有 70% 的性能提升,加密场景下 10%的性能提升,并在 MGS 扩容完成后可实现大幅的性能提升,调优的结果远超预期. ​ 业务背景 随着移动开发行业的步 ...

  3. WPF 通过 GifBitmapDecoder 调用 WIC 解析 Gif 和进行动画播放的简单方法

    本文告诉大家如何在 WPF 里,通过 GifBitmapDecoder 调用 WIC 层来解析 GIF 图片,然后采用动画的方式进行播放 在上一篇博客告诉大家,可以通过 GifBitmapDecode ...

  4. SHELL脚本获取域名对应的IP地址

    单个获取 编写角本pingip.sh #!/bin/sh ADDR=qq.com TMPSTR=`ping ${ADDR} -c 1 | sed '1{s/[^(]*(//;s/).*//;q}'` ...

  5. 基于FPGA的电子琴设计(按键和蜂鸣器)----第一版

    欢迎各位朋友关注"郝旭帅电子设计团队",本篇为各位朋友介绍基于FPGA的电子琴设计(按键和蜂鸣器)----第一版. 功能说明: 外部输入七个按键,分别对应音符的"1.2. ...

  6. linux time测试命令的运行时间

    在linux中,time命令是用来测试命令的运行时间的,命令的运行时间有三种: ​ real:实际使用时间,该时间包括进程执行时实际使用的 CPU 时间,进程耗费在阻塞上的时间(如等待完成 I/O 操 ...

  7. tkinter时钟(实时更新显示)

    from tkinter import * import time root = Tk() root.geometry('300x200') var = StringVar() def show(): ...

  8. Vue——方法(methods)

    我们用 methods 选项向组件实例添加方法,它应该是一个包含所需方法的对象: <div id="app"></div> <script> c ...

  9. WPF开发快速入门【1】WPF的布局

    概述 本文描述几款WPF中常用的布局控件. Grid Grid是WPF最常用的布局控件. 它把面板分割为固定长和宽的网格,子控件就放置在网格内. <Grid> <Grid.Colum ...

  10. 文件系统(五):exFAT 文件系统原理详解

    前言 exFAT是微软2006年推出的一种文件系统,距今已快二十年,相比于FAT16和FAT32,exFAT还是算年轻.exFAT一直是微软的一个专用文件系统,直到2019年微软发布它的规范,目前微软 ...