System.out.println(“请输入一行字符:”);
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();
scan.close();
count(str);
}
//统计输入的字符
private static void count(String str){
List list = new ArrayList();
char[] array_Char = str.toCharArray();
for(char c:array_Char)
list.add(String.valueOf©);//将字符作为字符串添加到list表中
Collections.sort(list);//排序
for(String s:list){
int begin = list.indexOf(s);
int end = list.lastIndexOf(s);
//索引结束统计字符数
if(list.get(end)==s)
System.out.println(“字符‘”+s+“’有”+(end-begin+1)+“个”);
}
}
}
【程序8】
=====
题目:求s=a+aa+aaa+aaaa+aa…a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。
程序分析:关键是计算出每一项的值。
import java.util.Scanner;
public class Prog8{
public static void main(String[] args){
System.out.print(“求s=a+aa+aaa+aaaa+…的值,请输入a的值:”);
Scanner scan = new Scanner(System.in).useDelimiter(“\s*”);//以空格作为分隔符
int a = scan.nextInt();
int n = scan.nextInt();
scan.close();//关闭扫描器
System.out.println(expressed(2,5)+add(2,5));
}
//求和表达式
private static String expressed(int a,int n){
StringBuffer sb = new StringBuffer();
StringBuffer subSB = new StringBuffer();
for(int i=1;i<n+1;i++){
subSB = subSB.append(a);
sb = sb.append(subSB);
if(i<n)
sb = sb.append(“+”);
}
sb.append(“=”);
return sb.toString();
}
//求和
private static long add(int a,int n){
long sum = 0;
long subSUM = 0;
for(int i=1;i<n+1;i++){
subSUM = subSUM*10+a;
sum = sum+subSUM;
}
return sum;
}
}
【程序9】
=====
题目:一个数如果恰好等于它的因子之和,这个数就称为"完数"。例如6=1+2+3.编程找出1000以内的所有完数。
public class Prog9{
public static void main(String[] args){
int n = 10000;
compNumber(n);
}
//求完数
private static void compNumber(int n){
int count = 0;
System.out.println(n+“以内的完数:”);
for(int i=1;i<n+1;i++){
int sum = 0;
for(int j=1;j<i/2+1;j++){
if((i%j)==0){
sum += j;
if(sum==i){
System.out.print(i+" ");
if((count++)%5==0)
System.out.println();
}
}
}
}
}
}
【程序10】
======
题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在 第10次落地时,共经过多少米?第10次反弹多高?
import java.util.Scanner;
public class Prog10{
public static void main(String[] args){
System.out.print(“请输入小球落地时的高度和求解的次数:”);
Scanner scan = new Scanner(System.in).useDelimiter(“\s”);
int h = scan.nextInt();
int n = scan.nextInt();
scan.close();
distance(h,n);
}
//小球从h高度落下,经n次反弹后经过的距离和反弹的高度
private static void distance(int h,int n){
double length = 0;
for(int i=0;i<n;i++){
length += h;
h /=2.0 ;
}
System.out.println(“经过第”+n+“次反弹后,小球共经过”+length+“米,”+“第”+n+“次反弹高度为”+h+“米”);
}
}
【程序11】
======
题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去 掉不满足条件的排列。
public class Prog11{
public static void main(String[] args){
int count = 0;
int n = 0;
for(int i=1;i<5;i++){
for(int j=1;j<5;j++){
if(j==i)
continue;
for(int k=1;k<5;k++){
if(k!=i && k!=j){
n = i100+j10+k;
System.out.print(n+" ");
if((++count)%5==0)
System.out.println();
}
}
}
}
System.out.println();
System.out.println(“符合条件的数共:”+count+“个”);
}
}
【程序12】
======
题目:企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数?
程序分析:请利用数轴来分界,定位。注意定义时需把奖金定义成长整型。
import java.io.*;
public class Prog12{
public static void main(String[] args){
System.out.print(“请输入当前利润:”);
long profit = Long.parseLong(key_Input());
System.out.println(“应发奖金:”+bonus(profit));
}
//接受从键盘输入的内容
private static String key_Input(){
String str = null;
BufferedReader bufIn = new BufferedReader(new InputStreamReader(System.in));
try{
str = bufIn.readLine();
}catch(IOException e){
e.printStackTrace();
}finally{
try{
bufIn.close();
}catch(IOException e){
e.printStackTrace();
}
}
return str;
}
//计算奖金
private static long bonus(long profit){
long prize = 0;
long profit_sub = profit;
if(profit>1000000){
profit = profit_sub-1000000;
profit_sub = 1000000;
prize += profit*0.01;
}
if(profit>600000){
profit = profit_sub-600000;
profit_sub = 600000;
prize += profit*0.015;
}
if(profit>400000){
profit = profit_sub-400000;
profit_sub = 400000;
prize += profit*0.03;
}
if(profit>200000){
profit = profit_sub-200000;
profit_sub = 200000;
prize += prize*0.05;
}
if(profit>100000){
profit = profit_sub-100000;
profit_sub = 100000;
prize += profit*0.075;
}
prize += profit_sub*0.1;
return prize;
}
}
【程序13】
======
题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
程序分析:在10万以内判断,先将该数加上100后再开方,再将该数加上268后再开方,如果开方后的结果满足如下条件,即是结果。
public class Prog13{
public static void main(String[] args){
int n=0;
for(int i=0;i<100001;i++){
if(isCompSqrt(i+100) && isCompSqrt(i+268)){
n = i;
break;
}
}
System.out.println(“所求的数是:”+n);
}
//判断完全平方数
private static boolean isCompSqrt(int n){
boolean isComp = false;
for(int i=1;i<Math.sqrt(n)+1;i++){
if(n==Math.pow(i,2)){
isComp = true;
break;
}
}
return isComp;
}
}
【程序14】
======
题目ÿ