2012年3月9日 星期五

百貨公司打折程式

豪慷慨百貨公司週年慶的打折策略,吸引了許多客人上門,因此公司決定再回饋客戶,當客戶消費超過2000 元時打7 折,消費超過5000 元時打6 折,消費超過10000 元時打55 折。請幫該公司寫出一個新的收銀台程式,輸入顧客購買總金額n 後,計算顧客實際需付的錢。

輸入說明:
購買金額n

輸出說明:
實付金額

輸入範例:
3000
6000
12000

輸出範例:
2100
3600
6600

5 則留言:

  1. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Do While Not EOF(1)
    Input #1, x
    Select Case x
    Case Is >= 10000
    Print #2, x * 0.55
    Case Is >= 5000
    Print #2, x * 0.6
    Case Is >= 1000
    Print #2, x * 0.7
    Case Else: Print #2, x
    End Select
    Loop
    Close
    Close
    End
    End Sub

    回覆刪除
  2. package work;
    import java.io.*;
    public class Work{



    public static void main(String[] args) {
    String file = "c:\\test.txt";
    String Fo = "c:\\out.txt";
    String in="0";

    try{
    DataInputStream isr= new DataInputStream(new FileInputStream(file));
    BufferedReader b = new BufferedReader(new InputStreamReader(isr));
    FileOutputStream A = new FileOutputStream(Fo);
    OutputStreamWriter out = new OutputStreamWriter(A);
    int i ;

    while(in !=null){
    in = b.readLine();
    if (in != null){
    int ans = Integer.parseInt(in);
    ans = S(ans);
    String Ans = String.valueOf(ans);
    System.out.println(Ans);
    out.write(Ans);
    out.write("\r\n");
    }
    }
    out.close();
    }
    catch(Exception e){}
    }



    public static int S(int n){

    if (n>=2000 & n<5000){
    n= n*7 /10 ;
    }
    if (n>=5000 & n<10000){
    n= n*6 /10 ;
    }
    if (n>=10000){
    n= n*55 /100 ;
    }

    return n;
    }
    }

    回覆刪除
  3. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Do While Not EOF(1)
    Input #1, n
    ans = 0
    Select Case n
    Case Is >= 10000
    ans = n * 0.55
    Case Is >= 5000
    ans = n * 0.6
    Case Is >= 2000
    ans = n * 0.7
    Case Else: ans = n
    End Select
    Print #2, ans
    Loop
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  4. Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Do While Not EOF(1)
    Input #1, x
    If x < 2000 Then
    Print #2, x
    Else
    Select Case x
    Case Is >= 10000
    Print #2, x * 0.55
    Case Is >= 5000
    Print #2, x * 0.6
    Case Is >= 2000
    Print #2, x * 0.7
    End Select
    End If
    Loop
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  5. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Do While Not EOF(1)
    Input #1, n
    n = Val(n)
    If n > 2000 And n <= 5000 Then n = n * 0.7
    If n > 5000 And n <= 10000 Then n = n * 0.6
    If n > 10000 Then n = n * 0.55
    Print #2, n
    Loop
    Close #2
    Close #1
    End
    End Sub

    回覆刪除