两个超大正整数相加
String a = "20394803294";
String b = "20394803294";
StringBuffer buffer = new StringBuffer();
int bound = 0;
int i = a.length()-1;
int j = b.length()-1;
while(i >=0 || j >= 0){
int sum =i < a.length() ? Integer.valueOf(a[i]): 0 + j<b.length()? Integer.valueOf(b[j]):0 + bound;
buffer.append(sum%10);
bound = sum/10;
i--;
j--;
}
if(bound > 0){
buffer.append(bound);
}
System.out.println(buffer.reverse().toString());
设计一个多线程下载模块,支持断点续下载
public void main() {
String url = "";
String savePath = "";
long total = getTotalLength(url);
int size = 3;
long interval = total / size;
for (int i = 0; i < size; i++) {
long start = interval * i + (i == 0 ? 0 : 1);
long end = Math.min(interval * (i + 1), total);
ChunkTask task = new ChunkTask(String.valueOf(i), url, savePath, total, start, end, new DownloadCallback() {
@Override
public void onSuccess(ChunkTask task) {
if (allChuckTaskSuccess()) {
renameFile();
}
}
});
AsyncTask.execute(task);
}
}
public class ChunkTask implements Runnable {
public String id;
public String url;
public String savePath;
public long total;
public long start;
public long end;
public int status;
public DownloadCallback callback;
public ChunkTask(String id, String url, String savePath, long total, long start, long end, DownloadCallback callback) {
this.id = id;
this.url = url;
this.savePath = savePath;
this.total = total;
this.start = start;
this.end = end;
this.callback = callback;
}
@Override
public void run() {
if (FileUtils.IsFileExist(savePath) && FileUtils.getFileLen(savePath) > 1024) {
return;
}
InputStream is = null;
RandomAccessFile raf = null;
BufferedReader markIs = null;
RandomAccessFile markOs = null;
HttpURLConnection connection = null;
try {
String tempPath = savePath + ".temp";
String markPath = savePath + id + ".mark";
markIs = new BufferedReader(new FileReader(markPath));
long current = StringUtils.getIntValue(markIs.readLine(), start);
URL netUrl = new URL(url);
connection = (HttpURLConnection) netUrl.openConnection();
connection.addRequestProperty("Range", "bytes=" + (current + start) + "-" + (end == 0 ? "" : end));
connection.connect();
raf = new RandomAccessFile(tempPath, "rwd");
markOs = new RandomAccessFile(markPath, "rwd");
raf.setLength(total);
raf.seek(current);
is = connection.getInputStream();
int count = 0;
byte[] buffer = new byte[1024];
while ((count = is.read(buffer)) != -1) {
current += count;
raf.write(buffer, 0, count);
markOs.seek(0);
markOs.write(String.valueOf(count).getBytes());
}
callback.onSuccess(this);
} catch (Exception e) {
e.printStackTrace();
} finally {
closeIO();
}
}
}
interface DownloadCallback {
void onSuccess(ChunkTask task);
}
0到100打印输出,要求两个线程交替顺序打印
static class SoulutionTask implements Runnable{
static int value = 0;
@Override
public void run() {
while (value <= 100){
synchronized (SoulutionTask.class){
System.out.println(Thread.currentThread().getName() + ":" + value++);
SoulutionTask.class.notify();
try {
SoulutionTask.class.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
快速排序:
public void qsort(int[] list, int left, int right) {
if (left < right) {
int pivot = left;
int i = left;
int j = right;
while (i < j) {
while (i < j && list[j] > list[pivot]) {
j--;
}
while (i < j && list[i] < list[pivot]) {
i++;
}
if (i < j) {
swap(list, i, j);
}
}
swap(list, pivot, i);
qsort(list, left, i - 1);
qsort(list, j + 1, right);
}
}
冒泡排序:
public void sort(int[] list) {
for (int i = 0; i < list.length - 1; i++) {
for (int j = 0; j < list.length - 1 - i; j++) {
if (list[j] > list[j + 1]) {
int temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
}
}
}
}