
1.统计一篇英文文章单词个数。
public class WordCounting {
public static void main(String[] args) {
try(FileReader fr = new FileReader("a.txt")) {
int counter = 0;
boolean state = false;
int currentChar;
while((currentChar= fr.read()) != -1) {
if(currentChar== ' ' || currentChar == '\n'
|| currentChar == '\t' || currentChar == '\r') {
state = false;
}
else if(!state) {
state = true;
counter++;
}
}
System.out.println(counter);
}
catch(Exception e) {
e.printStackTrace();
}
}
}
补充:这个程序可能有很多种写法,这里选择的是Dennis M. Ritchie和Brian W. Kernighan老师在他们不朽的著作《The C Programming Language》中给出的代码,向两位老师致敬。下面的代码也是如此。
2.输入年月日,计算该日期是这一年的第几天。
public class DayCounting {
public static void main(String[] args) {
int[][] data = {
{31,28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{31,29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
Scanner sc = new Scanner(System.in);
System.out.print("请输入年月日(1980 11 28): ");
int year = sc.nextInt();
int month = sc.nextInt();
int date = sc.nextInt();
int[] daysOfMonth = data[(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)?1 : 0];
int sum = 0;
for(int i = 0; i <p><strong>3.回文素数:所谓回文数就是顺着读和倒着读一样的数(例如:11,121,1991…),回文素数就是既是回文数又是素数(只能被1和自身整除的数)的数。编程找出11~9999之间的回文素数。</strong><br></p><pre class="brush:html;toolbar:false">public class PalindromicPrimeNumber {
public static void main(String[] args) {
for(int i = 11; i 0) {
sum= sum * 10 + temp % 10;
temp/= 10;
}
return sum == n;
}
}
4.全排列:给出五个数字12345的所有排列。
public class FullPermutation {
public static void perm(int[] list) {
perm(list,0);
}
private static void perm(int[] list, int k) {
if (k == list.length) {
for (int i = 0; i <p><strong>5.对于一个有N个整数元素的一维数组,找出它的子数组(数组中下标连续的元素组成的数组)之和的最大值。</strong><br></p><p><strong>下面给出几个例子(最大子数组用粗体表示):</strong></p><p>数组:{ 1, -2, 3,5, -3, 2 },结果是:8</p><p>2) 数组:{ 0, -2, 3, 5, -1, 2 },结果是:9</p><p>3) 数组:{ -9, -2,-3, -5, -3 },结果是:-2</p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/xiazai/skill11083" title="Java调试"><img
src="https://img.php.cn/upload/skill/001/246/273/6adaf20dbae7c507.jpg" alt="Java调试" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/xiazai/skill11083" title="Java调试" class="overflowclass">Java调试</a>
<p class="overflowclass">通过 JDWP 实现 Java 远程调试:连接远程 JVM、设置断点、单步执行代码、查看变量值、获取调用栈。适用于需要调试远程 Java 应用、排查运行时状态、追踪代码执行流程的场景。
术语注解
JDWP:Java Debug Wire Protocol,Java 调试有线协议(Java 远程调试底层通信协议)
remote JVM:远程 Java 虚拟机
step through code:代码单步调试(步入 / 步过 / 步出调试)
inspect variables:变量查看 / 变量检视
stac</p>
</div>
<a rel="nofollow" href="/xiazai/skill11083" title="Java调试" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div><p><strong>可以使用动态规划的思想求解:</strong></p><pre class="brush:html;toolbar:false">public class MaxSum {
private static int max(int x, int y) {
return x > y? x: y;
}
public static int maxSum(int[] array) {
int n = array.length;
int[] start = new int[n];
int[] all = new int[n];
all[n - 1] = start[n - 1] = array[n - 1];
for(int i = n - 2; i >= 0;i--) {
start[i] = max(array[i], array[i] + start[i + 1]);
all[i] = max(start[i], all[i + 1]);
}
return all[0];
}
public static void main(String[] args) {
int[] x1 = { 1, -2, 3, 5,-3, 2 };
int[] x2 = { 0, -2, 3, 5,-1, 2 };
int[] x3 = { -9, -2, -3,-5, -3 };
System.out.println(maxSum(x1)); // 8
System.out.println(maxSum(x2)); // 9
System.out.println(maxSum(x3)); //-2
}
}
6.用递归实现字符串倒转
public class StringReverse {
public static String reverse(String originStr) {
if(originStr == null || originStr.length()== 1) {
return originStr;
}
return reverse(originStr.substring(1))+ originStr.charAt(0);
}
public static void main(String[] args) {
System.out.println(reverse("hello"));
}
}
7.输入一个正整数,将其分解为素数的乘积。
public class DecomposeInteger {
private static List<integer> list = new ArrayList<integer>();
public static void main(String[] args) {
System.out.print("请输入一个数: ");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
decomposeNumber(n);
System.out.print(n + " = ");
for(int i = 0; i <p><strong>8、一个有n级的台阶,一次可以走1级、2级或3级,问走完n级台阶有多少种走法。</strong><br></p>
<pre class="brush:html;toolbar:false">public class GoSteps {
public static int countWays(int n) {
if(n <p><strong>9.写一个算法判断一个英文单词的所有字母是否全都不同(不区分大小写)<br></strong></p><pre class="brush:html;toolbar:false">public class AllNotTheSame {
public static boolean judge(String str) {
String temp = str.toLowerCase();
int[] letterCounter = new int[26];
for(int i = 0; i <temp.length> 1) {
return false;
}
}
return true;
}
public static void main(String[] args) {
System.out.println(judge("hello"));
System.out.print(judge("smile"));
}
}</temp.length>
10.有一个已经排好序的整数数组,其中存在重复元素,请将重复元素删除掉,例如,A= [1, 1, 2, 2, 3],处理之后的数组应当为A= [1, 2, 3]。
public class RemoveDuplication {
public static int[] removeDuplicates(int a[]) {
if(a.length <p><strong>11.给一个数组,其中有一个重复元素占半数以上,找出这个元素。</strong><br></p><pre class="brush:html;toolbar:false">public class FindMost {
public static <t> T find(T[] x){
T temp = null;
for(int i = 0, nTimes = 0; i<p><strong>12.编写一个方法求一个字符串的字节长度?</strong><br></p>
<pre class="brush:html;toolbar:false">public int getWordCount(String s){
int length = 0;
for(int i = 0; i = 0 && ascii
Java免费学习笔记:立即使用
解锁 Java 大师之旅:从入门到精通的终极指南










