7-1 sdut-常用类-骄傲的代价
E_star由于在上次考试中取得了很好的成绩他开始骄傲起来,此时von看不下去了,于是他把E_star叫来说,最近一道A+B编程题目不会,想让E_star来帮他解答,E_star二话没说结一口答应了,等到von把题目发给E_star的时候他傻眼了。这下,知道骄傲的后果了吧。
JAVA语言实现提示:可使用JDK标准API中的类:java.math.BigInteger。
输入格式:
题目有多组数据,处理到文件结束。输入的第一行包含一个数T,代表测试组数;
接下来有T行,每行有两个整数A和B,中间用空格分隔。
提示:整数可能超出long类型的取值范围。B不为0。
输出格式:
对于每一组数据,输出5行数据,分别为2个数的和、差、积、商(整除)、余数。形式为:
A+B=C,其中C是A与B的加和。
A-B=D,其中D是A与B的差。
A*B=E,其中E是A与B的乘积。
A/B=F,其中F是A与B的商(整数)。
A%B=G,其中G是A除以B的余数。
输入样例:
3
1 2
24 6
12313131231231232131 31232131315465436657434321
输出样例:
1+2=3
1-2=-1
1*2=2
1/2=0
1%2=1
24+6=30
24-6=18
24*6=144
24/6=4
24%6=0
12313131231231232131+31232131315465436657434321=31232143628596667888666452
12313131231231232131-31232131315465436657434321=-31232119002334205426202190
12313131231231232131*31232131315465436657434321=384565331518372453687453692411523206037368051
12313131231231232131/31232131315465436657434321=0
12313131231231232131%31232131315465436657434321=12313131231231232131
import java.util.Scanner;
import java.math.BigInteger;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int t = cin.nextInt();
while(t -- != 0)
{
BigInteger a, b, c;
a = cin.nextBigInteger();
b = cin.nextBigInteger();
c = a.add(b);
System.out.println(a+"+"+b+"="+c);
c = a.subtract(b);
System.out.println(a+"-"+b+"="+c);
c = a.multiply(b);
System.out.println(a+"*"+b+"="+c);
c = a.divide(b);
System.out.println(a+"/"+b+"="+c);
c = a.mod(b);
System.out.println(a+"%"+b+"="+c);
}
}
}
7-2 jmu-Java-01入门-开根号
import java.util.Scanner;
import java.math.BigInteger;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
while(cin.hasNext())
{
double n = cin.nextDouble();
double a = 0;
if(n < 0)
System.out.println(Double.NaN);
else
{
while((a*a<n)&&(Math.abs(n - a*a) > 0.0001))
a += 0.0001;
System.out.printf("%.6f\n", a);
}
}
}
}
7-3 伪随机数
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
int m = cin.nextInt();
int k = cin.nextInt();
List<Integer> s = new ArrayList<Integer>();
Random rand = new Random(k);
for(int i = 0; i < n; i ++)
{
s.add(rand.nextInt(m));
if(i == n - 1)
System.out.println(s.get(i));
}
}
}
7-4 jmu-java-随机数-使用蒙特卡罗法计算圆周率的值
import java.lang.reflect.Method;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
long seed = cin.nextLong();
int n = cin.nextInt();
Random r = new Random(seed);
int cnt = 0;
for(int i = 0; i < n; i ++)
{
double x = r.nextDouble()*2 - 1;
double y = r.nextDouble()*2 - 1;
if(Math.pow(x, 2) + Math.pow(y, 2) <= 1)
{
cnt ++;
}
}
System.out.println(4*(double)cnt/n);
}
}
7-5 jmu-Java-01入门-取数字浮点数
import java.lang.reflect.Method;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
while(cin.hasNext())
{
String s = cin.nextLine();
int sum = 0;
for(int i = 0; i < s.length(); i ++)
{
if(s.charAt(i) >= '0' && s.charAt(i) <= '9')
sum += s.charAt(i) - '0';
}
System.out.println(sum);
}
}
}
7-6 那年有几个黑五?
import java.lang.reflect.Method;
import java.util.Calendar;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
Calendar cal = Calendar.getInstance();
int a = cin.nextInt();
int cnt = 0;
if(a >= 1998)
{
for(int i = 0; i < 11; i ++)
{
cal.set(Calendar.YEAR, a);
cal.set(Calendar.MONTH, i);
cal.set(Calendar.DATE,13);
int t = cal.get(Calendar.DAY_OF_WEEK);
if(t == 6)
{
cnt ++;
}
}
}
System.out.println(cnt);
}
}
7-7 无聊的小明来数1
import java.lang.reflect.Method;
import java.util.Calendar;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int t = cin.nextInt();
while(t -- != 0)
{
int n = cin.nextInt();
int ans = 0;
while(n > 0)
{
if(n%2 == 1) ans ++;
n /= 2;
}
System.out.println(ans);
}
}
}