topic | enter | output | limit | Example |
There is a special positive integer (int type) that divides the number into two sides, each side has at least one digit, and each number on both sides adds up to be equal. Please write a judgment method to detect whether it is such a number.
|
The first line of input specifies the number of use cases T; |
whether the output is a special positive integer | bool is_magicnum(int number) intput: output: |
[implementation code]:
1 #include <stdio.h> 2 #include <stdbool.h> 3 4 bool is_magicnum(int number) 5 { 6 char arr[10] = {}; 7 int j = 0; 8 int sumi = 0; 9 int sumj = 0; 10 11 while(number) 12 { 13 arr[j++] = number%10; 14 number /= 10; 15 } 16 j--; 17 18 for(int i=0; i<=j; ) 19 { 20 if(sumi >= sumj) 21 { 22 sumj += arr[j--]; 23 } 24 else if(sumi < sumj) 25 { 26 sumi += arr[i++]; 27 } 28 } 29 if(sumi == sumj) 30 { 31 return true; 32 } 33 else 34 { 35 return false; 36 } 37 } 38 39 int main() 40 { 41 int T; 42 scanf("%d",&T); 43 int num[T]; 44 for(int i=0; i < T; i++) 45 { 46 scanf("%d",&num[i]); 47 } 48 for(int i=0; i < T; i++) 49 { 50 printf("%d\n",is_magicnum(num[i])); 51 } 52 }
[Code effect]: