链家金币问题

1.硬币交换(coins.pas/c/cpp)【问题描述】FZ身上有g1个金币,s1个银币,b1个铜币。而他至少需要g2个金币,s2个银币,b2个铜币,FZ只好来到银行。银行有如下规定:1、 你可以用1个金币换9个银币。2、 你可以用11个银币换1个金币。3、 你可以用1个银币换9个铜币。4、 你可以用11个铜币换一个银币。【输入文件】第一行包含3个数,g1,s1,b1第二行包含3个数,g2,s2,b2【输出文件】如果能够完成任务,输出需要交换的最少的次数。如果不能够完成任务,输出-1。【样例输入】1 0 00 0 81【样例输出】10【数据规模】100%的数据满足0 ≤ g1,s1,b1,g2,s2,b2 ≤ 1000000
全部评论
import java.util.Scanner; public class Main1 { static int[] re=new int[1]; static int[] g1=new int[1]; static int[] s1=new int[1]; static int[] b1=new int[1]; static int[] g2=new int[1]; static int[] s2=new int[1]; static int[] b2=new int[1]; public static void main(String[] args) { Scanner in  = new Scanner(System.in); g1[0] = in.nextInt(); s1[0]= in.nextInt(); b1[0]= in.nextInt(); g2[0]= in.nextInt(); s2[0]= in.nextInt(); b2[0]= in.nextInt(); getcount(); System.out.print(re[0]); } public static int getcount(){ if(g1[0]>=g2[0]){//无需交换 g1[0]=g1[0]-g2[0]; }else{ if(!leftchange(g1,g1[0]-g2[0],s1)){ if(!leftchange(s1,(g2[0]-g1[0]-s1[0]*11),b1)){ return -1; }else{ leftchange(g1,g2[0]-g1[0],s1); } } } if(s1[0]>=s2[0]){//无需交换 s1[0] = s1[0]-s2[0]; }else{ while(g1[0]>=0&&s1[0]<=s2[0]){ rightchange(g1,1,s1); } if(s1[0]<s2[0] ){ if(!leftchange(s1,s2[0]-s1[0],b1)){ return -1; } } s1[0]=s1[0]-s2[0]; } if(b1[0]>=b2[0]){ return re[0]; }else{ while(b1[0]<=b2[0]){ if(!rightchange(s1,1,b1)){ break; } } if(b1[0]<b2[0]){ if(!rightchange(g1,(b2[0]-b1[0]+80/*防止为0的情况*/)/(9*9),s1)){ return -1; }else{ rightchange(s1,(b2[0]-b1[0]+8)/9,b1); } } } return re[0]; } public static boolean leftchange(int[] h,int count,int[] l){ if( l[0]>= 11*count){ l[0] = l[0]-11*count; h[0]=h[0]+count; re[0] = re[0]+count; return true; }else{ return false; } } public static boolean rightchange(int[] h,int count,int[] l){ if(h[0]>=count){ l[0]  = l[0]+ h[0]*count*9; h[0] -=count; re[0] += count; return true; }else{ return false; } } }
点赞 回复 分享
发布于 2017-08-23 15:15
有没有不用枚举的方法
点赞 回复 分享
发布于 2017-08-21 21:16
#include <iostream> using namespace std; int main() { int g1, s1, b1; int g2, s2, b2; cin >> g1; cin >> s1; cin >> b1; cin >> g2; cin >> s2; cin >> b2; int count = 0; int b3 = 0; int s3 = 0; if (b1 < b2) { while (b1<b2) { if (s1 > 0) { s1--; b1 += 9; count++; } else { if (g1 >0) { g1--; s1 += 9; count++; } else { cout << -1; return 0; } } } } else { b3 = b2 - b1; } if (s1 < s2) { while (s1<s2) { if (b3 >= 11) { b3 -= 11; count++; s1++; } if (g1 > 0) { g1--; s1 += 9; count++; } else { cout << -1; return 0; } } } else { s3 = s2 - s1; } if (g1 < g2) { if (s3 >= 11) { g1++; s3 -= 11; count++; } else if (b3 >= 121) { b3 -= 121; count += 12; g1++; } else { cout << -1; return 0; } } cout << count; return 0; }
点赞 回复 分享
发布于 2017-08-21 20:57
过了82,也不知道错哪了?
点赞 回复 分享
发布于 2017-08-21 20:55
获取信息半天,很扎心,都开始怀疑自己了
点赞 回复 分享
发布于 2017-08-21 20:50
不知道对不对,但是还在转圈就交了,前面两道我实在想不出为啥不ac #include <iostream> using namespace std; int main() { int G1=0,S1=0,B1=0; int G2=0,S2=0,B2=0; cin>>G1>>S1>>B1; cin>>G2>>S2>>B2; int d1=G1-G2,d2=S1-S2,d3=B1-B2; if (d1<0&&d2<0&&d3<0) { cout<<-1; return 0; } if (d1>=0&&d2>=0&&d3>=0) { cout<<0; return 0; } int countNum=0; while (!(d1>=0&&d2>=0&&d3>=0)) { if (d1<0) { if (d2-11>=0) { d1=d1+1; d2=d2-11; countNum++; }else if (d3-121>=0) { d1=d1+1; d3=d3-121; countNum=countNum+12; } } if (d2<0) { if (d1-1>=0) { d2=d2+9; d1=d1-1; countNum++; }else if (d3-11>=0) { d2=d2+1; d3=d3-11; countNum=countNum+1; } } if (d3<0) { if (d2-1>=0) { d3=d3+9; d2=d2-1; countNum=countNum+1; }else if (d1-1>=0) { d3=d3+81; d1=d1-1; countNum=countNum+10; } } } cout<<countNum; }
点赞 回复 分享
发布于 2017-08-21 20:44

相关推荐

05-16 11:16
已编辑
东华理工大学 Java
牛客73769814...:盲猜几十人小公司,庙小妖风大,咋不叫她去4️⃣呢😁
牛客创作赏金赛
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务