打內SHOW
2007-07-12 07:48:01 UTC
我先說明一下,這些程式碼可以COMPILE過,也可以RUN,
這問題是在解決利潤問題
import java.util.Scanner;
public class Main {
public Main() {
}
public static int max(int compare1, int compare2, int compare3) {
int ascNumber[]={compare1, compare2, compare3};
int bubbleTemp=-1;
for (int max1=0; max1<2; max1++)
for(int max2=max1+1; max2<3; max2++){
if(ascNumber[max1]>ascNumber[max2]){
bubbleTemp=ascNumber[max1];
ascNumber[max1]=ascNumber[max2];
ascNumber[max2]=bubbleTemp;
}
}
return ascNumber[2];
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// TODO code application logic here
int G, S, C; //原料G的數量、原料S的數量、原料C的數量
int g, s, c; //原料G的單價成本、原料S的單價成本、原料C的單價成本
int g1, s1, c1, price1; //方案一需要的各原料單位,以及得到價值
int g2, s2, c2, price2; //方案二需要的各原料單位,以及得到價值
int g3, s3, c3, price3; //方案三需要的各原料單位,以及得到價值
int total=0;
G=input.nextInt(); S=input.nextInt(); C=input.nextInt();
g=input.nextInt(); s=input.nextInt(); c=input.nextInt();
g1=input.nextInt(); s1=input.nextInt(); c1=input.nextInt();
price1=input.nextInt();
g2=input.nextInt(); s2=input.nextInt(); c2=input.nextInt();
price2=input.nextInt();
g3=input.nextInt(); s3=input.nextInt(); c3=input.nextInt();
price3=input.nextInt();
int x=price1-(g*g1+s*s1+c*c1);
int y=price2-(g*g2+s*s2+c*c2);
int z=price3-(g*g3+s*s3+c*c3);
if(x>=max(x,y,z)){
for(int n=0; n<=G; n=n+g1){
for(int m=0; m<=S; m=m+s1){
for(int o=0; o<=C; o=o+c1){
if(G>=g1 && S>=s1 && C>=c1){
G=G-g1;
S=S-s1;
C=C-c1;
total=total+price1;
}
}
}
}
if(y>=z){
for(int m=0; m<=G; m=m+g2){
for(int n=0; n<=S; n=n+s2){
for(int o=0; o<=C; o=o+c2){
if(G>=g2 && S>=s2 && C>=c2){
G=G-g2;
S=S-s2;
C=C-c2;
total=total+price2;
}
}
}
}
}
else if(z>=y){
for(int m=0; m<=G; m=m+g3){
for(int n=0; n<=S; n=n+s3){
for(int o=0; o<=C; o=o+c3){
if(G>=g3 && S>=s3 && C>=c3){
G=G-g3;
S=S-s3;
C=C-c3;
total=total+price3;
}
}
}
}
}
}
else if(y>=max(x,y,z)){
for(int n=0; n<=G; n=n+g2){
for(int m=0; m<=S; m=m+s2){
for(int o=0; o<=C; o=o+c2){
if(G>=g2 && S>=s2 && C>=c2){
G=G-g2;
S=S-s2;
C=C-c2;
total=total+price2;
}
}
}
}
if(x>=z){
for(int m=0; m<=G; m=m+g1){
for(int n=0; n<=S; n=n+s1){
for(int o=0; o<=C; o=o+c1){
if(G>=g1 && S>=s1 && C>=c1){
G=G-g1;
S=S-s1;
C=C-c1;
total=total+price1;
}
}
}
}
}
else if(x<=z){
for(int m=0; m<=G; m=m+g3){
for(int n=0; n<=S; n=n+s3){
for(int o=0; o<=C; o=o+c3){
if(G>=g3 && S>=s3 && C>=c3){
G=G-g3;
S=S-s3;
C=C-c3;
total=total+price3;
}
}
}
}
}
}
else if(z>=max(x,y,z)){
for(int n=0; n<=G; n=n+g3){
for(int m=0; m<=S; m=m+s3){
for(int o=0; o<=C; o=o+c3){
if(G>=g3 && S>=s3 && C>=c3){
G=G-g3;
S=S-s3;
C=C-c3;
total=total+price3;
}
}
}
}
if(y>=x){
for(int m=0; m<=G; m=m+g2){
for(int n=0; n<=S; n=n+s2){
for(int o=0; o<=C; o=o+c2){
if(G>=g2 && S>=s2 && C>=c2){
G=G-g2;
S=S-s2;
C=C-c2;
total=total+price2;
}
}
}
}
}
else if(y<=x){
for(int m=0; m<=G; m=m+g1){
for(int n=0; n<=S; n=n+s1){
for(int o=0; o<=C; o=o+c1){
if(G>=g1 && S>=s1 && C>=c1){
G=G-g1;
S=S-s1;
C=C-c1;
total=total+price1;
}
}
}
}
}
}
System.out.print(total);
}
}
當我輸入9 8 7 1 1
3 2 1 7
2 2 2 9
1 2 3 7
輸出34
這是正解,可是當我輸入9 8 7 1 1 1
1 2 3 12
3 2 0 15
1 1 1 7
電腦就當機了= =+
請問各位強者們,我程式碼到底哪裡出錯了?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 210.240.180.217
這問題是在解決利潤問題
import java.util.Scanner;
public class Main {
public Main() {
}
public static int max(int compare1, int compare2, int compare3) {
int ascNumber[]={compare1, compare2, compare3};
int bubbleTemp=-1;
for (int max1=0; max1<2; max1++)
for(int max2=max1+1; max2<3; max2++){
if(ascNumber[max1]>ascNumber[max2]){
bubbleTemp=ascNumber[max1];
ascNumber[max1]=ascNumber[max2];
ascNumber[max2]=bubbleTemp;
}
}
return ascNumber[2];
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// TODO code application logic here
int G, S, C; //原料G的數量、原料S的數量、原料C的數量
int g, s, c; //原料G的單價成本、原料S的單價成本、原料C的單價成本
int g1, s1, c1, price1; //方案一需要的各原料單位,以及得到價值
int g2, s2, c2, price2; //方案二需要的各原料單位,以及得到價值
int g3, s3, c3, price3; //方案三需要的各原料單位,以及得到價值
int total=0;
G=input.nextInt(); S=input.nextInt(); C=input.nextInt();
g=input.nextInt(); s=input.nextInt(); c=input.nextInt();
g1=input.nextInt(); s1=input.nextInt(); c1=input.nextInt();
price1=input.nextInt();
g2=input.nextInt(); s2=input.nextInt(); c2=input.nextInt();
price2=input.nextInt();
g3=input.nextInt(); s3=input.nextInt(); c3=input.nextInt();
price3=input.nextInt();
int x=price1-(g*g1+s*s1+c*c1);
int y=price2-(g*g2+s*s2+c*c2);
int z=price3-(g*g3+s*s3+c*c3);
if(x>=max(x,y,z)){
for(int n=0; n<=G; n=n+g1){
for(int m=0; m<=S; m=m+s1){
for(int o=0; o<=C; o=o+c1){
if(G>=g1 && S>=s1 && C>=c1){
G=G-g1;
S=S-s1;
C=C-c1;
total=total+price1;
}
}
}
}
if(y>=z){
for(int m=0; m<=G; m=m+g2){
for(int n=0; n<=S; n=n+s2){
for(int o=0; o<=C; o=o+c2){
if(G>=g2 && S>=s2 && C>=c2){
G=G-g2;
S=S-s2;
C=C-c2;
total=total+price2;
}
}
}
}
}
else if(z>=y){
for(int m=0; m<=G; m=m+g3){
for(int n=0; n<=S; n=n+s3){
for(int o=0; o<=C; o=o+c3){
if(G>=g3 && S>=s3 && C>=c3){
G=G-g3;
S=S-s3;
C=C-c3;
total=total+price3;
}
}
}
}
}
}
else if(y>=max(x,y,z)){
for(int n=0; n<=G; n=n+g2){
for(int m=0; m<=S; m=m+s2){
for(int o=0; o<=C; o=o+c2){
if(G>=g2 && S>=s2 && C>=c2){
G=G-g2;
S=S-s2;
C=C-c2;
total=total+price2;
}
}
}
}
if(x>=z){
for(int m=0; m<=G; m=m+g1){
for(int n=0; n<=S; n=n+s1){
for(int o=0; o<=C; o=o+c1){
if(G>=g1 && S>=s1 && C>=c1){
G=G-g1;
S=S-s1;
C=C-c1;
total=total+price1;
}
}
}
}
}
else if(x<=z){
for(int m=0; m<=G; m=m+g3){
for(int n=0; n<=S; n=n+s3){
for(int o=0; o<=C; o=o+c3){
if(G>=g3 && S>=s3 && C>=c3){
G=G-g3;
S=S-s3;
C=C-c3;
total=total+price3;
}
}
}
}
}
}
else if(z>=max(x,y,z)){
for(int n=0; n<=G; n=n+g3){
for(int m=0; m<=S; m=m+s3){
for(int o=0; o<=C; o=o+c3){
if(G>=g3 && S>=s3 && C>=c3){
G=G-g3;
S=S-s3;
C=C-c3;
total=total+price3;
}
}
}
}
if(y>=x){
for(int m=0; m<=G; m=m+g2){
for(int n=0; n<=S; n=n+s2){
for(int o=0; o<=C; o=o+c2){
if(G>=g2 && S>=s2 && C>=c2){
G=G-g2;
S=S-s2;
C=C-c2;
total=total+price2;
}
}
}
}
}
else if(y<=x){
for(int m=0; m<=G; m=m+g1){
for(int n=0; n<=S; n=n+s1){
for(int o=0; o<=C; o=o+c1){
if(G>=g1 && S>=s1 && C>=c1){
G=G-g1;
S=S-s1;
C=C-c1;
total=total+price1;
}
}
}
}
}
}
System.out.print(total);
}
}
當我輸入9 8 7 1 1
3 2 1 7
2 2 2 9
1 2 3 7
輸出34
這是正解,可是當我輸入9 8 7 1 1 1
1 2 3 12
3 2 0 15
1 1 1 7
電腦就當機了= =+
請問各位強者們,我程式碼到底哪裡出錯了?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 210.240.180.217