23,404
社区成员
发帖
与我相关
我的任务
分享
import java.io.*;
public class Gailv {
public static void main(String[] args) {
int[] array1 = new int[10];
int[] array2 = new int[10];
int[] array3 = new int[10];
for(int i=0; i<10; i++) {
array1[i] = array2[i]= array3[i] = i;
}
System.out.println("请输入要组成的数:");
int number = 0;
boolean flag = true;
int count = 0;
while(flag) {
try{
InputStreamReader ins = new InputStreamReader(System.in);
BufferedReader bf = new BufferedReader(ins);
number = Integer.parseInt(bf.readLine());
//System.out.println("您输入的数是" + number);
if (number >= 0 && number <= 27) {
for(int i=0; i<10&&array1[i]<=number; i++) {
for(int j=0; j<10&&array2[j]<=number; j++) {
for(int k=0; k<10&&array3[k]<=number; k++) {
if(array1[i]+array2[j]+array3[k]==number) {
count++;
}
}
}
}
System.out.println("组成\"" + number + "\"的次数为" + count);
count = 0;
} else {
System.out.println("请输入0~27之间的数字!");
count = 0;
}
} catch(IOException e) {
e.printStackTrace();
} catch(NumberFormatException e) {
System.out.println("请输入数字!");
}
}
}
}
public static void Main()
{
for (int i = -1; i <= 28; i++)
{
int count = compute(i);
System.Console.WriteLine("Total Count:"+count.ToString());
}
System.Console.ReadLine();
}
public static int compute(int sum)
{
int returnValue = 0;
if (sum > 27 || sum < 0)
{
return returnValue;
}
int[] a = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] b = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] c = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
for (int i = 0; i < a.Length; i++)
{
if (a[i] > sum)
{
break;
}
for (int j = 0; j < a.Length; j++)
{
if (b[j] > sum)
{
break;
}
for (int k = 0; k < a.Length; k++)
{
if (c[k] > sum)
{
break;
}
else
{
if (sum == a[i] + b[j] + c[k])
{
returnValue++;
System.Console.Write("sum="+sum.ToString()+"="+a[i].ToString()+"+"+b[j].ToString()+"+"+c[k].ToString()+"\t");
}
}
}
}
}
return returnValue;
}