#26977: 為什麼NA?


will901024 (The joke is on me)

學校 : 臺中市私立致用高級中學
編號 : 158385
來源 : [211.20.7.115]
最後登入時間 :
2023-11-23 22:52:52
c317. 硬幣問題!前傳 | From: [125.230.52.21] | 發表日期 : 2021-09-05 16:53

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int t =sc.nextInt();
for(int i=0;i<t;i++)
{
int x=sc.nextInt(),a=sc.nextInt(),b=sc.nextInt(),ma=0,mb=0;
ma=x/a;
boolean isbreak=false;
for(;ma>0;ma--)
{
for(;mb<10;mb++)
{
if(ma*a+mb*b==x)
{
isbreak=true;
break;
}
}
if(isbreak==true)
{
break;
}
else if(isbreak==false){mb=0;}
}
if(isbreak==false){
System.out.println("-1");
}
else if(isbreak==true)
{
System.out.println(ma+mb);
}
}
}
}
}


我看每個測資都一樣 x=258 a=24 b=20 但是答案顯示26
 
#26978: Re:為什麼NA?


cges30901 (cges30901)

學校 : 不指定學校
編號 : 30877
來源 : [101.136.203.77]
最後登入時間 :
2024-04-07 15:34:14
c317. 硬幣問題!前傳 | From: [27.246.4.142] | 發表日期 : 2021-09-05 19:16

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int t =sc.nextInt();
for(int i=0;i<t;i++)
{
int x=sc.nextInt(),a=sc.nextInt(),b=sc.nextInt(),ma=0,mb=0;
ma=x/a;
boolean isbreak=false;
for(;ma>0;ma--)
{
for(;mb<10;mb++)
{
if(ma*a+mb*b==x)
{
isbreak=true;
break;
}
}
if(isbreak==true)
{
break;
}
else if(isbreak==false){mb=0;}
}
if(isbreak==false){
System.out.println("-1");
}
else if(isbreak==true)
{
System.out.println(ma+mb);
}
}
}
}
}


我看每個測資都一樣 x=258 a=24 b=20 但是答案顯示26


1.為什麼要mb<10?應該是mb<=x/b

2.ma有可能是0

 

而且你的程式太複雜了,根本不需要內層的for迴圈,直接判斷x-ma*a是否能整除就可以了

 
#26979: Re:為什麼NA?


will901024 (The joke is on me)

學校 : 臺中市私立致用高級中學
編號 : 158385
來源 : [211.20.7.115]
最後登入時間 :
2023-11-23 22:52:52
c317. 硬幣問題!前傳 | From: [125.230.52.21] | 發表日期 : 2021-09-05 19:51

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int t =sc.nextInt();
for(int i=0;i<t;i++)
{
int x=sc.nextInt(),a=sc.nextInt(),b=sc.nextInt(),ma=0,mb=0;
ma=x/a;
boolean isbreak=false;
for(;ma>0;ma--)
{
for(;mb<10;mb++)
{
if(ma*a+mb*b==x)
{
isbreak=true;
break;
}
}
if(isbreak==true)
{
break;
}
else if(isbreak==false){mb=0;}
}
if(isbreak==false){
System.out.println("-1");
}
else if(isbreak==true)
{
System.out.println(ma+mb);
}
}
}
}
}


我看每個測資都一樣 x=258 a=24 b=20 但是答案顯示26


1.為什麼要mb<10?應該是mb<=x/b

2.ma有可能是0

 

而且你的程式太複雜了,根本不需要內層的for迴圈,直接判斷x-ma*a是否能整除就可以了

太感謝你了!!

 
ZeroJudge Forum