題目概要:
計算出每個人平均要付多少錢。
解題方向:
每個case的第一個數字為總人數(N),第二個數字為多少人平分(F),接下來的數字為每個人所應該要付的錢(啤酒單價)。啤酒單價會超過Lnog的範圍,故需要用到Java的api(BigInteger)來實作。如過不使用BigInteger的方法,那就只能字元判斷加總為多少or相除為多少(可參考C++程式碼);
程式碼:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Java | |
import java.util.Scanner; | |
import java.math.BigInteger; | |
class uva10925{ | |
public static void main(String args[]){ | |
Scanner sc=new Scanner(System.in); | |
int count=1; | |
int N=0,F=0; //N=總人數,F=多少人平分價錢 | |
while(((N=sc.nextInt())!=0 && (F=sc.nextInt())!=0)&& sc.hasNext()){ | |
//初始化 sum=0(因為會超過Long範圍,所以使用Java所提供的api) | |
BigInteger sum=BigInteger.ZERO; | |
//加總 | |
for(int i=0;i<N;i++){ | |
BigInteger temp=sc.nextBigInteger(); | |
sum=sum.add(temp); | |
} | |
//輸出資料 | |
System.out.println("Bill #"+count+" costs "+sum+": each friend should pay "+(sum.divide(new BigInteger(Integer.toString(F))))); | |
//Uva online Judge不能直接在上面的輸出最後面加上\r\n,會Presentation Error (PE) | |
System.out.println(""); | |
count++; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//C++ | |
#include <iostream> | |
#include <algorithm> | |
#include <string.h> | |
using namespace std; | |
int find(char[], int); | |
int main() { | |
int N = 0; int F = 0; //N=總人數,F=平分人數。 | |
int count = 1; | |
while ((cin >> N >> F) && (N && F)) { | |
char sum[25]; | |
//初始化 sum陣列為0 | |
for (int i = 0; i<sizeof(sum); i++) { | |
sum[i] = '0'; | |
} | |
//add | |
for (int i = 0; i < N; i++) { | |
int c = 0; //進位數字 | |
char st[25] = { '0' }; | |
cin >> st; | |
reverse(st, st + strlen(st)); //把剛剛所輸入的數字全部反轉,這樣方便加總。 | |
for (int j = 0; j < sizeof(sum); j++) { | |
if (j < strlen(st)) { | |
int temp = sum[j] - 48 + st[j] - 48 + c; | |
c = temp / 10; | |
sum[j] = temp % 10 + 48; | |
}else { | |
int temp = sum[j] - 48 + c; | |
c = temp / 10; | |
sum[j] = temp % 10 + 48; | |
} | |
} | |
} | |
//division | |
reverse(sum, sum + sizeof(sum)); //把剛剛加總的數字再反轉回來。 | |
char di[25]; | |
for (int i = 0; i < sizeof(di); i++) { | |
di[i] = '0'; | |
} | |
int R = 0; | |
for (int i = 1; i < sizeof(sum); i++) { | |
di[i] = ((R * 10 + sum[i] - 48) / F) + 48; | |
R = (R * 10 + sum[i] - 48) % F; | |
} | |
//Output | |
cout << "Bill #" << count << " costs "; | |
//find函數為找出第一個非數字的index。 | |
for (int i = find(sum, sizeof(sum)); i<sizeof(sum); i++) { | |
cout << sum[i]; | |
} | |
cout << ": each friend should pay "; | |
for (int i = find(di, sizeof(di)); i<sizeof(sum); i++) { | |
cout << di[i]; | |
} | |
cout << endl << endl; | |
count++; | |
} | |
return 0; | |
} | |
int find(char f[], int size) { | |
for (int i = 0; i < size; i++) { | |
if (f[i] != '0') { | |
return i; | |
} | |
} | |
return size - 1; | |
} |
文章標籤
全站熱搜