当前位置:网站首页>Pat class B 1016 C language

Pat class B 1016 C language

2022-06-23 05:57:00 Octopus bro

1016. part A+B (15)


Positive integer A Of “DA( by 1 An integer ) part ” Defined by A All in DA A new integer composed of PA. for example : Given A = 3862767,DA = 6, be A Of “6 part ”PA yes 66, because A There is 2 individual 6.

Now given A、DA、B、DB, Please write a program to calculate PA + PB.

Input format :

The input is given in turn on a line A、DA、B、DB, Space between , among 0 < A, B < 1010.

Output format :

Output in one line PA + PB Value .

sample input 1:
3862767 6 13530293 3
sample output 1:
399
sample input 2:
3862767 1 13530293 8
sample output 2:
0


Ideas : Use two for Loop to count the number of characters to be counted in the two strings , So we can calculate two numbers , Add one


One 、 Starting variable

1.input1 and input2 These are two variables that accept strings

2.a and b The characters to be found

3.countOfA and countOfB Is the number of occurrences corresponding to two characters

4.A and B Is the last corresponding number


Two 、 operation

1. Accept two strings and two characters

2. use for Loop through two strings , Count the number of occurrences of characters to be searched

3. To calculate the A and B

4. Add and A and B Output


3、 ... and 、 Code

#include "stdio.h"
#include "string.h"
#include "math.h"
typedef char string[10];

int main()
{
	string input1,input2;
	char a,b;// Two characters to find  
	int A = 0, B = 0;// Finally used to calculate sum A and B
	scanf("%s %c %s %c",input1, &a, input2, &b);
	char * p;
	int countOfA = 0, countOfB = 0;
	// Statistics a Number of occurrences  
	int i = 0;
	for(i = 0; i < strlen(input1); i++)
	{
		if(input1[i] == a)
		countOfA++;
	}
	// Statistics b Number of occurrences  
	for(i = 0; i < strlen(input2); i++)
	{
		if(input2[i] == b)
		countOfB++;
	} 
	// Separate calculation A、B 
	for(i = 0; i < countOfA; i++)
	{
		A += (a - '0') * pow(10, i);
	}
	for(i = 0; i < countOfB; i++)
	{
		B += (b - '0') * pow(10, i);
	}
	printf("%d",A + B);
	return 0;
}


原网站

版权声明
本文为[Octopus bro]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230410348096.html