62,615
社区成员
发帖
与我相关
我的任务
分享
while(k<a.length)
{
if(k<a.length-1&&a[k]>a[k+1])
{
a[k] = a[k]^a[k+1];
a[k+1] = a[k]^a[k+1];
a[k] = a[k]^a[k+1];
k=0;
continue;
}
k++;
}
public static void main(String[] args) throws Exception
{
int a[] ={1,50,300,21,25,22,23,24,20,51,52,53,46,20001};
/*
* 排顺
*/
int k=0;
while(k<a.length)
{
if(k<a.length-1&&a[k]>a[k+1])
{
a[k] = a[k]^a[k+1];
a[k+1] = a[k]^a[k+1];
a[k] = a[k]^a[k+1];
k=0;
}
k++;
}
/*
* 排顺后输出结果
*/
boolean flag = true;//判断只加,顺序的第一个数,的标志位
int i=0;
while(i<a.length)
{
if(i<a.length-1&&a[i]+1==a[i+1])//如果连续,同时没到最后一个数
{
if(flag)
{
System.out.print(a[i]);//则把第一个顺序的数加进去
flag = false;//状态位改为false以后的数就进不来了
}
i++;//继续往后数
}
else
{
System.out.println(a[i]);//当不符合顺序的时候,把当前的数加近来,此时他是顺序的最后一位
i++;
flag = true;//加完顺序的最后一个数标志位再改为true
}
}
}
class HeadTail{
public static void main (String[] arg){
int head = 0;
int tail = 0;
int count = 0; //连续下一个的个数
int[] data = {1,300,20,21,22,23,24,25,50,51,52,53,23,20001};
for(int i = 0;i < data.length; i++){
head = data[i];
System.out.print(head);
while(data.length>i+1 && data[i+1]==data[i]+1 ){
tail = data[i+1];
i++;
count++;
}
if(count != 0){
System.out.print(tail);
count = 0;
}
System.out.println();
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] nums = new int[]{1,300,21,22,24,25,50,51,23,53,30001};
/**
* 排序
*/
int count = nums.length;
for(int i=0;i<count;i++){
for(int j=i+1;j<count;j++){
if(nums[i] > nums[j]){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
}
/**
* 找到连续的(排序后的)
*/
int i = 0;
while(i<count)
{
int num = nums[i];
if(i<count-1){
if((num+1) == nums[i+1]){
System.out.print(num+","); //输出连续首
int j = i+1;
for(;j<count-1;j++){
if(nums[j]+1 < nums[j+1]){
System.out.print(nums[j]); //输出连续尾
System.out.println();
i = j;
break;
}
}
}
else
System.out.println(num); //未连续单独输出
}
i++;
}
}
package houlei.test;
/**
* 有一批数据 [1,300,20,21,22,24,25,50,51,53,23,20001]
* 需要返回
* [1]
* [300]
* [20,25]
* [50,53]
* 就是连续的合并只要首尾,
* 不连续的返回单个数字, 数据不定.
*
* 该类创建于 2009-6-11 23:21:21
*
* @version 1.0.0
* @author 侯磊
*/
public class FindNumber {
private static enum State{Single,First,Last};//用于标明匹配状态
private final static int Default=0;//设置一个特殊的默认值,该值不参与运算。
public static void find(int array []){
//为算法简便,构建一个数组。
int temp [] = new int[array.length+1];
System.arraycopy(array, 0, temp, 0, array.length);
temp[array.length]=Default;
array=temp;
//初始化默认值
State state=State.Single;
int first=Default;
//循环遍历数组并输出结果
for(int i=1;i<array.length;i++){
if(array[i-1]==array[i]-1){
switch(state){
case Single :
first=array[i-1];
state=State.First;
continue;
case First:
continue;
case Last:
continue;
}
}else{
switch(state){
case Single :
print(array[i-1]);
continue;
case First:
state=State.Last;
continue;
case Last:
state=State.Single;
print(first,array[i-1]);
continue;
}
}
}
}
public static void main(String[] args) {
int array [] = new int[]{1,300,20,21,22,24,25,50,51,53,23,20001};
find(array);
}
public static void print(int i){
System.out.println("["+i+"]");
}
public static void print(int i,int j){
System.out.println("["+i+","+j+"]");
}
}
package com.ricky.www;
/**
*
* 有一批数据 [1,300,20,21,22,24,25,50,51,53,23,20001] 需要返回 [1] [300] [20,25] [50,53]
* 就是连续的合并只要首尾, 不连续的返回单个数字, 数据不定.
*
*/
public class Test {
public static void main(String[] args) {
int[] array = { 1, 300, 20, 21, 22, 24, 25, 50, 51, 53, 23, 20001 };
sort(array);
String result = coordinate(array);
System.out.println(result);
}
public static void sort(int[] array) {
int length = array.length;
for (int i = 0; i < length; i++) {
int temp = array[i];
for (int j = i; j < length; j++) {
if (temp > array[j]) {
temp = array[j];
array[j] = array[i];
array[i] = temp;
}
}
}
}
public static String coordinate(int[] array) {
int length = array.length;
int start = 0;
int end = 0;
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < length; i++) {
start = array[i];
end = array[i];
int j = 1;
int k = 0;
for (; j < length - i - 1; j++) {
if (array[i] + j == array[i + j]) {
end = array[i + j];
k ++;
}
}
i = i + k;
if (start == end) {
buffer.append("[" + start + "]\n");
} else {
buffer.append("[" + start + "," + end + "]\n");
}
}
return buffer.toString();
}
}
public static void main(String[] args) {
int a[] ={1,300,20,21,22,24,25,50,51,53,23,24};
int last = a[0]; // 记录上一个元素
int count = 1; // 记录连续数字的个数
System.out.print("[" + a[0]);
for(int i=1;i<a.length;i++) {
if(a[i] == last + 1) {
count ++;
} else {
if(count > 1)
System.out.print(", " + last);
System.out.print("]\n[" + a[i]);
count = 1;
}
last = a[i];
}
System.out.println("]");
}
public static List<int[]> getResult(int a[]) {
List<int[]> result = new ArrayList<int[]>();
boolean ok=false;//是否输出
boolean flag=false;//是否连续
int left = 0;
int right = 0;
for (int i = 0; i < a.length; i++) {
if(flag){//连续,
right=a[i];
if(i!=a.length-1&&right+1!=a[i+1]){
ok=true;
}
}else{//非连续
left=a[i];
if(i!=a.length-1&&left+1==a[i+1]){
flag=true;
}else{
ok=true;
}
}
if(ok||i==a.length-1){//输出
if(flag){
int[] temp = new int[2];
temp[0] = left;
temp[1] = right;
result.add(temp);
right=0;
}else{
int[] temp = new int[1];
temp[0]=left;
result.add(temp);
}
left=0;
ok=false;
flag=false;
}
}
return result;
}