转载出处:code4101。
Exercises:
习题3-1: 1585 - Score
#include <stdio.h>
#include <string.h>
#define maxn 100
int main()
{
char s[maxn];
int T,score,tot;
scanf("%d",&T);
while(T--)
{
memset(s,0,sizeof(s));
scanf("%s",s);
score = 0;tot = 0;//必须在循环内,不然值循环叠加
int len = strlen(s);
for(int i = 0;i < len;i++)
{
if (s[i] == 79)
{
tot++;
score += tot;
}
else
tot = 0;//重置为0
}
printf("%d\n",score);
}
return 0;
}
习题3-2: 1586 - Molar mass
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define maxn 100
int main()
{
char s[maxn];
int T,w;
double sum;
scanf("%d",&T);
while(T--)
{
memset(s,0,sizeof(s));
scanf("%s",s);
sum = 0;
int len = strlen(s);
for(int i = 0;i < len;i++)
{
// 算出后两位所代表的数值
if (isdigit(s[i+1]))
{
if (isdigit(s[i+2])) w = 10*(s[i+1]-'0')+s[i+2]-'0';
else
w = s[i+1] - '0';
}
else
w = 1;
// 判断第一位
if (!isalpha(s[i])) continue;
else if (s[i] == 'C') sum += 12.01*w;
else if (s[i] == 'H') sum += 1.008*w;
else if (s[i] == 'O') sum += 16.00*w;
else sum += 14.01*w;
}
printf("%.3lf\n",sum);
}
return 0;
}
习题3-3:1225 - Digit Counting
#include <stdio.h>
int main()
{
int T,N, t;
int i = 0;
scanf("%d",&T);
while(T--)
{
int count[10] = {0};
scanf("%d",&N);
for (i = 1; i <= N; i++)
{
t = i;
while (t) count[t%10]++, t/=10;
}
for (i = 0; i < 9; i++)
printf("%d ", count[i]);
printf("%d\n",count[9]);
}
return 0;
}
习题3-4: