求一个算法

175cm 2009-06-11 02:54:38
有一批数据
[1,300,20,21,22,24,25,50,51,53,23,20001]
需要返回
[1]
[300]
[20,25]
[50,53]
就是连续的合并只要首尾, 不连续的返回单个数字, 数据不定.
...全文
51 53 打赏 收藏 转发到动态 举报
写回复
用AI写文章
53 条回复
切换为时间正序
请发表友善的回复…
发表回复
micsolaris 2009-06-12
  • 打赏
  • 举报
回复
呵呵,对我个人而言,不走歪路就是对的路。我认为编程也是这样,没人多少人会去翻你的代码,看看如何的精妙。我只是凭我的第一感觉而把自己想到的方法写出来而已,而结果能实现了,我觉得这就够了,根本没必要去总是想要如何如何的,弄得结果自己头痛了而且还一点代码都没写。我并没反驳我方法笨这点,也灭责怪谁,只是把自己的想法说出来而已
「已注销」 2009-06-12
  • 打赏
  • 举报
回复
[Quote=引用 39 楼 bbschen1988 的回复:]
不错
[/Quote]

笨方法不难实现

排个序,遍历一下
shuai45 2009-06-12
  • 打赏
  • 举报
回复
public static void main(String[] args) {
int end = 0; //邻数首
int head =0; //邻数尾
int join =2; //邻数<join视为连续数(如:1,2,5)
StringBuffer sb = new StringBuffer();
int num[] = new int[]{1,300,20,21,22,24,25,50,51,53,23,20001};
Arrays.sort(num);//排顺
for(int j=0; j<=num.length; j++){
if(j+1<num.length){
/*下一下标数-去当前下标数<=邻数(join),就视为连续数*/
if(num[j+1]-num[j]<=join){
if(head==0){
head = num[j];//保存邻数首
}
}else{
end = num[j];
/*取得[邻数首]和[邻数尾]*/
if(head!=0 && end!=0){
sb.append("["+head+","+end+"]\n");
head = 0;
end = 0;
}else{/*剩余的为单一数(无邻数)*/
sb.append("["+num[j]+"]\n");
}
}
}
}
System.out.println(sb);
}
冰岛男孩 2009-06-12
  • 打赏
  • 举报
回复
mark
  • 打赏
  • 举报
回复
排顺有点错误

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
}
}
}
mn200456 2009-06-12
  • 打赏
  • 举报
回复


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();

}
}
}

--------------------Configuration: <Default>--------------------
1
300
2025
5053
23
20001

Process completed.
mn200456 2009-06-12
  • 打赏
  • 举报
回复
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();

}
}
}


--------------------Configuration: <Default>--------------------
1
300
2025
5053
23
20001

Process completed.
say___baby 2009-06-12
  • 打赏
  • 举报
回复
要不就先排序,再输出哇
youyou0204 2009-06-12
  • 打赏
  • 举报
回复

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++;
}

}



不过输出后就已经排序过了...
冰思雨 2009-06-12
  • 打赏
  • 举报
回复
额。貌似我没看懂楼主算法的意思。楼上的代码作废吧。不好意思。
冰思雨 2009-06-12
  • 打赏
  • 举报
回复
简单写了一个算法,代码还可以优化一些。运行结果应该没错。
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+"]");
}
}
sutdy 2009-06-11
  • 打赏
  • 举报
回复
妈妈
lulu0126 2009-06-11
  • 打赏
  • 举报
回复
mark
bbschen1988 2009-06-11
  • 打赏
  • 举报
回复
不错
micsolaris 2009-06-11
  • 打赏
  • 举报
回复
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();
}
}


[1]
[20,25]
[50,51]
[53]
[300]
[20001]
seaman_xh 2009-06-11
  • 打赏
  • 举报
回复
写了一个

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("]");
}


输出
[1]
[300]
[20, 22]
[24, 25]
[50, 51]
[53]
[23]
Yedy2000 2009-06-11
  • 打赏
  • 举报
回复
引用 19 楼 ltandfyy 的回复:
LZ的意思是即使后面出现了23这个数,虽然没有紧跟着22后出来,但确实是在这组数中的,还是算的,还是要算到20-25中,LZ,是这个意思吗?


不好意思,没看清楚。
Yedy2000 2009-06-11
  • 打赏
  • 举报
回复
测试通过,大家太不厚道了,给个system.out.println的lz怎么用呢?
我来给个返回List<int[]>的

输入int[] a = {1,300,20,0,21,22,24,50,51,53,23,20001,20002};
输出[1][300][20][0][21, 22][24][50, 51][53][23][20001, 20002]



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;
}
sunzerui 2009-06-11
  • 打赏
  • 举报
回复
厉害
顶!
加载更多回复(33)

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧