1、Java AC
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.StreamTokenizer; public class Main { /* * 1527 */ 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 n = (int) st.nval; int len = 2 * n; int array[] = new int[len]; for (int i = 0; i < n; i++) { st.nextToken(); array[i] = (int) st.nval; array[i + n] = array[i]; } int sum = 0; for (int i = 0; i < (i + n) && (i + n) <= len; i++) { int tempSum = array[i]; if (tempSum > sum) { sum = tempSum; } for (int j = i + 1; j < (i + n); j++) { if (tempSum < 0) { break; } else { tempSum += array[j]; } if (tempSum > sum) { sum = tempSum; } } } System.out.println(sum); } } } /************************************************************** Problem: 1527 User: wangzhenqing Language: Java Result: Accepted Time:1110 ms Memory:25440 kb ****************************************************************/1、C++ AC
#include <stdio.h> const int maxn = 200002; int n,i; int array[maxn]; int main(){ while(scanf("%d",&n) != EOF){ int len = 2*n; for(i = 0; i < n; i++){ scanf("%d",&array[i]); array[i+n] = array[i]; } int sum = 0; for (i = 0; i < (i + n) && (i + n) <= len; i++) { int tempSum = array[i]; if (tempSum > sum) { sum = tempSum; } for (int j = i+1; j < (i + n ); j++) { if (tempSum < 0) { break; }else { tempSum += array[j]; } if (tempSum > sum) { sum = tempSum; } } } printf("%d\n",sum); } return 0; } /************************************************************** Problem: 1527 User: wzqwsrf Language: C++ Result: Accepted Time:220 ms Memory:1800 kb ****************************************************************/
2、Java AC
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.StreamTokenizer; public class Main { /* * 1527 */ 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 n = (int) st.nval; int array[] = new int[n]; int sum = 0; st.nextToken(); array[0] = (int) st.nval; sum += array[0]; int maxValue = array[0] > 0 ? array[0] : 0; int minValue = array[0] < 0 ? array[0] : 0; int max = maxValue; int min = minValue; for (int i = 1; i < n; i++) { st.nextToken(); array[i] = (int) st.nval; if (maxValue > 0) { maxValue += array[i]; }else { maxValue = array[i]; } if (minValue < 0) { minValue += array[i]; }else { minValue = array[i]; } max = maxValue > max ? maxValue : max; min = minValue < min ? minValue : min; sum += array[i]; } int temp = sum - min; System.out.println(max > temp ? max : temp); } } } /************************************************************** Problem: 1527 User: wangzhenqing Language: Java Result: Accepted Time:860 ms Memory:28876 kb ****************************************************************/2、C++ AC
#include <stdio.h> const int maxn = 100002; int n,i; int array[maxn]; int main(){ while(scanf("%d",&n) != EOF){ scanf("%d",&array[0]); int sum = 0; sum += array[0]; int maxValue = array[0] > 0 ? array[0] : 0; int minValue = array[0] < 0 ? array[0] : 0; int max = maxValue; int min = minValue; for(i = 1; i < n; i++){ scanf("%d",&array[i]); if (maxValue > 0) { maxValue += array[i]; }else { maxValue = array[i]; } if (minValue < 0) { minValue += array[i]; }else { minValue = array[i]; } max = maxValue > max ? maxValue : max; min = minValue < min ? minValue : min; sum += array[i]; } int temp = sum - min; printf("%d\n",max > temp ? max : temp); } return 0; } /************************************************************** Problem: 1527 User: wzqwsrf Language: C++ Result: Accepted Time:70 ms Memory:1412 kb ****************************************************************/2、 题目1537:买卖股票 时间限制:1 秒内存限制:128 兆特殊判题:否提交:461解决:117
Java AC
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.StreamTokenizer; public class Main { /* * 1537 */ 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 n = (int) st.nval; st.nextToken(); int k = (int) st.nval; int array[] = new int[n+1]; for (int i = 1; i < n+1; i++) { st.nextToken(); array[i] = (int) st.nval; } int size = 1002; int dp[][] = new int[size][size]; int result = 0; int tempValue = Integer.MIN_VALUE; for (int i = 1; i < k+1; i++) { tempValue = dp[i-1][1] - array[1]; for (int j = 2; j < n+1; j++) { dp[i][j] = Math.max(tempValue+array[j], dp[i][j-1]); result = Math.max(dp[i][j], result); tempValue = Math.max(tempValue, dp[i-1][j]-array[j]); } } System.out.println(result); } } } /************************************************************** Problem: 1537 User: wangzhenqing Language: Java Result: Accepted Time:600 ms Memory:71720 kb ****************************************************************/C++ AC
#include <stdio.h> #include <string.h> const int maxn = 1002; int array[maxn]; int dp[maxn][maxn]; int max(int a, int b){ return a > b ? a : b; } int main(){ int n,k,i; while(scanf("%d%d",&n,&k)!=EOF){ for(i = 1; i < n+1; i++){ scanf("%d",&array[i]); } memset(dp,0,sizeof(dp)); int result = 0; int tempValue = 99999999; for (i = 1; i < k+1; i++) { tempValue = dp[i-1][1] - array[1]; for (int j = 2; j < n+1; j++) { dp[i][j] = max(tempValue+array[j], dp[i][j-1]); result = max(dp[i][j], result); tempValue = max(tempValue, dp[i-1][j]-array[j]); } } printf("%d\n", result); } return 0; } /************************************************************** Problem: 1537 User: wzqwsrf Language: C++ Result: Accepted Time:90 ms Memory:4948 kb ****************************************************************/3、题目1551:切蛋糕 时间限制:1 秒内存限制:128 兆特殊判题:否提交:211解决:59
有如下图半价为R的圆形蛋糕,被切一刀后(图中红色直线),分成两个部分(黄色和绿色),已知其比例为r,求刀痕长度(图中红色直线)。
输入:Java AC
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.StreamTokenizer; public class Main { /* * 2014年3月9日 22:55:47 */ public static void main(String[] args) throws IOException { StreamTokenizer st = new StreamTokenizer(new BufferedReader( new InputStreamReader(System.in))); while (st.nextToken() != StreamTokenizer.TT_EOF) { int R = (int) st.nval; st.nextToken(); double r = st.nval; double low = 0.0; double high = 2 * R; double mid = 0; while ((high - low) >= 0.0001) { mid = (low + high) / 2; if (countArea(R, mid) > r) { high = mid; } else { low = mid; } } System.out.printf("%.2f\n", mid); } } public static double countArea(double R, double mid) { double du = Math.asin(mid / (2 * R)); double l = R * R * du - 0.5 * mid * Math.sqrt(R * R - 0.25 * mid * mid); double r = R * R * Math.PI - l; return l / r; } } /************************************************************** Problem: 1551 User: wzqwsrf Language: Java Result: Accepted Time:380 ms Memory:15160 kb ****************************************************************/C++ AC
#include <stdio.h> #include <math.h> #define PI 3.1415926 double countArea(double R, double mid) { double du = asin(mid / (2 * R)); double l = R * R * du - 0.5 * mid * sqrt(R * R - 0.25 * mid * mid); double r = R * R * PI - l; return l / r; } int main(){ int R; double r; while(scanf("%d%lf",&R,&r)!=EOF){ double low = 0.0; double high = 2 * R; double mid = 0; while ((high - low) >= 0.0001) { mid = (low + high) / 2; if (countArea(R, mid) > r) { high = mid; } else { low = mid; } } printf("%.2lf\n", mid); } return 0; } /************************************************************** Problem: 1551 User: wzqwsrf Language: C++ Result: Accepted Time:0 ms Memory:1100 kb ****************************************************************/4、题目1552:座位问题 时间限制:1 秒内存限制:128 兆特殊判题:否提交:208解决:51
Java AC
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.StreamTokenizer; public class Main { /* * 2014年3月9日 21:05:41 */ public static void main(String[] args) throws IOException { StreamTokenizer st = new StreamTokenizer(new BufferedReader( new InputStreamReader(System.in))); while (st.nextToken() != StreamTokenizer.TT_EOF) { int n = (int) st.nval; long dp[] = new long[1002]; int mod = 1000000007; dp[1] = 1; dp[2] = 2; dp[3] = 4; dp[4] = 7; for (int i = 5; i <= n; i++) { dp[i] = (dp[i-1] + dp[i-2] + dp[i-4]) % mod; } System.out.println(dp[n]); } } } /************************************************************** Problem: 1552 User: wzqwsrf Language: Java Result: Accepted Time:440 ms Memory:17796 kb ****************************************************************/
C++ AC
#include <stdio.h> #include <string.h> const int maxn = 1002; long dp[maxn]; int main(){ int mod = 1000000007; dp[1] = 1; dp[2] = 2; dp[3] = 4; dp[4] = 7; for (int i = 5; i <= 1000; i++) { dp[i] = (dp[i-1] + dp[i-2] + dp[i-4]) % mod; } int n; while(scanf("%d",&n)!=EOF){ printf("%d\n", dp[n]); } return 0; } /************************************************************** Problem: 1552 User: wzqwsrf Language: C++ Result: Accepted Time:0 ms Memory:1028 kb ****************************************************************/