https://zhuanlan.zhihu.com/p/21454718?refer=passer
某互上看到的一个java相关的东西,突然发现自己后半个学期几乎没写过代码了,心痛。语法熟悉程度也在下降。
随便写写里面的题目,当做增加下语法熟悉度吧……
【程序6】
题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
package test;
import java.util.ArrayList;
import java.util.Scanner;
/**
* 两个数字的最小公倍数和最大公约数
* @author Xgl
*
*/
public class Test {
public static void main (String[] args){
System.out.println("此程序用于判断两个数字的最小公倍数和最大公约数φ(゜▽゜*)♪\n少年 请输入该两个数字,逗号分开即可");
Scanner input = new Scanner(System.in);
String in = input.next();
input.close();
String[] numS = new String[2];
numS = in.split(",",2);
int [] num = new int[2];
num[0]= Integer.parseInt(numS[0]);
num[1]= Integer.parseInt(numS[1]);//数组num中保存该两个数字
ArrayList l = new ArrayList();
a:while(true)
{
int min;//两个数的最小值,用于控制除数的上界
//三判断
if(num[0]>num[1]){min = num[1];}else{if(num[0]<num[1]){min = num[0];}else{l.add(num[0]);break a;}}
boolean ablity=true;//是否可以继续除法
b:for (int i = 2; i <= min; i++)
{
Test t = new Test();
if(num[0]/i==0 & num[1]/i==0)
{
l.add(i);
num[0]= num[0]/i;
num[1]= num[1]/i;
break b;//代表一次除式完成,应开始下一轮
}else{
ablity = false;
}
}
if(num[0]==1 | num[1]==1 | ablity==false)
{
l.add(num[0]);
l.add(num[1]);
break a;
}//得数如果是1,代表已经完成了分解步骤
}
int big=1;//最大公因数
int small=1;//最小公倍数
for (Object j : l) {
small = small * (int)j;
if((int)j > big){big=(int)j;}
}
System.out.println("最大公因数是:"+big);
System.out.println("最小公倍数是:"+small);
System.out.println("over");
}
}
【程序7】
题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
package test;
import java.util.ArrayList;
import java.util.Scanner;
/**
* 题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
* @author Xgl
*
*/
public class test {
int nums = 0;
int engs = 0;
int spaces = 0;
int others = 0;
int chns = 0;
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("φ(゜▽゜*)♪突然兴奋\n请输入欲统计的字符串:");
Scanner input = new Scanner(System.in);
String str = input.nextLine();
input.close();
ArrayList<Character> list = new ArrayList<Character>();
for (int i = 0; i < str.length(); i++) {
list.add(str.charAt(i));
}
System.out.println(list);
test t = new test();
for (char obj : list) {
t.f(obj);
}
System.out.println("数字有:"+ t.nums + "\n英文字母有:"+ t.engs+"\n空格有:"+ t.spaces+"\n中文字符有:"+ t.chns+"\n其他字符有:"+ t.others);
}
//统计英语字母、空格、数字、中文。
public int f(char j){
char [] a ={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
for (char str : a) {
if(str==j){
engs++;
return 0;
}
}
String b = String.valueOf(j);
if(b.equals(" ")){
spaces++;
return 0;
}
if((j >= 0x4e00)&&(j <= 0x9fbb)){
chns++;
return 0;
}
try{
@SuppressWarnings("unused")
int v = Integer.parseInt(String.valueOf(j));
nums++;
return 0;
}catch(Exception d){}
others++;
return 0;
}
}
【程序8】
题目:求s=a+aa+aaa+aaaa+aa…a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),
几个数相加有键盘控制。
package demo;
import java.util.Scanner;
//题目:求s=a+aa+aaa+aaaa+aa…a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),
//几个数相加有键盘控制。
public class demo {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print("φ(゜▽゜*)♪突然兴奋\n请输入a的值:");
Scanner input = new Scanner(System.in);
int a = input.nextInt();
System.out.print("然后然后 输入几个数相加:");
int times = input.nextInt();
input.close();
double out = 0;
for (int i = 1; i <= times; i++) {
double temp=(Math.pow(10, i)-1)/9;
out=out+a*temp;
}
System.out.println(out);
}
}
【程序9】
题目:一个数如果恰好等于它的因子之和,这个数就称为”完数”。例如6=1+2+3.编程 找出1000以内的所有完
数。
package demo;
import java.util.ArrayList;
public class demo {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Integer> f = new ArrayList<>();
for (int i = 1; i < 1000; i++) {
demo d = new demo();
if(d.Judge(i)){
f.add(i);
}
}
for (Integer integer : f) {
System.out.println(integer);
}
System.out.println("over");
}
public boolean Judge(int a){
ArrayList<Integer> test = new ArrayList<>();
for (int i = 1; i < a; i++) {
if(a%i==0){
test.add(i);
}
}
int sum =0;
for (Integer integer : test) {
sum = sum + integer;
if(sum==a){
return true;
}
}
return false;
}
}
【程序10】
题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在 第10次落地时,共经过多
少米?第10次反弹多高?
public class demo {
public static void main(String[] args) {
// TODO Auto-generated method stub
double sum = 100;
double last = 0;
for (int i = 1; i < 10; i++) {
double temp = 100/Math.pow(2, i);
sum = sum + temp*2;
if(i==9){last=temp;}
}
System.out.println("第10次落地时,共经过"+sum);
System.out.println(last);
}
}
【程序11】
题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
import java.util.ArrayList;
public class demo {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Integer> list = new ArrayList<>();
list.add(1);list.add(2);list.add(3);list.add(4);
// System.out.println(list);
int want = 0;
int ttemp=0;
int tttemp=0;
for (int i = 0; i < 4; i++) {
want = 0;
want = want + list.get(i)*100;
int temp = list.get(i);
list.remove(list.get(i));
for (int j = 0; j < 3; j++) {
if(j==0){
ttemp = want;
}
want = ttemp;
want = want + list.get(j)*10;
int temp_2 = list.get(j);
list.remove(list.get(j));
for (int m = 0; m < 2; m++) {
if(m==0){
tttemp = want;
}
want=tttemp;
want = want + list.get(m);
System.out.println(want);
}
list.add(j,temp_2);
}
list.add(i,temp);
}
System.out.println("over");
}
}
【程序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,求应发放奖金总数?
几个if解决,不写了………………
【程序13】
题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
public class test {
public static void main(String[] args) {
int i=-100;
a:while(true){
i++;
test t = new test();
if(t.Judge(i+100) & t.Judge(i+168)){
System.out.println(i);
break a;
}
}
System.out.println("over");
}
//判断是否为完全平方数
public boolean Judge(int a){
double temp = Math.pow(a, 0.5);
if(temp==(int)temp){
return true;
}
return false;
}
}
【程序14】
题目:输入某年某月某日,判断这一天是这一年的第几天?
public class test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("输入当前年月日,格式为 ****/**/**");
String str = input.nextLine();
input.close();
String [] strs = str.split("/");
boolean ruinian = false;
int total=0;
if(Integer.valueOf(strs[0])%4==0){
if(Integer.valueOf(strs[0])%100==0 & Integer.valueOf(strs[0])%400==0){ruinian=true;}
if(Integer.valueOf(strs[0])%100!=0){ruinian=true;}
}
int [] days = {31,28,31,30,31,30,31,31,30,31,30,31};
for (int i = 0; i < Integer.valueOf(strs[1])-1; i++) {
total = total + days[i];
}
total= total + Integer.valueOf(strs[2]);
if(ruinian & total>=60){total++;}
System.out.println("这是该年第"+total+"天");
}
}
【程序15】
题目:输入三个整数x,y,z,请把这三个数由小到大输出。
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("请分次输入三个数字:");
final int a = input.nextInt();int b = input.nextInt();int c = input.nextInt();
input.close();
int max =a>b?(b<c?b:c):(a<c?a:c);
switch(max==a?1:(max==b?2:3)){
case 1:if(b>c){System.out.println(a+"\n"+b+"\n"+c);}else{System.out.println(a+"\n"+c+"\n"+b);break;}
case 2:if(a>c){System.out.println(b+"\n"+a+"\n"+c);}else{System.out.println(b+"\n"+c+"\n"+a);break;}
case 3:if(a>b){System.out.println(c+"\n"+a+"\n"+b);}else{System.out.println(c+"\n"+b+"\n"+a);break;}
}
}
}
这里感觉蛮奇怪的,case后面只能加常量,感觉这地方可以进行改进啊,学习下人Python的elif!!!
【程序16】
题目:输出9*9口诀。
public class test {
public static void main(String[] args) {
for (int i = 1; i < 10; i++) {
for (int j = 1; j <= i; j++) {
if(j==1){System.out.print("\n"+j+"*"+i+"="+i*j);;continue;}
System.out.print(" "+j+"*"+i+"="+i*j);
}
}
}
}
——————以上为1.8日工作——————————————————
【程序17】
题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个 第二天早上又将剩
下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下
的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。
public class test {
public static void main(String[] args) {
int num = 1;
for (int day = 9; day >= 1 ; day--) {
num = (num+1)*2;
}
System.out.println("第一天采摘了:"+num);
}
}
【程序18】
题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单。
先排列组合,只要排一个队伍就行,再和另一队对应,再遍历删除不行的,适用性不强,类似前面的4个数字排列组合。
【程序19】
题目:打印出如下图案(菱形)
*
- *
public class demo {
public static void main(String[] args) {
int width = 2*4-1;
int mid = width/2+1;
//print 2*4-1 lines
for (int i = 1; i <= 4; i++) {
int star = 2*i-1;
for (int j = 1; j <= width; j++) {
if((mid+1-i) <=j & j<= (mid-1+i)){
System.out.print("*");
}else{System.out.print(" ");}
if(j==width){System.out.println("\n");}
}
}
for (int i = 4-1; i >0 ; i--) {
int star = 2*i-1;
for (int j = 1; j <= width; j++) {
if((mid+1-i) <=j & j<= (mid-1+i)){
System.out.print("*");
}else{System.out.print(" ");}
if(j==width){System.out.println("\n");}
}
}
}
}
【程序20】
题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13…求出这个数列的前20项之和。
public class demo {
public static void main(String[] args) {
int i = 2;
int j = 1;
int sum = 0;
while(i!=21){
sum = sum +i/j;
int temp = i;
i = i+j;
j = temp;
}
System.out.println(sum);
}
}
【程序21】
题目:求1+2!+3!+…+20!的和
public class demo {
public static void main(String[] args) {
int sum=0;
for (int i = 1; i <= 20; i++) {
sum = sum + (int) Math.pow(i, 2);
}
System.out.println(sum);
}
}
——-1.9—1:31分–
【程序22】
题目:利用递归方法求5!。
public static void main(String[] args) {
demo d = new demo();
System.out.println(d.digui(5));
}
public int digui(int i){
if(i==1){
return 1;
}
demo d = new demo();
i = i * d.digui(i-1);
return i;
}
}
【程序23】
题目:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第3个人大2岁。问
第三个人,又说比第2人大两岁。问第2个人,说比第一个人大两岁。最后问第一个人,他说是10岁。请问第五个
人多大?
public class example23 {
public static void main(String[] args) {
example23 example = new example23();
System.out.println(example.age(5));
}
//int i is the man's num
//return his age
public int age(int i){
if(i==1){
return 10;
}
example23 example = new example23();
return example.age(i-1)+2;
}
}
【程序24】
题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。
public class example24 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("please input your number:");
int in = input.nextInt();
input.close();
//int[] a stand for each num in the position of the input num
int[] a = new int[5];
a[0] = in/10000;
a[1] = in/1000-in/10000*10;
a[2] = in/100-in/1000*10;
a[3] = in/10-in/100*10;
a[4] = in-in/10*10;
int weishu=5;
for (int i = 0; i < a.length; i++) {
if(a[i]==0){
weishu = weishu - 1;
}
}
System.out.println("该数字的位数为"+weishu+"(这怎么翻译...)");
System.out.print("print in diversion:");
for (int i = a.length-1; i >=0 ; i--) {
if(a[i]==0){
continue;
}
System.out.print(a[i]);
}
}
}
【程序25】
题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。
public class example25 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Please input your number:");
int in = input.nextInt();
input.close();
//int[] a stand for each num in the position of the input num
int[] a = new int[5];
a[0] = in/10000;
a[1] = in/1000-in/10000*10;
a[2] = in/100-in/1000*10;
a[3] = in/10-in/100*10;
a[4] = in-in/10*10;
if(a[0]==a[4] & a[1]==a[3]){
System.out.println("This is a 回文数");
}else{
System.out.println("This is not a 回文数");
}
}
}
【程序26】
题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续 判断第二个字母。
拆分字符串,多判断
【程序27】
题目:求100之内的素数
public class example27 {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(2);
System.out.println(2);
for (int i = 3; i <= 100; i++) {
// bool 判断是否为合数
boolean bool = false;
for (int j : list) {
if(i%j==0){
bool = true;
}
}
if(bool==false){
list.add(i);
System.out.println(i);
}
}
}
}
【程序28】
题目:对10个数进行排序
冒泡、插入等等经典排序算法均可
【程序29】
题目:求一个3*3矩阵对角线元素之和
二维数组
【程序30】
题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。
遍历 判断(插入排序)
【程序31】
题目:将一个数组逆序输出。
用一个临时变量,i--
【程序32】
题目:取一个整数a从右端开始的4~7位。
拆分 再获得
【程序33】
题目:打印出杨辉三角形(要求打印出10行如下图)
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
【程序34】
题目:输入3个数a,b,c,按大小顺序输出。
程序15
【程序35】
题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
遍历 临时变量储存最值 比较判断
【程序36】
题目:有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数
声明新的数组
【程序37】
题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下
的是原来第几号的那位。
import java.util.ArrayList;
import java.util.Scanner;
public class example37 {
public static void main(String[] args) {
System.out.println("Please input the value of 'n':");
Scanner input = new Scanner(System.in);
int n = input.nextInt();
input.close();
ArrayList<Integer> people = new ArrayList<Integer>();
for (int i = 1; i <= n; i++) {
people.add(i);
}
int index = 0;
int count = 1;
while(index<people.size()){
//when size of people is too low, break
if(people.size()<3){
break;
}
//remove the multiple of three
if(count%3==0){
people.remove(index);
index--;
}
count++;
if(count==4){
count=1;
}
index++;
//when index is out of the bound, reset to 0
if(index==people.size()){
index=0;
}
}
for (Integer integer : people) {
System.out.println(integer);
}
}
}
这个程序里使用index和count的分离解决掉数数问题……卡了有点久啊啊
【程序38】
题目:写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度。
String.length();
【程序39】
题目:编写一个函数,输入n为偶数时,调用函数求1/2+1/4+…+1/n,当输入n为奇数时,调用函数
1/1+1/3+…+1/n(利用指针函数)
java没有指针函数吧………………
【程序40】
题目:字符串排序。
String.charAt(index);
【程序41】
题目:海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子凭据分为五份,多了一个,这只猴子把多的一
个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中
,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?
前面有类似的,不表
【程序42】
题目:809*??=800*??+9*??+1
其中??代表的两位数,8*??的结果为两位数,9*??的结果为3位数。求??代表的两位数,及809*??后的结果。
没看懂题目
【程序43】
题目:求0—7所能组成的奇数个数。
最后一位确定即可(直接翻倍)
【程序44】
题目:一个偶数总能表示为两个素数之和。
2行吗???不然负数呢,题设有问题
在一半的范围内遍历数字,是素数就减掉测差是否为素数
【程序45】
题目:判断一个素数能被几个9整除
wtf
【程序46】
题目:两个字符串连接程序
s3 = s1 + s2;
【程序47】
题目:读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*。 for
【程序48】
题目:某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字
都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。
【程序49】
题目:计算字符串中子串出现的次数
import java.util.Scanner;
public class example42 {
public static void main(String[] args) {
System.out.print("Please input your string:");
Scanner input = new Scanner(System.in);
String str = input.nextLine();
System.out.print("Please input your son substring:");
String substr = input.nextLine();
input.close();
String [] strs = str.split("");
String [] substrs = substr.split("");
int times = 0;
for (int i = 0; i < strs.length; i++) {
if(substrs[0].equals(strs[i])){
boolean bool = true;
for (int j = 0; j < substrs.length; j++) {
try {
if(substrs[j].equals(strs[i+j])==false){
bool = false;
}
} catch (Exception e) {}
}
if(bool==true){
times=times+1;
}
}
}
System.out.printf("substring appear %d times",times);
}
}
【程序50】
题目:有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,况原有的数据和计算出的平均分数存放在磁盘文件”stud”中。
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;
public class example50 {
public static void main(String[] args) {
File file = new File("stud.txt");
if(file.exists()){
System.out.println("文件已存在");
}
else{
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("新空白文件已创建");
}
String str = example50.in();
try {
FileOutputStream fileOutputStream = new FileOutputStream(file,true);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "GBK");
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
bufferedWriter.write(str);
bufferedWriter.close();
outputStreamWriter.close();
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static String in(){
Scanner input = new Scanner(System.in);
System.out.print("请输入学生的学生号:");
String num = input.nextLine();
System.out.print("请输入学生的姓名:");
String name = input.nextLine();
System.out.print("请依次输入学生的三门课成绩:");
int score1 = input.nextInt();
int score2 = input.nextInt();
int score3 = input.nextInt();
input.close();
Float scoreAve = (score1+score2+score3)/3f;
String str = String.format("studentNum:%s name:%s aveScore:%.2f\n",,num,name,scoreAve);
return str;
}
}