UVa1585 Score
#include<stdio.h>
#include<string.h>int main()
{
char a[100];
int n , sum=0, p=0;
scanf("%d",&n);
while(n--){
p = 0;
sum = 0;
scanf("%s",a);
for(int i = 0;i<strlen(a);i++)
{
if(a[i]=='O') p++;
if(a[i]=='X') p = 0;
sum+=p;
}
printf("%d\n",sum);
}
return 0;
}
/* 1.设立开关int p(遇到O就+1,遇到X就为零),设立n(输入的字符串个数),设立sum(最终的得分)
2.输入字符串
3.for循环遍历每一个字符串,为X就为零,为O就加一(strlen(),直接比较 ) !!注意strcmp只能比较字符串
4.最后加上sum。
*/
UVa1586 Molar Mass
#include<stdio.h>#include<string.h>
#include<ctype.h>
int digitnum(char a[] , int i , int lenght)
{
int sum=0;
bool x =true;
for( ;i<lenght;i++)
{
if(isdigit(a[i])==0)
{
x = false;
break;
}
if(isdigit(a[i])&&x==true)
{
sum = sum*10 + (a[i]-'0'); //有个错误是在sum后直接重置了sum,导致输出为0;
}
}
return sum;
}
int main()
{
char a[100];
int n,num,dignum,x;
double sum=0;
double fmlaC = 12.01;
double fmlaO = 16.00;
double fmlaH = 1.008;
double fmlaN = 14.01;
scanf("%d",&n);
while(n--){
sum = 0;
scanf("%s",a);
int length = strlen(a);
for(int i = 0;i<length;i++)
{
num = 1;
dignum = 0;
x=0;
switch (a[i])
{
case 'C' :
if(isdigit(a[i+1]))
num = digitnum(a,i+1,length);
sum+= num*fmlaC;
dignum = num; //dignum的赋值应该在外面
while(dignum)
{
dignum = dignum/10;
x++;
}
i = i+x-1; //i应该跳到最后一个数字上面,否则最后一个元素无法记值
break;
case 'H':
if(isdigit(a[i+1])) num = digitnum(a,i+1,length);
sum+= num*fmlaH ;
dignum = num;
while(dignum)
{
dignum = dignum/10;
x++;
}
i = i+x-1;
break;
case 'O':
if(isdigit(a[i+1])) num = digitnum(a,i+1,length);
sum += num*fmlaO ;
dignum = num;
while(dignum)
{
dignum = dignum/10;
x++;
}
i = i+x-1;
break;
case 'N':
if(isdigit(a[i+1])) num = digitnum(a,i+1,length);
sum+= num*fmlaN ;
dignum = num;
while(dignum)
{
dignum = dignum/10;
x++;
}
i = i+x-1;
break;
default:
break;
}
}
printf("%.3f\n",sum);
}
return 0;
}
/* 1.设立n(输入的字符串个数),设立sum(最终的分子量),num(分子的个数)
2.输入字符串
***** 设立函数
传入(char 【】,subscript ,size)
返回值为num
3.for循环遍历每一个字符串, 补充——有特殊情况数字超过一位数的时候
(1)判断是数字还是字符
(2)如果为C、H、O、N —> sum += (对应分子量);
(3)如果是数字,就让 num = num-1 ,再让sum+=(对应分子量)*(num-1);
(4) 调整for循环中下标到每个元素相对应的后一个数字中
*/
UVa1225 Digit Counting
#include<stdio.h>
#include<string.h>
int main()
{
int num[10];
memset(num,0,sizeof(num));
int n,N,u;
int last = 0,first = 1;
bool x = true;
scanf("%d",&n);
while(n--)
{
first = 1;
scanf("%d",&N);
for(int i = 1; i<=N ; i++)
{
x = true;
u = i;
while(x==true)
{
last = u%10;
switch(last)
{
case 0 :
num[0]++;
break;
case 1 :
num[1]++;
break;
case 2 :
num[2]++;
break;
case 3 :
num[3]++;
break;
case 4 :
num[4]++;
break;
case 5 :
num[5]++;
break;
case 6 :
num[6]++;
break;
case 7 :
num[7]++;
break;
case 8 :
num[8]++;
break;
case 9 :
num[9]++;
break;
default :
break;
}
u =u/10;
if(u==0) x = false;
}
}
for(int i = 0 ; i<10;i++)
{
if(first) first = 0;
else printf(" ");
printf("%d",num[i]);
}
printf("\n");
}
}
/* 问题更正: 1.输入n与输入N要分为两个Scanf输出
2.在for循环中,如果要变化for中新定义的值,
需要重新定义一个副本值,对副本值进行变换,
不要对定义数变换。因为for循环中的条件与这个
值有关。
*/