作为一个算法渣渣,入门小白开始了第一天刷题之路,(~ ̄▽ ̄)~
1.
package com.wyc.cxsj;
import java.text.DecimalFormat;
import java.util.Scanner;
/**
* 输入一个华氏温度,要求输出摄氏温度。公式为 c=5(F-32)/9,取位2小数。
* @author chen
*
*/
public class Main {
public static void main(String []args) {
Scanner scanner = new Scanner(System.in);
int F= scanner.nextInt();
double c = (double) (5*(F-32)/9);
DecimalFormat df = new DecimalFormat(".00");
System.out.println(df.format(c));
}
}
说明:DecimalFormat 是 NumberFormat 的一个具体子类,用于格式化十进制数字。
DecimalFormat 包含一个模式 和一组符号.
0 一个数字 #:不包括0的数字
DecimalFormat df = new DecimalFormat(".00"); 保留两位.
2
package com.wyc.cxsj;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
*给出一个不多于5位的整数,要求 1、求出它是几位数 2、分别输出每一位数字
* 3、按逆序输出各位数字,例如原数为321,应输出123
* @author chen
*
*/
public class Main {
public static void main(String []args) {
int count = 0;
List shuzi = new ArrayList();
Scanner scanner = new Scanner(System.in);
int c = scanner.nextInt();
if(c<=10000&&c>=-10000) {
for(int i = 10;c !=0 ;c = c/i) {
shuzi.add(c%i);
}
System.out.println(shuzi.size());
for(int i = shuzi.size()-1;i>=0;i--) {
System.out.println(shuzi.get(i));
}
for(Object a:shuzi) {
System.out.println(a);
}
}
}
}
过程比较繁琐,来自学渣的叹息…
下面是标准解法:
Scanner s=new Scanner(System.in);
String num=s.next();
int a=num.length();
System.out.println(a);
for(int i=0;i<a;i++) { char c=num.charAt(i); System.out.print(c); System.out.print(" "); }
System.out.println();
for(int j=num.length()-1;j>=0;j--) {
char b=num.charAt(j);
System.out.print(b);
运用字符串可以轻易解决问题,因为题目中没有要求类型.
3.
/**
输入两个正整数m和n,求其最大公约数和最小公倍数。
* @author chen
*
*/
public class Main {
//递归思想
public static int gongyue(int a,int b) {
if(a<b) {
int t = a;
a = b;
b = t;
}
if(a%b != 0) {
return gongyue(b,a%b); //注意是用b/(a%b)
}
return b;
}
public static int gongbei(int a,int b) {
//运用公式法求 a*b/[a,b]
int c = gongyue( a, b) ;
int gongbei = a*b/c;
return gongbei;
}
public static void main(String []args) {
Scanner s=new Scanner(System.in);
int a = s.nextInt();
int b = s.nextInt();
System.out.println(gongyue(a,b));
System.out.println(gongbei(a,b));
}
}
4
package com.wyc.cxsj
import java.util.Scanner;
/**
* 求Sn=1!+2!+3!+4!+5!+…+n!之值,其中n是一个数字(n不超过20)。
* @param args
*/
public class Main {
//一定要记得使用long类型
// 基本类型:int 二进制位数:32
// 包装类:java.lang.Integer
// 最小值:Integer.MIN_VALUE= -2147483648 (-2的31次方) 接近2*10的9次方
// 最大值:Integer.MAX_VALUE= 2147483647 (2的31次方-1)
// 3、
// 基本类型:long 二进制位数:64
// 包装类:java.lang.Long
// 最小值:Long.MIN_VALUE=-9223372036854775808 (-2的63次方) 9*10的18次方
// 最大值:Long.MAX_VALUE=9223372036854775807 (2的63次方-1
public static long jiecheng(int n) {
if(n == 1) {
return 1;
}
return n*jiecheng(n-1);
}
public static void main(String [] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
long sum = 0;
if(n<=20) {
for(int i = n;i>0;i--) {
long c = jiecheng(i);
sum = sum +c;
}
System.out.println(sum);
}
}
}
有坑: 这种阶乘都会很大,用int类型不行,要用long
5
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* 一个数如果恰好等于不包含它本身所有因子之和,这个数就称为"完数"。
* 例如,6的因子为1、2、3,而6=1+2+3,因此6是"完数"。 编程序找出N之内的所有完数,
* 并按下面格式输出其因子
* 6 its factors are 1 2 3
28 its factors are 1 2 4 7 14
496 its factors are 1 2 4 8 16 31 62 124 248
* @param args
*/
public class Main {
List list = new ArrayList();
public boolean wanshu(int n) {
int sum = 0;
//请注意这里是不包含他本身 所有因子之和
for(int i = 1;i<n;i++) {
if(n%i==0) {
sum = sum+i;
list.add(i);
}
}
if(sum ==n ) {
return true;
}
return false;
}
public static void main(String [] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
Main mian = new Main();
for(int j= 1;j<=n;j++) {
if(mian.wanshu(j)) {
System.out.print(j+" its factors are"+" ");
for(Object i:mian.list) {
System.out.print(i+" ");
}
System.out.println();
}
mian.list.clear();
}
}
}
这里我使用的是集合类,好像用数组更好一点,这是用数组的结果
package com.wyc.cxsj;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* 一个数如果恰好等于不包含它本身所有因子之和,这个数就称为"完数"。
* 例如,6的因子为1、2、3,而6=1+2+3,因此6是"完数"。 编程序找出N之内的所有完数,
* 并按下面格式输出其因子
* 6 its factors are 1 2 3
28 its factors are 1 2 4 7 14
496 its factors are 1 2 4 8 16 31 62 124 248
* @param args
*/
public class Main {
public static void main(String [] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
for(int j = 1;j<=n;j++) {
int[] a = new int [j];
int index = 0;
int sum = 0;
for(int i = 1;i<j;i++) {
if(j%i == 0) {
sum = sum+i;
a[index] = i;
index++;
}
}
if(sum == j ) {
System.out.print(j+" its factors are ");
for(int i = 0;i<index;i++) {
System.out.print(a[i]+" ");
}
System.out.println();
}
}
}
}