7-7 无聊的小明来数1 (10 分)
| |
| import java.util.Calendar; |
| import java.util.Random; |
| import java.util.Scanner; |
| |
| public class Main { |
| |
| public static void main(String[] args) { |
| Scanner sc = new Scanner(System.in); |
| |
| int n=sc.nextInt(); |
| for(int i=0;i<n;i++) { |
| int k=sc.nextInt(); |
| String cnt=Integer.toBinaryString(k); |
| int ans=0; |
| for(int j=0;j<cnt.length();j++) { |
| char ch=cnt.charAt(j); |
| if(ch=='1') ans++; |
| } |
| System.out.println(ans); |
| } |
| sc.close(); |
| |
| } |
| } |
| |
| |
7-6 那年有几个黑五? (10 分)
| |
| import java.util.Calendar; |
| import java.util.Random; |
| import java.util.Scanner; |
| |
| public class Main { |
| |
| public static void main(String[] args) { |
| Scanner sc = new Scanner(System.in); |
| String str=sc.nextLine(); |
| int year=Integer.parseInt(str); |
| int sum=0; |
| Calendar ca= Calendar.getInstance(); |
| for(int i=0;i<12;i++) { |
| ca.set(year,i,12); |
| if(ca.get(Calendar.DAY_OF_WEEK)==6) { |
| sum++; |
| } |
| } |
| System.out.println(sum); |
| sc.close(); |
| |
| } |
| } |
| |
| |
7-5 jmu-Java-01入门-取数字浮点数 (10 分)
代码1
| import java.util.Random; |
| import java.util.Scanner; |
| |
| public class Main { |
| |
| public static void main(String[] args) { |
| Scanner sc = new Scanner(System.in); |
| while(sc.hasNext()) { |
| String s=sc.nextLine(); |
| int n=s.length(); |
| char[] a=s.toCharArray(); |
| int ans=0; |
| for(int i=0;i<n;i++) { |
| if(Character.isDigit(a[i])) { |
| |
| |
| ans+=a[i]-'0'; |
| } |
| } |
| System.out.println(ans); |
| } |
| sc.close(); |
| |
| } |
| } |
| |
| |
代码2
| import java.util.Random; |
| import java.util.Scanner; |
| |
| public class Main { |
| |
| public static void main(String[] args) { |
| Scanner sc = new Scanner(System.in); |
| while(sc.hasNext()) { |
| String s=sc.nextLine(); |
| int n=s.length(); |
| char[] a=s.toCharArray(); |
| int ans=0; |
| for(int i=0;i<n;i++) { |
| if('0'<=a[i]&&a[i]<='9') { |
| ans+=a[i]-'0'; |
| } |
| } |
| System.out.println(ans); |
| } |
| sc.close(); |
| |
| } |
| } |
| |
| |
7-4 jmu-java-随机数-使用蒙特卡罗法计算圆周率的值 (10 分)
背吧
| import java.util.Random; |
| import java.util.Scanner; |
| |
| public class Main { |
| |
| public static void main(String[] args) { |
| Scanner sc = new Scanner(System.in); |
| long seed=sc.nextLong(); |
| int n=sc.nextInt(); |
| Random R=new Random(seed); |
| |
| int count=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) { |
| count++; |
| } |
| } |
| System.out.println(4*(double)count/n); |
| |
| |
| sc.close(); |
| |
| } |
| } |
| |
| |
7-3 伪随机数 (10 分)
| import java.util.Random; |
| import java.util.Scanner; |
| |
| public class Main { |
| |
| public static void main(String[] args) { |
| Scanner sc = new Scanner(System.in); |
| int n=sc.nextInt(); |
| int m=sc.nextInt(); |
| int k=sc.nextInt(); |
| Random R=new Random(k) ; |
| |
| |
| |
| |
| |
| int t=-1; |
| for(int i=0;i<n;i++) { |
| t=R.nextInt(m); |
| |
| |
| } |
| System.out.println(t); |
| sc.close(); |
| |
| } |
| } |
| |
| |
(逐步逼近法)7-2 jmu-Java-01入门-开根号 (10 分)
NaN = not a number“不是一个合理的实数”
| import java.util.Scanner; |
| |
| public class Main { |
| |
| public static void main(String[] args) { |
| Scanner sc = new Scanner(System.in); |
| while(sc.hasNext()) { |
| double n=sc.nextDouble(); |
| double tmp=0; |
| if(n<0) { |
| System.out.println(Double.NaN); |
| } |
| else { |
| while(Math.pow(tmp, 2)<n&&Math.abs(n-tmp*tmp)>0.0001) { |
| tmp+=0.0001; |
| } |
| System.out.printf("%.6f\n",tmp); |
| } |
| |
| |
| } |
| sc.close(); |
| |
| } |
| } |
| |
| |
7-1 sdut-常用类-骄傲的代价 (10 分)
| import java.math.BigInteger; |
| import java.util.Arrays; |
| import java.util.Scanner; |
| |
| public class Main { |
| public static void main(String[] args) { |
| Scanner sc = new Scanner(System.in); |
| long t=sc.nextLong(); |
| for(int i=0;i<t;i++) { |
| BigInteger a=sc.nextBigInteger(); |
| BigInteger b=sc.nextBigInteger(); |
| System.out.println(a+"+"+b+"="+a.add(b)); |
| System.out.println(a+"-"+b+"="+a.subtract(b)); |
| System.out.println(a+"*"+b+"="+a.multiply(b)); |
| System.out.println(a+"/"+b+"="+a.divide(b)); |
| System.out.println(a+"%"+b+"="+a.mod(b)); |
| } |
| sc.close(); |
| } |
| } |
| |
| |
本文作者:kingwzun
本文链接:https://www.cnblogs.com/kingwz/p/15573004.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步