1.十进制转十六进制
【Java代码】:
</pre><pre name="code" class="java">// 十进制转化为十六进制,结果为C8。
Integer.toHexString(200);
// 十六进制转化为十进制,结果140。
Integer.parseInt("8C",16);
【C语言代码】:
第一种:输入一个数采用16进制格式输出
#include <stdio.h>
int main()
{
int num = 0;
scanf("%d", &num);
printf("%x", num);//采用16进制输出表示
return 0;
}
第二种: #include <stdio.h>
#include <string.h>
char oNumTable[6] = {'A', 'B', 'C', 'D', 'E', 'F'};
int i;
void dToO(int, char*);
int main()
{
int dNum;
char oNum[100] = {0};
//输入一个十进制数
scanf("%d", &dNum);
//调用转换函数,将十进制转换成以字符串表示的十六进制
dToO(dNum, oNum);
//输出以字符串表示的16进制数据
printf("%s\n", oNum);
return 0;
}
//转换函数
void dToO(int dNum, char* oNum)
{
char temp[100] = {0};
for ( i = 0; (dNum) && (i < 100) ; i++) {
temp[i] = (dNum % 16);//对16取余数
if (temp[i] > 9) {
temp[i] = oNumTable[temp[i] - 10];//大于9则减10
}else{
temp[i] += '0';
}
dNum /= 16;
}
char* p = temp;
while (*(p+1))
p++;
for (i = 0; p != temp - 1; i++, p--) {
oNum[i] = *p;
}
}
2.在一篇英语文章中找到出现次数最多的单词。
package TestMyselfe;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* 哈希表计出现次数
* 在一篇英语文章中找出单词出现次数最多的一个单词
* @author zyq
*/
public class Testdanci {
public static void main(String[] args) {
// 此变量可作为参数,也可重新写一个函数
String wStr = "my name is abc, and he name is aab, and she name is bbc."; //文章
String[] strs = wStr.split(" "); //所有单词
Map<String, Integer> strMap = new HashMap<String, Integer>(); //统计表
for (String s : strs) {
s = trim(s);
if (strMap.containsKey(s)) {
strMap.put(s, strMap.get(s) + 1);
} else {
strMap.put(s, 1);
}
}
//hashmap可以通过迭代器取key,也可以直接通过迭代器取value。所以没有必要这个
Set<String> keySet = strMap.keySet();
//这个set是把hashmap的所有key取出来。并没有做任何操作,但是hasnmap的key本来就允许重复
Iterator<String> it = keySet.iterator();
//这个Set就是数学中的集合,就是hashmap的key,set是不重复元素集合,但是没有办法遍历,所以使用iterator迭代器
int[] m = new int[keySet.size()];
int index = 0;
while (it.hasNext()) { //iterator就是一个迭代器,它的作用就是将一个集合按照它自己的顺序输出出来,所以他的方法只有hasNext和next
m[index++] = strMap.get(it.next());
}
//对单词出现的次数进行快速排序的操作
System.out.println(Arrays.toString(QSortTest(m,0,m.length-1)));
}
/**
* 说问题
* 你看报错啊
* @param world
* @return
*/
public static String trim(String world) {// 清除单词标点符号
char ch = world.charAt(world.length() - 1);
// ASCII码
// 第48~57号为0~9十个阿拉伯数字;65~90号为26个大写英文字母,97~122号为26个小写英文字母
if ((ch >= 48 && ch <= 57) || (ch >= 65 && ch <= 90)
|| (ch >= 97 && ch <= 122)) {
return world;
} else {
return world.substring(0, world.length() - 1);
}
}
private static int[] QSortTest(int b[], int low, int high) {
int pivot;
if (low < high) {
pivot = Partition(b, low, high);// 关键是选取划分的关键字,即就是从哪里开始划分
if (pivot > low) {// 要注意判断边界值
QSortTest(b, low, pivot - 1);
}
if (pivot < high) {
QSortTest(b, pivot + 1, high);
}
}
return b;
}
private static int Partition(int[] c, int low, int high) { // 找记录中划分后的中枢
int privotkey;
privotkey = c[low];// 默认先取数组中第一个元素
while (low < high) { // 从表的两端交替向中间扫描
while (low < high && c[high] >= privotkey)
// 如果低坐标小于高坐标
high--;
swap(c, low, high);// 将比关键字小的记录交换到低端(也就是比他小的排在他的左边)
while (low < high && c[low] <= privotkey)
low++;
swap(c, low, high);// 将比关键字大的记录交换到高端(也就是比他大的排在他的右边)
}
return low;// 返回关键字所在的位置
}
private static void swap(int e[], int m, int n) {
int temp = e[m];
e[m] = e[n];
e[n] = temp;
}
}