刚学没几天比较菜,希望大佬解个惑。
by 洛水·天依 @ 2019-02-12 13:52:14
希望更丰富的展现?使用Markdown
by qwaszx @ 2019-02-12 13:52:50
```cpp
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
int puttogether(int a[])
{
int m=0,y=0,i;
for (i = 6; i >= 0; i--)
{
if (a[i] != 0)
{
break;
}
}
for (int j = i; j >= 0; j--)
{
y += a[j] * pow(10, i - j);
}
return y;
}
int main()
{
char a[200];
cin.getline(a, 200);
int C = 0, H = 0, O = 0, c[7] = {}, h[7] = {}, o[7] = {};
for (int i = 0; i <= strlen(a)-1; i++)
{
if (a[i] == 'C' && (('9' < a[i + 1] || a[i + 1] < '0') || a[i + 1] == '\0'))
C++;
if (a[i] == 'H' && (('9' < a[i + 1] || a[i + 1] < '0') || a[i + 1] == '\0'))
H++;
if (a[i] == 'O' && (('9' < a[i + 1] || a[i + 1] < '0') || a[i + 1] == '\0'))
O++;
if (a[i] == 'C' && ('0' <= a[i + 1] && a[i + 1] <= '9'))
{
int m = 0;
for (int j = i + 1;; j++)
{
if ('0' <= a[j] && a[j] <= '9')
{
c[m] = int(a[j]) - 48;
m++;
}
else
{
break;
}
}
C += puttogether(c);
}
if (a[i] == 'H' && ('0' <= a[i + 1] && a[i + 1] <= '9'))
{
int m = 0;
for (int j = i + 1;; j++)
{
if ('0' <= a[j] && a[j] <= '9')
{
h[m] = int(a[j]) - 48;
m++;
}
else
{
break;
}
}
H += puttogether(h);
}
if (a[i] == 'O' && ('0' <= a[i + 1] && a[i + 1] <= '9'))
{
int m = 0;
for (int j = i + 1;; j++)
{
if ('0' <= a[j] && a[j] <= '9')
{
o[m] = int(a[j]) - 48;
m++;
}
else
{
break;
}
}
O += puttogether(o);
}
}
int p, q;
p = C;
if (H % 2 == 0)
{
q = H / 2;
}
else
{
q = H;
}
cout << p << " " << q;
return 0;
}
```
by 洛水·天依 @ 2019-02-12 13:53:10
试试CH4O,答案应该是:2 4
by Skaditxdy @ 2021-03-31 17:48:09