Java小知识
1. 对于给出一个数字求它是个几位数:
(1)int num=****;
String str=String.valueOf(num);
System.out.println(str.length());
for(char c:str.toCharArray()){
System.out.println(c);
}//将整型转换成字符串型,在求出其长度。
(2)int n=****;
Int m=0;
While(n!=0){
n=n/10;
m++;
}
1004母牛的故事
import java.util.Scanner;
public class Main {
public static void main(String []args) {
Scanner input = new Scanner(System.in);
int m;
while (input.hasNext()) {
int n = input.nextInt();
if (n <= 4 && n > 0) {
System.out.println(n);
}
else if (n<=0)
return;
else
System.out.println(an(n));
}
}
public static int an(int i){
if (i==1 || i==2 ||i==3||i==4)
return i;
else
return an(i-1)+an(i-3);//a(n)=a(n-1)+a(n-3)并非直接a(n)=(n-1)+(n-3)类似于这种的可以使用这种方法
}
}
1009数字的处理与判断
import java.util.Scanner;
public class Main1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m=0,t=n;
while (t!=0){
t=t/10;
m++;
}//不能直接使用n,不然使用完之后n变为了0;后面的循环变得无意义
System.out.println(m);
int[]a=new int[m];
if (n!=0){
for(int i=m-1;i>=0;i--){//先用m-1,在--;
a[i]=n%10;
n/=10;
}
}
for (int i=0;i<m;i++)
System.out.print(a[i]+" ");
System.out.println();
for (int i=m-1;i>=0;i--)
System.out.print(a[i]);
}
}
2、求最大公约数
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
int n = sc.nextInt();
// 最大公约数
int min=1;
for(int i = m; i>=1; i--) {
min = i;
if(m%min==0 && n%min==0) {
break;
}
}
System.out.println(min);
// 最小公倍数
System.out.println(m*n/min);//最小公倍数=两数之积/最大公约数
}
}
3、DecimalFormat x=new DecimalFormat("0.000");
System.out.println(x.format(y));//涉及到小数点后几位的使用方法
4、根号Math.sqrt();
5、Java语言String.format("%5.2f",45.556)中哪个5.2表示什么;
5是代表输出的总位数,包括小数点,以及小数点前后的位数,2代表小数点后2位,当实际长度大于5位时,按实际输出,保留2位小数,当位数不够,整数部分会自动在最前面补缺占位。输出结果为45.56.
6、求三个数中的最大的数的方法
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Main {
1. public static void max(float a,float b,float c){
float t=Math.max(Math.max(a,b),c);
String n=String.format("%6.3f",t);
System.out.println(n);
}
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
float x=input.nextFloat(),y=input.nextFloat(),z=input.nextFloat();
DecimalFormat q=new DecimalFormat("0.000");
2. if (x>y){
if (x>z)
System.out.println(q.format(x));
else
System.out.println(q.format(z));
}
else if (y>z)
System.out.println(q.format(y));
else
System.out.println(q.format(z));
3. float t=(x>y)?x:y;
float max=(t>z)?t:z;
System.out.println(q.format(max));
max(x,y,z);
4. Scanner sc=new Scanner(System.in);
ArrayList list=new ArrayList();
for(int i=0;i<3;i++){
list.add(sc.nextDouble());
}
String s1=String.format("%.3f", Collections.max(list));
System.out.println(s1);
System.out.println(s1);
}
}
7、https://m.jb51.net/article/84821.htm关于Collection
https://blog.csdn.net/a1439775520/article/details/95373159
8、排序的方法
Arrays.sort(a);//从小到大排序,a为一个数组
0.0分
0 人评分
高精度加法 (C++代码)(大数加法)浏览:913 |
2004年秋浙江省计算机等级考试二级C 编程题(1) (C语言代码)浏览:456 |
2005年春浙江省计算机等级考试二级C 编程题(2) (C语言代码)浏览:490 |
C语言训练-数字母 (C语言代码)浏览:582 |
剪刀石头布 (C语言代码)不知道怎么直接在scanf中用枚举变量浏览:1301 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:547 |
C语言程序设计教程(第三版)课后习题3.7 (C语言代码)浏览:840 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:667 |
母牛的故事 (C语言代码)浏览:1426 |
1118(求助_已解决)浏览:329 |