求为电话号码划分等级的算法

BlackBabyzmc 2010-03-16 09:28:01
11位数字匹配划分等级(以尾几位算)
ABC和AA为二星
AABB和ABAB为三星
AAA,ABCD为四星
AAAA,ABCDE,AAAAA为五星
其他为一星
例如,136487812522为二星,
1364878100022为三星,
1364878112333为四星,
1364878105555为五星
...全文
381 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
hanwangyu 2010-03-16
  • 打赏
  • 举报
回复
String number = "13607371234567";
char[] cnum = number.toCharArray();
// System.out.print(cnum[cnum.length-1]);
int len = cnum.length - 1;
int num = cnum[cnum.length - 1];

if (cnum[len] == cnum[len - 1] && cnum[len] == cnum[len - 2]
&& cnum[len] == cnum[len - 3] && cnum[len] == cnum[len - 4]) {
System.out.println("此数为: 五星");
} else if (num - 1 == (int) cnum[len - 1]
&& (int) cnum[len - 1] - 1 == (int) cnum[len - 2]
&& (int) cnum[len - 2] - 1 == (int) cnum[len - 3]
&& (int) cnum[len - 3] - 1 == (int) cnum[len - 4]) {
System.out.println("此数为: 五星");
}
hanwangyu 2010-03-16
  • 打赏
  • 举报
回复
根据你所说的,,我认为你五星级只有两种情况,1。最后五位数是连续自然数,2。五个相同的自然数。

不知是否正确
String number="136073712345";
char[] cnum=number.toCharArray();
//System.out.print(cnum[cnum.length-1]);
int len=cnum.length-1;
int num=cnum[cnum.length-1];

if (cnum[len]==cnum[len-1] && cnum[len]==cnum[len-2] &&cnum[len]==cnum[len-3] &&cnum[len]==cnum[len-4] ){
System.out.println("此数为: 五星");
}else if(num-1==(int)cnum[len-1] && (int)cnum[len-1]-1==(int)cnum[len-2] && (int)cnum[len-2]-1==(int)cnum[len-3] && (int)cnum[len-3]-1==(int)cnum[len-4] ){
System.out.println("此数为: 五星");
}
BlackBabyzmc 2010-03-16
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 nickycheng 的回复:]

不懂,你这个abc是指每个位置不相同,还是指给定的真实数字?

如果前者,那么ABC二星 ABCD四星 ABCDE五星 这几个最后3位不都是符合要求的,判断2 3 4星有啥意义
[/Quote]

ABCDE 这表示数字的尾几位,正如你说的,但是按优先级看要先看他是不是五星,问题五星又有很多情况,五星是最高等级,比如尾数有4位以上相同或者4位以上顺序的(比如尾数为12345,23456,1234567都是五星)
BlackBabyzmc 2010-03-16
  • 打赏
  • 举报
回复
[Quote=引用楼主 blackbabyzmc 的回复:]
11位数字匹配划分等级(以尾几位算)
ABC和AA为二星
AABB和ABAB为三星
AAA,ABCD为四星
AAAA,ABCDE,AAAAA为五星
其他为一星
例如,136487812522为二星,
1364878100022为三星,
1364878112333为四星,
1364878105555为五星
[/Quote]

对不起,大家我可能没有说清楚136487815555513648781555551364878112345 也是五星
wfeng007 2010-03-16
  • 打赏
  • 举报
回复
逆向读取。 撰写有限自动机的流程图。然后用表达式 简化算法 就好了。。。。


Dazzlingwinter 2010-03-16
  • 打赏
  • 举报
回复
五星级别高,那就先判断五星好了, 刚才的顺序换一下就好~
Dazzlingwinter 2010-03-16
  • 打赏
  • 举报
回复

String number = "13968021234";
char[] c = number.toCharArray();
if(c[10] == c[9] || (c[10} != c[9] && c[10] != c[8] != c[9] != c[8])) {
return 2;
}else if((c[10] == c[9] && c[8] == c[7] && c[10] != c[8]) || (c[10] == c[8] && c[9] == c[7] && c[10] != c[9])){
return 3;
}else {
...
}
//Have A Try...
hbgzg3006 2010-03-16
  • 打赏
  • 举报
回复
这样的是不是也算二星呢?
136487812123 136487812321
SambaGao 2010-03-16
  • 打赏
  • 举报
回复
试试正则表达式.................
nickycheng 2010-03-16
  • 打赏
  • 举报
回复
不懂,你这个abc是指每个位置不相同,还是指给定的真实数字?

如果前者,那么ABC二星 ABCD四星 ABCDE五星 这几个最后3位不都是符合要求的,判断2 3 4星有啥意义
xiachedan 2010-03-16
  • 打赏
  • 举报
回复
一共就4位,排列组合就出来了
nickycheng 2010-03-16
  • 打赏
  • 举报
回复
笨方法实现。


public static void main(String[] args) {
String tel = "13851513313";
char[] num = tel.toCharArray();

if (eq(num, 5) || eq(num, 4) || ne(num, 5)) {
System.out.println("5");
} else if (eq(num, 3) || ne(num, 4)) {
System.out.println("4");
} else if (lv3(num)) {
System.out.println("3");
} else if (eq(num, 2) || ne(num, 3)) {
System.out.println("2");
} else {
System.out.println("1");
}
}

// 三星单独判断
public static boolean lv3(char[] c) {
if (c[10] == c[8] && c[7] == c[9]) {
return true;
}
if (c[10] == c[9] && c[7] == c[8]) {
return true;
}
return false;
}

// 是否从末位开始全不等
public static boolean ne(char[] c, int length) {
c = Arrays.copyOfRange(c, 11 - length, 11);
Arrays.sort(c);
for (int i = 0; i < c.length - 1; i++) {
if (c[i] == c[i + 1]) return false;
}
return true;
}

// 是否从末位开始,连续相等
public static boolean eq(char[] c, int length) {
c = Arrays.copyOfRange(c, 11 - length, 11);
for (int i = 0; i < c.length - 1; i++) {
if (c[i] != c[i + 1]) return false;
}
return true;
}

62,612

社区成员

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

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