★在10以内, 实现不重复相加=10的有几组★
[code]package haha;public class Untitled1 {
public static void main(String[] args) {
{
int sum = 0;
int n = 0;
for (int i = 1; i <= 10; i++) {
for (int j = i; j <= 10; j++) {
sum = i + j;
if (sum == 10) {
n++;
}
}
}
System.out.println("循环的次数" + n);
}
}
}
//我想实现相加=10的有几组[/code]
是连续相加的很简单 如果不是连续相加呢
有9中情况
1+9 1+2+3+4 1+2+7 1+3+6 1+4+5
2+3+5 2+8
3+7
4+6
5+5
[tk02] [tk02] 大家帮我看下! 谢谢了!!!!!!!!
[[it] 本帖最后由 hanzhu3366 于 2008-5-12 20:42 编辑 [/it]] 你上面的程序叫单元加..你找看看啊 上面的程序多了一个大括号! 不是括号问题,第二个for循环有问题! 这个题目看似简单,还真有点小麻烦,希望大家帮我调试下! [tk43] [tk43] 有10种,
1,2,7
1,3,6
1,4,5
2,3,5
1,2,3,4
其他5钟不说了
详细自己看打印,我就不多说了
[code]public class Test {
static int sum=0;
static int count=0;
public static void main(String[] args){
int result=10;//相加后的结果
//因为i=5,j=5在我这里是特殊的,因为他们相等而且相加刚好等于10,所以要特别给count+1,只要result%2==0都要加1
if(result%2==0){
count++;
}
for(int i=1;i<(result/2+1);i++){
System.out.println("开始 i ="+i);
for(int j=i+1;j<10;j++){
System.out.println("开始 j ="+j);
sum=i;
sum+=j;
if(i==1&&j==1){
continue;
}
if(sum>result){
break;
}
if(sum<result){
if((sum+j+1)<result){//表示三个或更多的数相加小于10
add(sum,j+1,result);
}else if (sum+j+1==result) {
count++;
System.out.println("count++:"+count);
}else{
continue;
}
System.out.println();
}
if(sum==result){
count++;
System.out.println("i:"+i+" &&& j:"+j+" count="+count);
break;
}
}
}
System.out.println(count);
}
/**
* @param i
* @param j
* @param result:相加后的结果
*/
public static void add(int i,int j,int result){
System.out.println("add中的i="+i);
int temp=i;
for(;j<result;j++){
i=temp;
i+=j;
if(i<result){
int temp1=i,temp2=j+1;
System.out.println("temp1:"+temp1+" + temp2:"+temp2);
if(temp1+temp2==result){
count++;
System.out.println("temp1:"+temp1+" + temp2:"+temp2+" count="+count);
}
else if(temp1+temp2<result){
add(temp1, temp2,result);
}
}else if(i>result){
break;
}else if(i==result){
count++;
System.out.println("i:"+i+" + j:"+j+" count="+count);
}else{
continue;
}
}
}
}[/code]
回复 6# 的帖子
谢谢你~~[tk05] 6楼的不错![tk05]如果数字太大,这个方法就不行了!
[code]package wwwww;public class Untitled1 {
public static void main(String[] args) {
int cnt2 = 10;
for (int i = 1; i < 10; i++) {
for (int j = 1; j < 10; j++) {
if (i < j) {
if ((i + j) == cnt2) {
System.out.println("两个数相加的有: " + i + "+" + j);
}
}
}
}
for (int k = 1; k < 10; k++) {
for (int n = 1; n < 10; n++) {
for (int m = 1; m < 10; m++) {
if (k < n) {
if (n < m) {
if ((k + n + m) == cnt2) {
System.out.println("三个数相加的有: " + k + " " + n +
" " + m);
}
}
}
}
}
}
for (int i = 1; i < 10; i++) {
for (int j = 1; j < 10; j++) {
for (int k = 1; k < 10; k++) {
for (int m = 1; m < 10; m++) {
if (i < j) {
if (j < k) {
if (k < m) {
if ((i + j + k + m) == cnt2) {
System.out.println("四个数字相加的有: " + i +
" " + j + " " +
k + " " + m);
}
}
}
}
}
}
}
}
}
}[/code] import java.io.*;
class demo{
private int k=0;
private int a[]=new int[100];
public void num(int number)
{
int n1,j,temp=k,i;
for(j = 1 ; j <= number/2 + 1 ; j ++)
{
if(number-j <= 0)
break;
n1 = number - j;
a[k ++] = j;
if(n1 > 1)
num(n1);
else
{
a[k ++] = 1;
System.out.println();
for(i = 0 ; i < k-1 ; i ++)
System.out.print(a[i]);
System.out.print(a[k-1]);
}
k=temp;
}
System.out.printf("\n");;
for(i = 0 ; i < k ;i ++)
System.out.print(a[i]);
System.out.print(number);
}
public static void main(String []arg)
{
int m;
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
String s;
demo d=new demo();
try{
s=br.readLine();
m=Integer.parseInt(s);
d.num(m);
}catch(Exception e)
{
System.out.println(e.toString());
}
}
}
一点小的回答!
我想到的也是最后这个办法,不过这个方法近似于穷举,要知道所有的情况才能这样做,不知道所有的情况就没办法了。回复 10# 的帖子
运行时候结果为空! [tk13] 么原因啊? 我做做的是单元加程序..你要自己输入的数据..然后对你的数据进行分解...用数组方法!
[code]package wwwww;public class Untitled1 {
static int sum = 0;
static int count = 0;
public static void main(String[] args) {
final int result = 10;
int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
for (int i = 0; i < a.length; i++) {
for (int j = 1; j < a.length; j++) {
if ((a[i] + a[j]) == result) {
if (a[i] != a[j]) {
if (a[i] < a[j]) {
System.out.println("result=" + a[i] + "+" + a[j]);
}
}
}
for (int m = 2; m < a.length; m++) {
if ((a[i] + a[j] + a[m]) == result) {
if (a[i] < a[j]) {
if (a[j] < a[m]) {
if (a[i] != a[j]) {
if (a[i] != a[m]) {
if (a[j] != a[m]) {
System.out.println("result=" + a[i] +
"+" +
a[j] + "+" +
a[m]);
}
}
}
}
}
}
for (int n = 3; n < a.length; n++) {
if ((a[i] + a[j] + a[m] + a[n]) == result) {
System.out.println("result=" + a[i] +
"+" + a[j] +
"+" + a[m] +
"+" + a[n]);
}
}
}
}
}
}
}
[/code] 建议你用递归做...背包法..从九开始找..直到指针下降到1 import java.io.*;
class demo{
private int a[]={0,1,2,3,4,5,6,7,8,9};
public boolean knap(int s,int n)
{
if(s<0||(s>0&&n<1))
return false;
if(s==0)
return true;
if(knap(s-a[n],n))
{
System.out.print(a[n]);
return true;
}
knap(s-a[n],n-1);
return knap(s,n-1);
}
public static void main(String []arg)
{
int m;
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
String s;
demo d=new demo();
try{
s=br.readLine();
m=Integer.parseInt(s);
for(int i=9;i>=0;i--)
{
if(d.knap(m,i))
System.out.println();
}
}catch(Exception e)
{
System.out.println(e.toString());
}
}
}
[[it] 本帖最后由 sunkaidong 于 2008-5-13 12:43 编辑 [/it]] 你改该就可以了
页:
[1]
