日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

第一天蓝桥杯备战

發布時間:2024/1/8 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 第一天蓝桥杯备战 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

作為一個算法渣渣,入門小白開始了第一天刷題之路,(~ ̄▽ ̄)~

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();}}} }

總結

以上是生活随笔為你收集整理的第一天蓝桥杯备战的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。