Last active
July 11, 2016 13:57
[Uva10925] Krakovia(C++)
This file contains hidden or 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment