JDK8运行通过,没装JDK8的,你把()->这种Lambda改一下就好了 import java.awt.*; import javax.swing.*;public class CompsDemo { private final JFrame frame; private final JTextField textField; private final JButton runBtn; private final JTextArea textArea;
public CompsDemo() { frame = new JFrame(); frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); frame.getContentPane().setLayout(new BorderLayout());
runBtn = new JButton("Run"); runBtn.addActionListener((e) -> runBtnOnClick());
textField = new JTextField(20);
JLabel label = new JLabel("Enter a number");
JPanel p = new JPanel(); p.add(label); p.add(textField); p.add(runBtn); frame.add(p, BorderLayout.NORTH);
textArea = new JTextArea(); textArea.setEditable(false); JScrollPane sp = new JScrollPane(textArea, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); frame.add(sp, BorderLayout.CENTER);
int max = (int) ((Math.sqrt(n * 8 + 1) - 1) / 2); for (int i = 2; i <= max; i++) { if ((i & 1) == 0) { // i is even int x = i / 2; if (n % x != 0) { continue; } int y = n / x; if ((y & 1) == 0) { // y is even continue; }
// y is odd int z = y / 2 - x + 1;
comps.append(n + "=" + z); for (int j = 1; j < i; j++) { comps.append("+" + (z + j)); } comps.append(System.lineSeparator()); } else { // i is odd if (n % i != 0) { continue; } int x = n / i - i / 2;
public static void main(String[] args) { SwingUtilities.invokeLater(() -> new CompsDemo()); } }
JDK6这老掉牙的东西趁早升级吧。。不过还是帮你改了一下,应该JDK6也可以编译运行了。import java.awt.*; import java.awt.event.*; import javax.swing.*;public class CompsDemo { private final JFrame frame; private final JTextField textField; private final JButton runBtn; private final JTextArea textArea;
public CompsDemo() { frame = new JFrame(); frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); frame.getContentPane().setLayout(new BorderLayout());
runBtn = new JButton("Run"); runBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { runBtnOnClick(); } });
textField = new JTextField(20);
JLabel label = new JLabel("Enter a number");
JPanel p = new JPanel(); p.add(label); p.add(textField); p.add(runBtn); frame.add(p, BorderLayout.NORTH);
textArea = new JTextArea(); textArea.setEditable(false); JScrollPane sp = new JScrollPane(textArea, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); frame.add(sp, BorderLayout.CENTER);
int max = (int) ((Math.sqrt(n * 8 + 1) - 1) / 2); for (int i = 2; i <= max; i++) { if ((i & 1) == 0) { // i is even int x = i / 2; if (n % x != 0) { continue; } int y = n / x; if ((y & 1) == 0) { // y is even continue; }
// y is odd int z = y / 2 - x + 1;
comps.append(n + "=" + z); for (int j = 1; j < i; j++) { comps.append("+" + (z + j)); } comps.append(System.lineSeparator()); } else { // i is odd if (n % i != 0) { continue; } int x = n / i - i / 2;
import java.awt.*;
import javax.swing.*;public class CompsDemo {
private final JFrame frame;
private final JTextField textField;
private final JButton runBtn;
private final JTextArea textArea;
public CompsDemo() {
frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout());
runBtn = new JButton("Run");
runBtn.addActionListener((e) -> runBtnOnClick());
textField = new JTextField(20);
JLabel label = new JLabel("Enter a number");
JPanel p = new JPanel();
p.add(label);
p.add(textField);
p.add(runBtn);
frame.add(p, BorderLayout.NORTH);
textArea = new JTextArea();
textArea.setEditable(false);
JScrollPane sp = new JScrollPane(textArea,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
frame.add(sp, BorderLayout.CENTER);
frame.setMinimumSize(new Dimension(500, 400));
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private void runBtnOnClick() {
int n = Integer.parseInt(textField.getText());
String result = getCompsOf(n);
SwingUtilities.invokeLater(() -> textArea.setText(result));
}
private static String getCompsOf(int n) {
StringBuilder comps = new StringBuilder();
int max = (int) ((Math.sqrt(n * 8 + 1) - 1) / 2);
for (int i = 2; i <= max; i++) {
if ((i & 1) == 0) {
// i is even
int x = i / 2;
if (n % x != 0) {
continue;
}
int y = n / x;
if ((y & 1) == 0) {
// y is even
continue;
}
// y is odd
int z = y / 2 - x + 1;
comps.append(n + "=" + z);
for (int j = 1; j < i; j++) {
comps.append("+" + (z + j));
}
comps.append(System.lineSeparator());
} else {
// i is odd
if (n % i != 0) {
continue;
}
int x = n / i - i / 2;
comps.append(n + "=" + x);
for (int j = 1; j < i; j++) {
comps.append("+" + (x + j));
}
comps.append(System.lineSeparator());
}
}
return comps.toString();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new CompsDemo());
}
}
JDK6这老掉牙的东西趁早升级吧。。不过还是帮你改了一下,应该JDK6也可以编译运行了。import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class CompsDemo {
private final JFrame frame;
private final JTextField textField;
private final JButton runBtn;
private final JTextArea textArea;
public CompsDemo() {
frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout());
runBtn = new JButton("Run");
runBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
runBtnOnClick();
}
});
textField = new JTextField(20);
JLabel label = new JLabel("Enter a number");
JPanel p = new JPanel();
p.add(label);
p.add(textField);
p.add(runBtn);
frame.add(p, BorderLayout.NORTH);
textArea = new JTextArea();
textArea.setEditable(false);
JScrollPane sp = new JScrollPane(textArea,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
frame.add(sp, BorderLayout.CENTER);
frame.setMinimumSize(new Dimension(500, 400));
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private void runBtnOnClick() {
int n = Integer.parseInt(textField.getText());
String result = getCompsOf(n);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
textArea.setText(result);
}
});
}
private static String getCompsOf(int n) {
StringBuilder comps = new StringBuilder();
int max = (int) ((Math.sqrt(n * 8 + 1) - 1) / 2);
for (int i = 2; i <= max; i++) {
if ((i & 1) == 0) {
// i is even
int x = i / 2;
if (n % x != 0) {
continue;
}
int y = n / x;
if ((y & 1) == 0) {
// y is even
continue;
}
// y is odd
int z = y / 2 - x + 1;
comps.append(n + "=" + z);
for (int j = 1; j < i; j++) {
comps.append("+" + (z + j));
}
comps.append(System.lineSeparator());
} else {
// i is odd
if (n % i != 0) {
continue;
}
int x = n / i - i / 2;
comps.append(n + "=" + x);
for (int j = 1; j < i; j++) {
comps.append("+" + (x + j));
}
comps.append(System.lineSeparator());
}
}
return comps.toString();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new CompsDemo();
}
});
}
}
static void lc(int n){
int x,count;
boolean bn;
for(int i=1;i<n/2+1;i++){
x=i;
count=0;
bn=false;
for(int j=i+1;x<n;j++){
x+=j;
count++;
if(x==n){
bn=true;
break;
}
}
if(bn){
System.out.print(n+" = "+i);
for(int j=1;j<=count;j++)
System.out.print(" "+(i+j));
System.out.println();
}
}
}
我用的是最原始,也是最笨的方法。
大神你能告诉我 你这句 int max = (int) ((Math.sqrt(n * 8 + 1) - 1) / 2); 是求什么吗?
还有你求相等的思路是什么。我看你下面 只是取余,看了半天还是没明白啊
具体思路就是连续整数的和有两种情况,奇数个连续整数,和偶数个,奇数个的话,只要判断N能不能被这个奇数整除就行了,比如15能被3整除,那它就一定能被3个连续整数表示,并且中间那个数是15/3=5,然后左边补一个4,右边补一个6就好了,偶数个的话,首先看N能不能被这个偶数的一半整除,能够整除的话拿N去除以这个偶数的一半得到Y,那么这个Y就一定是中间两个数的和,如果Y是奇数,才说明N能被这个偶数个连续整数表示,比如检查30是否可以表示为4个连续整数,首先30能被4/2=2整除,30/2=15是奇数,15是中间两数的和,所以那4个数就是6 7 8 9,可以看到中间两个数7 8之和正好是15。
首先是这个公式 1+2+3+...+max=max(max-1)/2,max(max-1)/2 这的是代表 max*(max-1)/2还是代表 max(max-1)/2是一个函数式.
第2个地方 max(max-1)/2<=N 得到max<=(sqrt(N * 8 + 1) - 1) / 2
我得到的是max<=sqrt(2n+max),大神,你怎么推导到上面的那步的。
数学不好,请见谅!
大神,你这的公式是不是应该是 1+2+3+...+max=max(max-1)/2+max
推倒max(max+1)/2<=N的话用二次方程公式求出右边端点就好了,左边的话是max>=2,因为至少要由两个数组成