写一个函数传入一个任意整形数组 判断数组元素是否为连续数字如{1,2,3,4}或{2,1,4,3},而{1,2,4,3}则不是,是返回TRUE,不是返回FALSE
解决方案 »
- 如何用windows的shell语言将某网站添加到允许弹出窗口的网址!!!!!!!!!!!!
- "Fri Jun 24 08:20:21 CST 2011"转换日期型?
- Socket编程的疑问
- 用jsp方式怎么导出有多个sheet内容的excel,一个sheet的很容易,为什么多个的就不行呢?
- java1.4 显示 mysql5.0 中的一个表的内容,结果中文为乱码。
- 谁可以告诉我封装到底是指的什么啊?没分给了~~SORRY了~
- 把一个字符串进行Base64编码,用两种方法为何得到不一样的结果?
- 程序编译出错,请问这段代码错在什么地方?
- 一个发牌的程序,拍光是拿在手上肯定还是不够的,我想给手牌排序
- 一个awt的问题,关于布局管理器
- 简单的细胞分裂算法题
- 计算TableViewer的行数
public class a{
public static void main(String... args)
{
int[] s={1,3,0,4};
System.out.println(lx(s));
}
public static boolean lx(int[] arr)
{
boolean f=false;
Arrays.sort(arr);
int m=0;
for(int i=arr[0];i<=arr.length;i++)
{
if(i!=arr[m++])
{
f=false;
return false;
}
}
return true;
}
}这个第一次可以不比较...无所谓...功能实现就行了...
for(int i=0;i<sorce.length()-1;i++){
if(sorce[i+1]!=(sorce[i]+1)
return false;
}
return true;
}
思路:只要第i+1个数字不是第i歌数字加1,则返回false,循环结束如果都是,那么就返回true
public static boolean function(int[] a){
boolean b=true;
int i=0;
while(b){
if(i+1<a.length){
if(a[i]==a[i+1]+1){
b=true;
}
else {
b= false;
}
i++;
}
return true;
}return false;
}
public static boolean function1(int []a){
for(int i=0;i<a.length-1;i++){
int num=0;
if(a[i]>a[i+1]){
num=a[i];
a[i]=a[i+1];
a[i=1]=num;
}
}for(int i=0;i<a.length;i++){
System.out.println(a[i]);}
if(function(a)){
return true;
}else{
return false;
}
}
}
新手多指教:
function 是用来判断给的数组是否是连续的
function1 是用来判断给的数组能否排列成连续的
public static boolean isLianxu(int [] sorce){
for(int i=0;i<sorce.length()-1;i++){
if((sorce[i+1]+1=(sorce[i]+1))||(sorce[i+1]-1=(sorce[i]+1))
return true;
}
return false;
}
那就是一个for循环
for(int i=0;i<a.length;i++)
{
if((a[i]-a[i+1]=1)||(a[i+1]-a[i]=1)||(a[2i+1]-a[2i]=1)||(a[2i+1]-a[2i]=-1))
return true;
}
return false;
好学生,只解题,无代码。
如题目:
{1,2,3,4}原本就有序 略
{2,1,4,3}表面看无序的,事实上经过一定程度的循环后一定是连续的,如左循环两位{4,3,2,1}那么什么情况才能旋转事实上此处有一个趋势。
由于一定严格要求“连续”,则刚开始的数组的“趋势”和以后任意相邻两数字的“趋势”是一样的(要么++,要么--),一种情况除外就是 ,已经--到本数组最小,或者已经++到本数组
最大了,就要从相反的方向重新以相反的趋势发展。
基于以上思路,为了逻辑清晰,我们可以先遍历一遍 求出最大,最小值(或下标),然后在遍历记下数组发展“趋势”,,一旦不一样返回false,但是遍历到最小(最大)处要除外的。
这里还有一点就是,既然是循环连续的那么max和min的值只有两种可能:差1;差length-1代
码如下public static boolean isL(int[] a)
{
int i;
int max=0,min=0;
for(i=1;i<a.length;++i)
{
if(a[i]>a[max]) max=i;
if(a[i]<a[min]) min=i;
}
if((Math.abs(max-min)!=1)&&(Math.abs(max-min)!=a.length-1))return false;
int status=a[0]-a[1];
for(i=0;i<a.length-1;++i)
{
if(Math.abs(status)!=1)return false;
if((a[i]-a[i+1]!=status)) //不是依次递增或递减时
{
if(((i+1)!=min)&&((i+1)!=max))return false;
}
}
return true;
只测试了几个用例 还望指点
package doudou;public class Test {
public static boolean checkDown(int[] num, int start, int end) {
for (int i = start; i < end - 1; i++) {
if (num[i] > num[i + 1]) {
continue;
} else {
return false;
}
}
return true;
} public static boolean checkUp(int[] num, int start, int end) {
for (int i = start; i < end - 1; i++) {
if (num[i] < num[i + 1]) {
continue;
} else {
return false;
}
}
return true;
} public static boolean loopDown(int[] num, int len) {
int times = num.length / len;
int mod = num.length % len;
boolean flag = false;
int i = 0;
if (times == 1) {
return checkDown(num, 0, len);
}
if (mod != 0
&& checkDown(num, num.length - 1 - mod, num.length - 1) == false) {
flag = loopDown(num, len + 1);
} else {
for (i = 0; i < times; i++) {
if (checkDown(num, i * len, (i + 1) * len) == false) {
break;
}
}
if (i == times) {
flag = true;
} else {
flag = loopDown(num, len + 1);
}
}
return flag;
} public static boolean loopUp(int[] num, int len) {
int times = num.length / len;
int mod = num.length % len;
boolean flag = false;
int i = 0;
if (times == 1) {
return checkUp(num, 0, len);
}
if (mod != 0
&& checkUp(num, num.length - 1 - mod, num.length - 1) == false) {
flag = loopUp(num, len + 1);
} else {
for (i = 0; i < times; i++) {
if (checkUp(num, i * len, (i + 1) * len) == false) {
break;
}
}
if (i == times) {
flag = true;
} else {
flag = loopUp(num, len + 1);
}
}
return flag;
} public static boolean loop(int[] num, int len) {
if (loopDown(num, 2) || loopUp(num, 2)) {
return true;
} else {
return false;
}
} public static void printArr(int[] num) {
System.out.print("{");
for (int i = 0; i < num.length - 1; i++) {
System.out.print(num[i] + ",");
}
System.out.print(num[num.length - 1] + "}");
} public static void main(String[] args) {
int[] a = { 4, 3, 2, 6, 5, 3, 2, 1 };
int[] b = { 1, 4, 7, 2, 3, 5, 9 };
if (loop(a, 2)) {
printArr(a);
System.out.println("连续");
} else {
printArr(a);
System.out.println("不连续");
}
if (loop(b, 2)) {
printArr(b);
System.out.println("连续");
} else {
printArr(b);
System.out.println("不连续");
}
}
}不知道是不是楼主想要的效果,楼主可以找几个用例测试一下。
class LianXu
{
static int[] sz = new int[]{1,2,4,3}; public static void main(String[] args){
System.out.println(isLianxu());
} public static boolean isLianxu(){
int index = 0;
for (int i = 1;i<sz.length ;i++ )
if (sz[index]>sz[i])
index = i;
int direction = 0;
if (index==0){
if (sz[index+1]==sz[index]+1){
direction = 1; index++;
}
else if (sz[sz.length-1]==sz[index]+1){
direction = -1; index = sz.length-1;
}
else{
return false;
}
}
if (index==sz.length-1){
if (sz[index-1]==sz[index]+1){
direction = -1; index--;
}
else if (sz[0]==sz[index]+1){
direction = 1; index = 0;
}
else{
return false;
}
}
int count = sz.length-2;
int next = index; while(count>0){
next += direction;
if (next < 0) {next = sz.length-1;}
else if (next>sz.length-1){ next = 0;}
if (sz[index] == sz[next]){
index = next;
}
else{
return false;
}
count--;
}
return true;
}
}
class LianXu
{
static int[] sz = new int[]{1,2,3,4}; public static void main(String[] args){
System.out.println(isLianxu());
} public static boolean isLianxu(){
int max = 0,min = 0,direction = 0,index = 0;
for (int i = 1;i<sz.length ;i++ ){
if (sz[min]>sz[i]) min = i;
if (sz[max]<sz[i]) max = i;
}
index = min;
if (max-min==1 || max-min==-(sz.length-1)){
direction = -1;
}
else if (max-min==-1 || max-min==sz.length-1){
direction = 1;
}
else {return false;}
int count = sz.length-1;
int next = index; while(count>0){
next += direction;
if (next < 0) {next = sz.length-1;}
else if (next>sz.length-1){ next = 0;}
if (sz[index]+1 == sz[next]){
index = next;
}
else{
return false;
}
count--;
}
return true;
}
}
public class IsSequential {
public static void main(String[] args) {
int[] a={1,3,2,3};
check(a);
}
public static void check(int[] numbers)
{
int numCount;
numCount = numbers.length;
System.out.println(numCount);
if (numCount < 2)
{
System.out.println("此数组是连续的");
}
else if (numCount == 2)
{
if (Math.abs(numbers[0]-numbers[1]) == 1)
System.out.println("此数组是连续的");
else
System.out.println("此数组是不连续的");
}
else
{
for (int i=1;i<numCount-1;i++)
{
if (Math.abs(numbers[i]-numbers[i-1])!=1 && Math.abs(numbers[i]-numbers[i+1])!=1 )
{
System.out.println("此数组是不连续的");
break;
}
else
if (i == numCount-2 ) System.out.println("此数组是连续的");
}
}
}
}难不成是这样的?
public class Test {
public static boolean checkDown(int[] num, int start, int end) {
for (int i = start; i < end - 1; i++) {
if (num[i] > num[i + 1]) {
continue;
} else {
return false;
}
}
return true;
} public static boolean checkUp(int[] num, int start, int end) {
for (int i = start; i < end - 1; i++) {
if (num[i] < num[i + 1]) {
continue;
} else {
return false;
}
}
return true;
} public static boolean loopDown(int[] num, int len) {
int times = num.length / len;
int mod = num.length % len;
boolean flag = false;
int i = 0;
if (times == 1) {
return checkDown(num, 0, len);
}
if (mod != 0
&& checkDown(num, num.length - 1 - mod, num.length) == false) {//刚刚的写错了,应该是checkDown(num, num.length - 1 - mod, num.length)
flag = loopDown(num, len + 1);
} else {
for (i = 0; i < times; i++) {
if (checkDown(num, i * len, (i + 1) * len) == false) {
break;
}
}
if (i == times) {
flag = true;
} else {
flag = loopDown(num, len + 1);
}
}
return flag;
} public static boolean loopUp(int[] num, int len) {
int times = num.length / len;
int mod = num.length % len;
boolean flag = false;
int i = 0;
if (times == 1) {
return checkUp(num, 0, len);
}
if (mod != 0 && checkUp(num, num.length - 1 - mod, num.length) == false) {//刚刚的写错了,应该是checkUp(num, num.length - 1 - mod, num.length)
flag = loopUp(num, len + 1);
} else {
for (i = 0; i < times; i++) {
if (checkUp(num, i * len, (i + 1) * len) == false) {
break;
}
}
if (i == times) {
flag = true;
} else {
flag = loopUp(num, len + 1);
}
}
return flag;
} public static boolean loop(int[] num, int len) {
if (loopDown(num, 2) || loopUp(num, 2)) {
return true;
} else {
return false;
}
} public static void printArr(int[] num) {
System.out.print("{");
for (int i = 0; i < num.length - 1; i++) {
System.out.print(num[i] + ",");
}
System.out.print(num[num.length - 1] + "}");
} public static void main(String[] args) {
int[] a = { 4, 3, 6, 5, 3, 2, 1 };
int[] b = { 1, 4, 7, 2, 3, 5, 9, 5 };
if (loop(a, 2)) {
printArr(a);
System.out.println("连续");
} else {
printArr(a);
System.out.println("不连续");
}
if (loop(b, 2)) {
printArr(b);
System.out.println("连续");
} else {
printArr(b);
System.out.println("不连续");
}
}
}
/*结果
{4,3,6,5,3,2,1}连续
{1,4,7,2,3,5,9,5}不连续
*/不管是不是楼主要,我只是纠正一下我上面的小失误!
这样的解法,我是这样理解的:把数组先按两个一组分成若干组,余下的作为一组,依次判断是不是都是递增或递减的。若两个一组没有得到每个小组是都是递增或递减的,则按三个一组继续这样做,三个不行就四个一组.....直到整个数组一起作为一组还没有得到是递增或递减的,那么就不连续。如果中途得到了就是连续的。
举几个例子:{1,2, 3,4, 5}连续的,{2,1 5,4, 3}连续的,{1,2,3,5,6,7}连续的
{1,4,7,2,3,5,9,5}不连续
解释一下原因:
1,2递增 3,4递增 5单独一个数一组随便算作是递增还是递减 ,所以连续。
2,1递减 5,4递减 3单独一个数一组随便算作是递增还是递减 ,所以连续。
1,2递增 3,5递增 6,7递增,所以连续。也可以这样理解1,2,3递增5,6,7递增,所以连续。
最后一个不管这么分都不连续。
好了,到此告一段落!
if((t != null)&&(t.length == 0))
{
return false;
}
else if(t.length == 1)
{
return true;
}
int steplength , temp , count=0;
steplength = t[1] - t[0];
for(int i=2;i<t.length;i++)
{
temp = t[i] - t[i-1];
if((steplength != temp)&&(count++ == 1))
{
return false;
}
}
return true;
}
忘记这个数组时循环连续了,增加一段首尾步长的判断^_^ public static boolean checkseq(int []t){
if((t != null)&&(t.length == 0))
{
return false;
}
else if(t.length == 1)
{
return true;
}
int steplength , temp , count=0;
steplength = t[1] - t[0];
if((t[0] - t[t.length-1])!=steplength)
{
count++;
}
for(int i=2;i<t.length;i++)
{
temp = t[i] - t[i-1];
if((steplength != temp)&&(count++ == 1))
{
return false;
}
}
return true;
}
/**写一个函数传入一个任意整形数组
* 判断数组元素是否为连续数字如{1,2,3,4}
* 或{2,1,4,3},而{1,2,4,3}则不是,
* 是返回TRUE,不是返回FALSE
* @author hlw
*
*/
public class ContinuousCheck {
public static boolean isContinue(int[] args){
if(args.length<2) return true;
boolean breaked = false;
int step = args[0]<args[1]?1:-1;
for(int i = 0;i<args.length-1;i++){
if(args[i]+step != args[i+1]) {
if(breaked) return false;
else {
breaked = true;
}
}
}
if(breaked&&args[args.length-1]+step != args[0]) return false;
return true;
}
/**
* @param args
*/
public static void main(String[] args) {
System.out.println(ContinuousCheck.isContinue(new int[]{1, 2, 3, 4}));
System.out.println(ContinuousCheck.isContinue(new int[]{4, 3, 2, 1}));
System.out.println(ContinuousCheck.isContinue(new int[]{2, 1, 4, 3}));
System.out.println(ContinuousCheck.isContinue(new int[]{1, 2, 4, 3}));
}
}
[/code]
int step = args[0]<args[1]?1:-1;
改为
int step = args[0]<args[1]&&args[0]+1 == args[1]?1:-1;