【问题描述】所谓“回文数”是指具有如下性质的整数:一个整数,当它的各位数字逆序排列,形成的整数与原整数相同,这样的数称为回文数。例如,素数11,373,其各位数字对换位置后仍然为11,373,因此这两个整数均为回文数。编写程序,接收控制台输入的两个整数a、b,输出a到b之间(包括a和b)的所有回文数
【输入形式】控制台输入两个整数a和b(必有a<b),以空格分隔。
【输出形式】输出有若干行,每行有一个a和b之间的回文数。输出各行上的数字不重复,且从小至大依次按序输出。
【样例输入】3 120
【样例输出】
3
4
5
6
7
8
9
11
22
33
44
55
66
77
88
99
101
111
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num1 = in.nextInt();
int num2 = in.nextInt();
for (int i = num1; i <= num2; i ++) {
if (is_huiwen(i)) {
System.out.println(i);
}
}
}
public static boolean is_huiwen(int num) {
String s = num + "";
int i, j;
for (i = 0, j = s.length() - 1; i < j; i ++, j --) {
if (s.charAt(i) != s.charAt(j)) {
break;
}
}
if (i >= j) return true;
else return false;
}
}
【问题描述】
输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。并打印出来。
【输入形式】
Please input a sentence:
【输出形式】
The number of numbers is:
The number of English letters is:
The number of blank space is:
The number of other characters is:
【输入样例】
This is my 1st JAVA code!
【输出样例】
The number of numbers is: 1
The number of English letters is: 18
The number of blank space is: 5
The number of other characters is: 1
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Please input a sentence:");
String str = in.nextLine();
System.out.println("The number of numbers is: " + numbers_count(str));
System.out.println("The number of English letters is: " + letters_count(str));
System.out.println("The number of blank space is: " + blank_count(str));
System.out.println("The number of other characters is: " + other_count(str));
}
public static int numbers_count(String s) {
int res = 0;
for (int i = 0; i < s.length(); i ++) {
if (s.charAt(i) >= '0' && s.charAt(i) <= '9') res ++;
}
return res;
}
public static int letters_count(String s) {
int res = 0;
for (int i = 0; i < s.length(); i ++) {
if (Character.isLetter(s.charAt(i))) res ++;
}
return res;
}
public static int blank_count(String s) {
int res = 0;
for (int i = 0; i < s.length(); i ++) {
if (Character.isSpaceChar(s.charAt(i))) res ++;
}
return res;
}
public static int other_count(String s) {
int res = 0;
for (int i = 0; i < s.length(); i ++) {
if (!Character.isSpaceChar(s.charAt(i)) && !Character.isDigit(s.charAt(i)) &&
!Character.isLetter(s.charAt(i))) res ++;
}
return res;
}
}
【问题描述】
从键盘输入10个整数,存放在一个数组中,然后使数组中的所有整数整体向后移动m个位置,最后m个数变成最前面的m个数,并输出移动后的结果。m从键盘输入。
【输入形式】
输入10个整数,每个整数间用空格分隔,回车。然后输入整数m。
【输出形式】
首先输出数组中的10个元素,然后输出后移m位以后的数组所有元素。
【输入输出样例】
Please input 10 numbers:
1 2 3 4 5 6 7 8 9 10
Your numbers are:
1 2 3 4 5 6 7 8 9 10
Please input m:
3
The new numbers are:
8 9 10 1 2 3
4 5 6 7
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int m;
int[] num = new int[20];
System.out.println("Please input 10 numbers:");
for (int i = 0; i < 10; i ++) num[i] = in.nextInt();
System.out.println("Your numbers are:");
for (int i = 0; i < 10; i ++) System.out.print(num[i] + " ");
System.out.println("");
System.out.println("Please input m:");
for (int i = 10, j = 0; i < 20; i ++, j ++) {
num[i] = num[j];
}
m = in.nextInt();
System.out.println("The new numbers are:");
for (int i = 10 - m; i < 20 - m; i ++) System.out.print(num[i] + " ");
}
}
【问题描述】输入一个高精度的大正整数S(S最长可达240位),去掉其中任意N位数字后剩下的数字按原次序组成一个新的正整数S'。编程对给定的N和S,寻找一种方案使得剩下的数字组成的新数S'最小。
【输入形式】输入有两行:
1.第一行是大整数S。其中S最长可达240位。
2.第二行是整数N。S、N均以非0数字开头。
【输出形式】输出有一行,是在S中删除N位后所得的最小数字S'。
【样例输入1】
178543
4
【样例输出1】13
【样例输入2】
1002
1
【样例输出2】002
【样例说明】样例1中输入整数S=178543,N=4,要求在178543中删除4位,使剩下的数字最小。正确答案为S'= 13。样例2中输入整数S=1002,N=1,删完一位后S'= 002,而不是2,即2之前的0也必须输出。
【运行时限】程序一次运行的最长时间限制在15秒内,超出则认为程序错误。
【算法提示】将整数看作字符串形式读入;删数时,从前往后寻找第一个比后一个数字大的数字,然后删除之,按照这种方法删除N个数字即得最小数字。
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s;
int a;
int i;
s = in.nextLine();
a = in.nextInt();
while (a != 0) {
for (i = 0; i < s.length() - 1 && s.charAt(i) <= s.charAt(i + 1);){
i++;
}
s=s.substring(0,i)+s.substring(i+1);
a--;
}
System.out.println(s);
}
}