Java AC
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.StreamTokenizer; public class Main { /* * 1358 */ public static void main(String[] args) throws Exception { StreamTokenizer st = new StreamTokenizer(new BufferedReader( new InputStreamReader(System.in))); while (st.nextToken() != StreamTokenizer.TT_EOF) { int a = (int) st.nval; st.nextToken(); int b = (int) st.nval; a = Math.max(11, a); int count = 0; for (int i = a; i <= b; i++) { String astr = i + ""; char array[] = astr.toCharArray(); int size = array.length; int tempSum = 0; for (int j = 0; j < size; j++) { tempSum += array[j] - '0'; } if (tempSum % 2 != 0) { continue; } else { int midSum = tempSum / 2; int dp[] = new int[midSum + 1]; for (int j = 0; j < size; j++) { int num = array[j] - '0'; for (int k = midSum; k >= num; k--) { dp[k] = Math.max(dp[k], dp[k - num] + num); } } if (dp[midSum] == midSum) { count++; } } } System.out.println(count); } } } /************************************************************** Problem: 1358 User: wzqwsrf Language: Java Result: Accepted Time:860 ms Memory:87240 kb ****************************************************************/C++ AC
#include <stdio.h> #include <string.h> const int maxn = 102; const int maxLen = 10; int numArr[maxLen]; int dp[maxn]; int a, b; int i, j; int count; int max(int x, int y){ return x > y ? x : y; } bool isTrue(int num){ int tempSum = 0; int len = 0; int k = 0; while(num){ numArr[k] = num % 10; tempSum += numArr[k]; num /= 10; k++; } len = k; if(tempSum % 2 != 0){ return false; } int midSum = tempSum / 2; for (j = 0; j < len; j++) { for (k = midSum; k >= numArr[j]; k--) { dp[k] = max(dp[k], dp[k - numArr[j]] + numArr[j]); } } if (dp[midSum] == midSum) { return true; } return false; } int main(){ while(scanf("%d %d", &a, &b) != EOF){ count = 0; a = max(11, a); for(i = a; i <= b; i++){ memset(dp, 0, sizeof(dp)); if(isTrue(i)){ count++; } } printf("%d\n", count); } return 0; } /************************************************************** Problem: 1358 User: wangzhenqing Language: C++ Result: Accepted Time:160 ms Memory:1020 kb ****************************************************************/