应你的要求 我只做了加一位的和加两位的
算法类代码如下:
public class CountSumClass {
private String number
private String result = new String("")
public CountSumClass() {
number = new String()
}
public CountSumClass(String str) {
number = str
}
public String getResult() {
return result
}
public void addOne() {
int x,y
for (int i = 0i <number.length()-1i++) {
x = (int)number.charAt(i) - 48
for(int j = i+1j<number.length()j++) {
y = (int)number.charAt(j) - 48
result += x+"+"+y+"="+(x+y)+"\n"
}
}
y=0
for (int i = 0i<number.length()i++) {
x = (int)number.charAt(i) - 48
y += x
if(i == number.length()- 1) {
result += x+"="+y
} else {
result += x+"+"
}
}
}
public void addTwo() {
int x,y,z
result += "\n"
for (int i = 0i<number.length()-1i++) {
String substr = number.substring(i,i+2)
x = Integer.parseInt(substr)
z = (x%10) * 10 + (x/10)
for(int j=0j<number.length()j++) {
if(j!=i &&j!=i+1) {
y = (int)number.charAt(j) - 48
result += x+"+"+y+"="+(x+y)+"\n"
result += z+"+"+y+"="+(z+y)+"\n"
}
}
}
}
public String toString() {
return result
}
}
希望能帮助你哈
需要使用多线程
package org.xuyh.design
import java.util.Scanner
/**
* N个苹果放到20个袋子中, *** 作时间为4个小时,其中每个袋子的各苹果放入时间间隙大于30分钟
* @author XuYanhang
*
*/
public class AppleOperation implements Runnable{
public static final int ONE_SECOND = 1000
private final int[] packages
public final int operationTime
public final int minSpacetTime
private int appleCount
private boolean timeEnd = false
public AppleOperation(int appleCount,int packCount,int operationTime,int minSpacetTime){
this.appleCount = appleCount
packages = new int[packCount]
this.operationTime = operationTime
this.minSpacetTime = minSpacetTime
}
public int getAppleCount(){
return appleCount
}
public int getPackCount(){
return packages.length
}
public int getOperationTime(){
return operationTime
}
public boolean hasEnd(){
return timeEnd || appleCount == 0
}
public synchronized void setTimeEnd(){
timeEnd = true
}
public void run() {
int pack = Integer.parseInt(Thread.currentThread().getName())
while(appleCount>0){
synchronized(this){
if(hasEnd()){
break
}
appleCount--
packages[pack]++
System.out.println("一个苹果放入了第"+(pack+1)+"个袋子中,还剩"+appleCount+"个苹果")
}
for(int i = 0 i <minSpacetTime i++){
synchronized(this){
if(hasEnd()){
break
}
}
try {
Thread.sleep(ONE_SECOND)
} catch (InterruptedException e) {
e.printStackTrace()
}
}
}
synchronized(this){
System.out.println("第"+(pack+1)+"个袋子最终有"+packages[pack]+"个苹果")
}
}
//Test main method
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in)
int appleCount = -1
do{
System.out.print("N个苹果放到20个袋子中, *** 作时间为4个小时,其中每个袋子的各苹果放入时间间隙大于30分钟。\n"
+ "输入苹果总数N:")
String string = scanner.next()
if(null != string &&string.matches("^\\d{1,8}$")){
appleCount = Integer.parseInt(string)
}
}while(appleCount == -1)
scanner.close()
AppleOperation operation = new AppleOperation(appleCount,20,4*60*60,30*60)
new WaitThread(operation).start()
for (int i = 0i <operation.getPackCount()i++) {
Thread thread = new Thread(operation,""+i)
thread.start()
}
}
static class WaitThread extends Thread{
private AppleOperation operation
public WaitThread(AppleOperation operation){
this.operation = operation
}
public void run(){
try {
Thread.sleep(operation.getOperationTime()*ONE_SECOND)
} catch (InterruptedException e) {
e.printStackTrace()
}
operation.setTimeEnd()
System.out.println("时间到,苹果装袋 *** 作结束,最后为装袋的苹果个数:"+operation.getAppleCount())
}
}
}
这里我们额外启动了21个线程,其中一个是计4小时的定时的,其他20个是向每个袋子中放苹果的线程。
1:HelloWorldApp1.//
2.//
HelloWorld
应用示例
3.//
4.public
class
HelloWorldApp{
5.
public
static
void
main
(String
args[])
{
6.System.out.println
("Hello
World!")
7.
}
8.}
以上程序行是在你的屏幕上打印“Hello
World!”所需的最少代码。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)