一行,包含一个数N。
一行,包含一个数,表示最少收到的硬币数。
200
17
花200,需要找零824块,找12个64元硬币,3个16元硬币,2个4元硬币即可。
对于100%的数据,。
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = 1024 - in.nextInt(); int[] g = {1, 4, 16, 64}; int[] dp = new int[t + 1]; Arrays.fill(dp, Integer.MAX_VALUE / 2); dp[0] = 0; for (int gg: g) { for (int j = gg; j <= t; ++j) { dp[j] = Math.min(dp[j], dp[j - gg] + 1); } } System.out.println(dp[t]); } }
//来提供点有趣的思路,用位运算 public class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); n = 1024 - n; int ans = 0; int k = 3; while (k-- > 0) { int temp = n & 3; ans +=temp; n = n>>2; } ans += n; System.out.println(ans); } }
import java.util.Scanner; //java.util为包名,Scanner为类名 public class Main { public static void main(String[] args) // 切莫少了传入参数 { Scanner input = new Scanner( System.in ); //使用前先导入Scanner类 int N = input.nextInt(); //next() 为方法 input.close(); int Z = 1024-N; int n64 = Z/64; int S = Z-64*n64; // *不能少,和数学里的带分数的单项式不同 int n16 = S/16; S = S-16*n16; //切莫重复定义,应用之前剩下的来减 int n4 = S/4; S = S-4*n4; int NS = n64+n16+n4+S; System.out.println( NS ); //输出语句切莫和C语言搞混 } }
public static void main(String[] args) throws IOException { Scanner s=new Scanner(System.in); Integer n=s.nextInt(); Integer money=s.nextInt(); int balance=n-money; int a=balance/64; int b=balance%64/16; int c= balance%64%16/4; int d=balance%64%16%4/1; System.out.println(a+b+c+d); }这代码我在idea里运行成功,提交就报错,我提交9次还是报错,大家看看有什么问题
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner scanner = new Scanner(System.in); int n; n = scanner.nextInt(); int zhaoling = 1024 - n; int x = 0,y = 0,z = 0,m = 0;//x-1yuan,y-4yuan,z-16yuan,m-64yuan m = zhaoling / 64; if(zhaoling % 64 != 0){ zhaoling = zhaoling - m * 64; z = zhaoling / 16; if(zhaoling %16 != 0){ zhaoling = zhaoling - z * 16; y = zhaoling / 4; if(zhaoling % 4 != 0){ zhaoling = zhaoling - y * 4; x = zhaoling; } } } int sum = x + y + z + m; System.out.println(sum); } }
import java.util.Scanner; public class Main { public static void printCoinNum(int price){ //过滤到非法的商品price if(price <=0 || price > 1024){ return; } int num = 1024 - price; //应该找回的零钱 int count1 = num / 64; //64元硬币的数量 int temp1 = num - (count1*64); int count2 = temp1 / 16; //16元硬币的数量 int temp2 = temp1 - (count2*16); int count3 = temp2 / 4; //4元硬币的数量 int temp3 = temp2 - (count3*4); int count4 = temp3 / 1; //1元硬币的数量 int temp4 = temp3 - (count4*1); if(temp4 ==0){ System.out.println(count1 + count2 + count3 + count4); //应找零钱的硬币总数 }else{ System.out.println("error!"); } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int price = sc.nextInt(); printCoinNum(price); } sc.close(); } }
import java.util.Scanner; public class Main { public static int minCoins(int give){ int remain=1024-give,cnt=0; while(remain!=0){ if(remain>=64){ cnt+=remain/64; remain-=remain/64*64; }else if(remain>=16){ cnt+=remain/16; remain-=remain/16*16; }else if(remain>=4){ cnt+=remain/4; remain-=remain/4*4; }else{ cnt+=remain; remain=0; } } return cnt; } public static void main(String[] args) { Scanner scanner=new Scanner(System.in); int input=scanner.nextInt(); System.out.println(Main.minCoins(input)); } }
//动态规划 import java.util.Scanner; /** * @Date: 2020-05-06 22:13 * @version: 1.0 */ public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = 1024 - sc.nextInt(); int dp[] = new int[t+1]; for (int i = 1; i <= t; i++){ dp[i] = dp[i-1]; if (i - 4 >= 0){ dp[i] = Math.min(dp[i], dp[i-4]); if (i - 16 >= 0){ dp[i] = Math.min(dp[i], dp[i-16]); if (i - 64 >= 0) dp[i] = Math.min(dp[i], dp[i-64]); } } dp[i]++; } System.out.println(dp[t]); } }
import java.util.*; public class Main{ public static void main(String[]args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int n = 1024-sc.nextInt(); int[]coins={1,4,16,64}; int max=n+1; int[]dp=new int[max+1]; Arrays.fill(dp,max); dp[0]=0; for(int i=1;i<=n;i++){ for(int coin:coins){ if(i>=coin)dp[i]=Math.min(dp[i],dp[i-coin]+1); } } System.out.println(dp[n]); } } }
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int i = scanner.nextInt(); i= 1024-i; int sum = i/64; i=i%64; if (i>0){ sum+=i/16; i=i%16; if (i>0){ sum+=i/4; i=i%4; if (i>0){ System.out.println(sum+i); return; } } } System.out.println(sum); }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int N = input.nextInt(); input.close(); System.out.println(Solution(N)); } public static int Solution(int N) { return (1024 - N)/64 + (1024 - N)%64/16 + (1024 - N)%64%16/4 + (1024 - N)%64%16%4; } }
import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner input = new Scanner(System.in); int N = input.nextInt(); int n = 1024 - N; int n1 = n/64; int n2 = n%64/16; int n3 = n%64%16/4; int n4 = n%64%16%4/1; int sum = n1+n2+n3+n4; System.out.println(sum); } }