题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=5625
题意:给一个化学方程式,aA+bB=C,abc行元素种类及个数求a和b的大小
思路:暴力即可,比赛的时候因为写错一个变量,死活没调出来,里面的3个数组也可以开成一个第一维为3的二维数组
AC代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <cstring> #include <climits> #include <cmath> #include <cctype> const int inf = 0x7f7f7f7f;//2139062143 typedef long long ll; using namespace std; int sum1[30]; int sum2[30]; int sum3[30]; int main() { int t; int a,b,c; int i,j,k; scanf("%d",&t); while(t--) { memset(sum1,0,sizeof(sum1)); memset(sum2,0,sizeof(sum2)); memset(sum3,0,sizeof(sum3)); scanf("%d%d%d",&a,&b,&c); getchar(); char d; int e; for(i=0; i<a; i++) { scanf("%c%d",&d,&e); getchar(); sum1[d-'A'] = e; } for(i=0; i<b; i++) { scanf("%c%d",&d,&e); getchar(); sum2[d-'A'] = e; } int max1 = 0; for(i=0; i<c; i++) { scanf("%c%d",&d,&e); getchar(); sum3[d-'A'] = e; if(e > max1) { max1 = e;//就是这写错了,写成a了 } } bool flag = false; int x,y; for(i=1; i<=max1; i++) { for(j=1; j<=max1; j++) { for(k=0; k<26; k++) { if(i*sum1[k]+j*sum2[k] != sum3[k]) { break; } } if(k == 26) { flag = true; x = i; y = j; } } if(flag) { break; } } if(flag) { printf("%d %d\n",x,y); } else { printf("NO\n"); } } return 0; }
,