【FOJ】Problem 1171 Hard to Believe, but True!

Problem 1171 Hard to Believe, but True!.

The meaning of problems

Digital read from right to left, it is determined whether the correct equation

Thinking

  • Of str1 + str2 = ans:
    respectively converted into the corresponding character string is read from right to left when the number x1, x2, xa
    string from the forward traverse, the end of the skip all 0, until the first occurrence a positive integer , the remaining into digital
  • Calculation of the equation is established, output

notes

  • scanf("%[^\n]%*c", str)
    %[^\n]Encounter \ n the end, not to \ n reads string
    %*c absorb a character, a character that is read and then again and then abandoned it, is equal togetchar()

Code

#include<cstdio>
#include<string.h>
using namespace std;

char str1[10], str2[10], ans[10];

int str_to_rint(char str[]){
	int len = strlen(str);
	int i = len-1;
	int x = 0;
	while(str[i]=='0')
		i--;
	while(i>=0)
		x = x*10 + str[i--]-'0';
	return x;
}

int main(){
	int x1, x2, x_a;
	while(1){
		scanf("%[^+]%*c", str1);
		scanf("%[^=]%*c", str2);
		scanf("%[^\n]%*c", ans);
		x1 = str_to_rint(str1);
		x2 = str_to_rint(str2);
		x_a = str_to_rint(ans);
		if(x1+x2==x_a)
			printf("True\n");
		else
			printf("False\n");
		if(x1==0 && x2==0 && x_a==0)
			break;
	}
	return 0;
}
Published 28 original articles · won praise 0 · Views 316

Guess you like

Origin blog.csdn.net/qq_44531167/article/details/105330966