67,513
社区成员
发帖
与我相关
我的任务
分享
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// 记录大写字母出现次数
int[] UpCaseCount = new int[26];
// 记录小写字母出现次数
int[] LowCaseCount = new int[26];
char f[] = { 'a', 'A', 'h', 'f', 'H', 'F' };
int i, j, k;
// 遍历字符串
for (i = 0; i < f.length; i++) {
// 记录大写字母出现次数
if (f[i] >= 'A' && f[i] <= 'Z') {
UpCaseCount[f[i] - 65]++;
}
// 记录小写字母出现次数
if (f[i] >= 'a' && f[i] <= 'z') {
LowCaseCount[f[i] - 97]++;
}
}
// 遍历输出
for (k = 0; k < 26; k++) {
// k = 0表示 a||A .... k = 25表示z||Z
// 输出大写字母
for (i = 0; i < UpCaseCount[k]; i++)
System.out.print((char) (65 + k));
// 输出小写字母
for (j = 0; j < LowCaseCount[k]; j++)
System.out.print((char) (97 + k));
}
}
}
这种题目完全是算法思想题,建议不要使用 Map ,会使用类包固然好,但是不能不去思考算法
public void SortTest() {
char[] list = { 'a', 'A', 'h', 'f', 'H', 'F' };
// 按顺序先把最小的数先放到奇数位上
for (int i = 0; i < list.length; i++) {
for (int j = 0; j < list.length; j++) {
if (list[i] < list[j]) {
list[i] = (char) (list[i] + list[j]);
list[j] = (char) (list[i] - list[j]);
list[i] = (char) (list[i] - list[j]);
}
j++;
}
}
// 再按顺序把剩下的数放到偶数位上
for (int i = 1; i < list.length; i++) {
for (int j = 0; j < list.length; j++) {
if (list[i] < list[j]) {
list[i] = (char) (list[i] + list[j]);
list[j] = (char) (list[i] - list[j]);
list[i] = (char) (list[i] - list[j]);
}
}
i++;
}
for (int k = 0; k < list.length; k++) {
System.out.print(list[k] + " ");
}
}
import java.util.Comparator;
public class CharComparator implements Comparator<Character>{
public int compare(Character c1, Character c2) {
double c1_value = c1;
double c2_value = c2;
if(c1_value >= 'a'){
c1_value = Character.toUpperCase(c1) + 0.5;
System.out.println(c1_value);
}else{
c1_value -= 0.4;
}
if(c2_value >= 'a'){
c2_value = Character.toUpperCase(c2) + 0.5;
}else{
c2_value -= 0.4;
}
return c1_value - c2_value > 0 ? 1 : -1 ;
}
}
public static void main(String[] args){
Character[] f = new Character[] {'a','A', 'h', 'F', 'f', 'H'};
Arrays.sort(f, new CharComparator());
System.out.println(Arrays.toString(f));
}
/**
* @param args
*/
public static void main(String[] args) {
Character[] f = new Character[] {'a','A', 'h', 'F', 'f', 'H'};
Arrays.sort(f);
Arrays.sort(f, new Comparator<Character>(){
@Override
public int compare(Character o1, Character o2) {
String s1 = String.valueOf(o1.charValue());
String s2 = String.valueOf(o2.charValue());
String s1new = s1.toUpperCase();
String s2new = s2.toUpperCase();
if(s1new.equals(s2new)){
if(s1.equals(s1new) && s2.equals(s2new)){
/**
* 两个都是大写字母
*/
return 0;
}else if(s1.equals(s1new)){
/**
* 此处说明o1是大写字母,o2是小写字母
*/
return -1;
}else if(s2.equals(s2new)){
/**
* 此处说明o1是小写字母,o2是大写字母
*/
return 1;
}else{
/**
* 两个都是小写字母
*/
return 0;
}
}else{
return s1new.compareTo(s2new);
}
}
});
for(Character c : f){
System.out.println(c);
}
}
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
public class SortTest implements Comparable<SortTest>{
private char c;
private int v;
private static final Map<Character, Integer> DICT = new HashMap<Character, Integer>();
static{
char c[] = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz".toCharArray();
for (int i = 0; i < c.length; i++) {
DICT.put(c[i], i);
}
}
public SortTest(char c){
this.c = c;
this.v = DICT.get(c);
}
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + v;
return result;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
SortTest other = (SortTest) obj;
if (v != other.v)
return false;
return true;
}
public String toString() {
return String.valueOf(c);
}
public int compareTo(SortTest o) {
return o.v < this.v ? 1 : -1;
}
public static void main(String[] args) {
char strs[] = "aAhfHF".toCharArray();
Set<SortTest> ss = new TreeSet<SortTest>();
for (int i = 0; i < strs.length; i++) {
ss.add(new SortTest(strs[i]));
}
System.out.println(ss);//out: [A, a, F, f, H, h]
}
}
public class Demo_ {
public static void main(String args[]) {
String str = "f[a,A,h,f,H,F]";
System.out.println(Check(str));
}
public static String Check(String str) {
String resstr=str;
StringBuffer reqstr =new StringBuffer();
String replstr = null;
String strLowDic = "[a-z]";//定义字典。
//String strUppDic = "[A-Z]";
reqstr.append(String.valueOf(resstr.charAt(0)));
for (int i = 1; i < str.length(); i++) {
replstr = String.valueOf((resstr.charAt(i)));
if (replstr.matches(strLowDic)){
replstr = replstr.replace(replstr, replstr.toUpperCase());
}else
replstr = replstr.replace(replstr, replstr.toLowerCase());
reqstr.append(replstr);
}
return reqstr.toString();
}
}
public class Demo_ {
public static void main(String args[]) {
String str = "f[a,A,h,f,H,F]";
System.out.println(Check(str));
}
public static String Check(String str) {
String resstr=str;
StringBuffer reqstr =new StringBuffer();
String replstr = null;
String strLowDic = "[a-z]";
String strUppDic = "[A-Z]";
reqstr = reqstr.append(String.valueOf(resstr.charAt(0)));
for (int i = 1; i < str.length(); i++) {
replstr = String.valueOf((resstr.charAt(i)));
if (replstr.matches(strLowDic)){
replstr = replstr.replaceAll(strLowDic, replstr.toUpperCase());
}else
replstr = replstr.replaceAll(strUppDic, replstr.toLowerCase());
reqstr = reqstr.append(replstr);
}
return reqstr.toString();
}
}