您的位置:首页 > 其它

PAT乙级:1016. 部分A+B (C)

2016-08-13 22:08 169 查看


#include <stdio.h>
#include <stdlib.h>
int main()
{
char A[10];
char B[10];
char D_A;
int count_A=0;
count_A=0;
int count_B=0;
count_B=0;
char D_B;
scanf("%s %c %s %c",&A,&D_A,&B,&D_B);
int i;
for(i=0; i<10; i++)
{
if(A[i]==D_A)
{
count_A++;
}
if(B[i]==D_B)
{
count_B++;
}
}
int a=(int)D_A-48;//将字符转换为整数
int b=(int)D_B-48;
int Pa;
int Pb;
//根据是否出现了D为Pa和Pb赋初值
if(count_A==0)
{
Pa=0;
}
else
{
Pa=a;
}
if(count_B==0)
{
Pb=0;
}
else
{
Pb=b;
}

int result=0;//和
//生成Pa Pb
for(i=0; i<count_A-1; i++)//因为在赋初值的时候已经给了一次
{
Pa*=10;
Pa+=a;
}
for(i=0; i<count_B-1; i++)
{
Pb*=10;
Pb+=b;
}
result=Pa+Pb;
printf("%d",result);
system ("pause");
return 0;
}


题目不难,重点在于对于没有该数出现情况的考虑。我们在实际编程之中构思好一个“公式”之后,也要思考,是不是就会有临界情况出现。祝大家编程愉快,万事如意!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: