📄 1919.cpp
字号:
//1919
#include <cstdio>
#include <string>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
const int ANSMAX = 10000;
const int NMAX = 20;
int cas = 1,n,ans;
double rate[NMAX];
char strdbl[NMAX][100];
int ndec[NMAX];
int pow10[] = {1,10,100,1000,10000,100000};
bool check(int x,int n,int idx) {
char str[100];
int ival = 100.0*x/n *pow10[ ndec[idx] ] +0.5;
if (ival <= 0) return false;
sprintf(str,"%d",ival/pow10[ ndec[idx] ]);
if (ndec[idx] > 0) sprintf(str+strlen(str),".%0*d",ndec[idx],ival % pow10[ ndec[idx] ]);
return (strcmp(str,strdbl[idx]) == 0);
}
void cal() {
int i,j,k,s;
ans = -1;
for (i=1;i<ANSMAX;i++) {
s = 0;
for (j=0;j<n-1;j++) {
int tmp = rate[j]*i/100.0 +0.5;
if (check(tmp,i,j)) s += tmp;
else break;
}
if (j == n-1 && check(i-s,i,j)) {
ans = i;
return;
}
}
}
int main() {
int i,j,k;
while (scanf("%d",&n),n) {
for (i=0;i<n;i++) {
scanf("%s",strdbl[i]);
sscanf(strdbl[i],"%lf",rate+i);
ndec[i] = 0;
for (k=0,j=strlen(strdbl[i])-1;j>=0;j--,k++) {
if (strdbl[i][j] == '.') {
ndec[i] = k;
break;
}
}
}
cal();
if (ans != -1) printf("Case %d: %d\n",cas++,ans);
else printf("Case %d: error\n",cas++);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -