HDOJ1003:Max Sum——简单动态规划

Problem Description
Given a sequence a[1],a[2],a[3]……a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.

Input
The first line of the input contains an integer T(1 #include #include #include #include #include #include using namespace std; int n, s, e; int a[100002]; int maxsub(int a[]) { s = e = 1; int sum, j, b, bs, be; sum = b = a[0]; for (j = 0; j= 0 && j != 0) { b = b + a[j]; be = j + 1; } else { b = a[j]; bs = j + 1; be = j + 1; } if (b>sum) { sum = b; s = bs; e = be; } } return sum; } int main() { int t, i, k = 1, sum; scanf(“%d”, &t); while (t–) { scanf(“%d”, &n); for (i = 0; i

另一版本

#include 
using namespace std;
int *arr, first, last, sum;
void MaxSub(int *arr, int count, int &first, int &last, int ∑)
{
	//tFirst,tSum为临时的最大子序列的第一个元素与和
	int tFirst, tSum, i;
	//初始化
	first = tFirst = last = 0;
	sum = tSum = arr[0];
	//遍历该数组
	for (i = 1; i= 0)
			tSum += arr[i];
		else
		{
			tSum = arr[i];
			tFirst = i;
		}
		//如果临时的最大值大于实际的最大值,则更新子序列的first, last, sum
		if (sum > num;
	for (j = 0; j> n;
		arr = new int[n];
		for (i = 0; i> arr[i];
		MaxSub(arr, n, first, last, sum);
		if (top)
			top = false;
		else
			cout 

部分参考自网络。

HDOJ1049:Climbing Worm——基础题

Problem Description
An inch worm is at the bottom of a well n inches deep. It has enough energy to climb u inches every minute, but then has to rest a minute before climbing again. During the rest, it slips down d inches. The process of climbing and resting then repeats. How long before the worm climbs out of the well? We’ll always count a portion of a minute as a whole minute and if the worm just reaches the top of the well at the end of its climbing, we’ll assume the worm makes it out.

Input
There will be multiple problem instances. Each line will contain 3 positive integers n, u and d. These give the values mentioned in the paragraph above. Furthermore, you may assume d

像是小学算术题。没什么好说的,一次过。
放上代码:

#include
int main()
{
	int n, u, d, route, t;
	while (scanf("%d%d%d", &n, &u, &d) && n)
	{
		t = 0;
		route = n;
		while (1)
		{
			route -= u;
			t++;
			if (route 
	

HDOJ1048:The Hardest Problem Ever——字符串处理

Problem Description
Julius Caesar lived in a time of danger and intrigue. The hardest situation Caesar ever faced was keeping himself alive. In order for him to survive, he decided to create one of the first ciphers. This cipher was so incredibly sound, that no one could figure it out without knowing how it worked.
You are a sub captain of Caesar’s army. It is your job to decipher the messages sent by Caesar and provide to your general. The code is simple. For each letter in a plaintext message, you shift it five places to the right to create the secure message (i.e., if the letter is ‘A’, the cipher text would be ‘F’). Since you are creating plain text out of Caesar’s messages, you will do the opposite:

Cipher text
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Plain text
V W X Y Z A B C D E F G H I J K L M N O P Q R S T U

Only letters are shifted in this cipher. Any non-alphabetical character should remain the same, and all alphabetical characters will be upper case.

Input
Input to this problem will consist of a (non-empty) series of up to 100 data sets. Each data set will be formatted according to the following description, and there will be no blank lines separating data sets. All characters will be uppercase.

A single data set has 3 components:

Start line – A single line, “START”

Cipher message – A single line containing from one to two hundred characters, inclusive, comprising a single message from Caesar

End line – A single line, “END”

Following the final data set will be a single line, “ENDOFINPUT”.

Output
For each data set, there will be exactly one line of output. This is the original message by Caesar.

Sample Input
START
NS BFW, JAJSYX TK NRUTWYFSHJ FWJ YMJ WJXZQY TK YWNANFQ HFZXJX
END
START
N BTZQI WFYMJW GJ KNWXY NS F QNYYQJ NGJWNFS ANQQFLJ YMFS XJHTSI NS WTRJ
END
START
IFSLJW PSTBX KZQQ BJQQ YMFY HFJXFW NX RTWJ IFSLJWTZX YMFS MJ
END
ENDOFINPUT

Sample Output
IN WAR, EVENTS OF IMPORTANCE ARE THE RESULT OF TRIVIAL CAUSES
I WOULD RATHER BE FIRST IN A LITTLE IBERIAN VILLAGE THAN SECOND IN ROME
DANGER KNOWS FULL WELL THAT CAESAR IS MORE DANGEROUS THAN HE

简单的字符串处理问题,好久没有A题目了,热热身。
注意字符串的判断方式。要在赋值给char后再通过strcmp()函数比较。
读题目,搞清楚到底哪一行是明文,哪一行是密文。
没什么好说的,一次水过。
代码:

#include
#include
#include
int main()
{
	char passwd[201];
	int i, l, cache;
	while (gets(passwd))
	{
		if (strcmp(passwd, "START") == 0)
			continue;
		if (strcmp(passwd, "END") == 0)
			continue;
		if (strcmp(passwd, "ENDOFINPUT") == 0)
			break;
		l = strlen(passwd);
		for (i = 0; i = 70 && passwd[i] = 65)
			{
				passwd[i] += 21;
			}
		}
		for (i = 0; i 
	

HDOJ1008:Elevator——简单数学题

Problem Description
The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one floor, and 4 seconds to move down one floor. The elevator will stay for 5 seconds at each stop.

For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled.

Input
There are multiple test cases. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100. A test case with N = 0 denotes the end of input. This test case is not to be processed.

Output
Print the total time on a single line for each test case.

Sample Input
1 2
3 2 3 1
0

Sample Output
17
41

Author
ZHENG, Jianqiang

别人问的一道题目,没什么好说的。要注意即便是相同楼层也要停5s才行。

样例:

3 1 1 1

输出:

21

 
代码:

#include
int main()
{
	int n;
	while (~scanf("%d", &n)&&n!=0)
	{
		int f, fi,j, sum;
		f = sum = 0;
		for (j = 0; j f)
			{
				sum += 4 * (fi - f);
			}
			//if (j!=n-1)注意最后也要停五秒
				sum += 5;
		}
		printf("%dn", sum);
	}
	return 0;
}

临近期末,心浮气躁。来点水题降降火~

HDOJ5062:Beautiful Palindrome Number

Problem Description
A positive integer x can represent as (a1a2…akak…a2a1)10 or (a1a2…ak−1akak−1…a2a1)10 of a 10-based notational system, we always call x is a Palindrome Number. If it satisfies 0<a1<a2<…<ak≤9, we call x is a Beautiful Palindrome Number.
Now, we want to know how many Beautiful Palindrome Numbers are between 1 and 10N.

Input
The first line in the input file is an integer T(1≤T≤7), indicating the number of test cases.
Then T lines follow, each line represent an integer N(0≤N≤6).

Output
For each test case, output the number of Beautiful Palindrome Number.

Sample Input
2
1
6

Sample Output
9
258


要求判断范围内有多少回文串。由于范围小,可以直接打表计算。

#include
int main()
{
	int a[7]={1,9,18,54,90,174,258},t,n;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		printf("%dn",a[n]);
	}
return 0;
}

 

HDOJ2108:Shape of HDU

Problem Description
话说上回讲到海东集团推选老总的事情,最终的结果是XHD以微弱优势当选,从此以后,“徐队”的称呼逐渐被“徐总”所取代,海东集团(HDU)也算是名副其实了。
创业是需要地盘的,HDU向钱江肉丝高新技术开发区申请一块用地,很快得到了批复,据说这是因为他们公司研发的“海东牌”老鼠药科技含量很高,预期将占全球一半以上的市场。政府划拨的这块用地是一个多边形,为了描述它,我们用逆时针方向的顶点序列来表示,我们很想了解这块地的基本情况,现在请你编程判断HDU的用地是凸多边形还是凹多边形呢?

 

Input
输入包含多组测试数据,每组数据占2行,首先一行是一个整数n,表示多边形顶点的个数,然后一行是2×n个整数,表示逆时针顺序的n个顶点的坐标(xi,yi),n为0的时候结束输入。

 

Output
对于每个测试实例,如果地块的形状为凸多边形,请输出“convex”,否则输出”concave”,每个实例的输出占一行。

 

Sample Input
4
0 0 1 0 1 1 0 1
0

 

Sample Output
convex

 

采用的方法是三角函数求内角度数,判断角是否超过180.

问题:

sqrt函数中形参必须为double,但double不能用^2进行乘方,降低了代码可读性。

忘记判断最后一点与初始两点的角度。

还是WA:

#include
#include
#include
int main()
{
	int  flag,n,ni;
	double a, b, c,cc,arc;
	double num[2][10000];
	while(~scanf("%d", &n)&&n!=0)
	{
		memset(num, 0, sizeof(num));
		flag = 0;
		for (ni = 0; ni < n; ni++)
		{
			scanf("%lf%lf", &num[0][ni], &num[1][ni]);
		}
		for (ni = 2; ni < n; ni++)
		{
			a = sqrt((num[0][ni - 2] - num[0][ni - 1])*(num[0][ni - 2] - num[0][ni - 1]) + (num[1][ni - 2] - num[1][ni - 1])*(num[1][ni - 2] - num[1][ni - 1]));
			b = sqrt((num[0][ni - 1] - num[0][ni])*(num[0][ni - 1] - num[0][ni]) + (num[1][ni - 1] - num[1][ni])*(num[1][ni - 1] - num[1][ni]));
			c = sqrt((num[0][ni - 2] - num[0][ni])*(num[0][ni - 2] - num[0][ni]) + (num[1][ni - 2] - num[1][ni])*(num[1][ni - 2] - num[1][ni]));
			cc = (a*a + b*b - c*c) / (2 * a*b);
			arc = acos(cc) * 180 / 3.1415926;
			if (arc>(double)180||arc<(double)0)
			{
				flag = 1;
			}
		}
		a = sqrt((num[0][ni] - num[0][0])*(num[0][ni] - num[0][0]) + (num[1][ni] - num[1][0])*(num[1][ni] - num[1][0]));
		b = sqrt((num[0][0] - num[0][1])*(num[0][0] - num[0][1]) + (num[1][0] - num[1][1])*(num[1][0] - num[1][1]));
		c = sqrt((num[0][ni] - num[0][1])*(num[0][ni] - num[0][1]) + (num[1][ni] - num[1][1])*(num[1][ni] - num[1][1]));
		cc = (a*a + b*b - c*c) / (2 * a*b);
		arc = acos(cc) * 180 / 3.1415926;
		if (arc>(double)180 || arc<(double)0)
		{
			flag = 1;
		}
		if (flag = 0)
		{
			printf("convexn");
		}
		if (flag = 1)
		{
			printf("concaven");
		}
	}
	return 0;
}

 


更新下AC代码:

思路是判断相邻两条边的斜率,如果第二条边斜率小于第一条边,则为凹。

 

#include
#include
struct xy
{
    int x;
    int y;
};
typedef struct xy xy;
int main()
{
    xy num[10001],d,z;
    int a, b, c, cc, n, ni;
    int flag;
    while (~scanf("%d",&n)&&n!=0)
    {
        flag = 0;
        for (ni = 0; ni 

 

HDOJ2054:A==B? 字符串处理、看似水题

Problem Description
Give you two numbers A and B, if A is equal to B, you should print “YES”, or print “NO”.

 

Input
each test case contains two numbers A and B.

 

Output
for each case, if A is equal to B, you should print “YES”, or print “NO”.

 

Sample Input
1 2
2 2
3 3
4 3

 

Sample Output
NO
YES
YES
NO
主要考虑:数字中负号的处理、前导和后导零的问题;尽量开大数组。

 

#include
#include
using namespace std;
char a[100000], b[100000];
bool is(char *p)
{
	for (; *p != ' '; p++)
	if (*p == '.')return true;
	return false;
}

void det(char *p)
{
	for (; *p != ' '; p++);
	p--;
	for (; *p == '0'; p--)
		*p = ' ';
	if (*p == '.')*p = ' ';
}

int main()
{
	while (cin >> a >> b)
	{
		if (is(a))det(a);
		if (is(b))det(b);
		if (strcmp(a, b) == 0)cout 
	

HDOJ5003:Osu!——套公式的水题

Problem Description
Osu! is a famous music game that attracts a lot of people. In osu!, there is a performance scoring system, which evaluates your performance. Each song you have played will have a score. And the system will sort all you scores in descending order. After that, the i-th song scored ai will add 0.95^(i-1)*ai to your total score.

Now you are given the task to write a calculator for this system.

Input

The first line contains an integer T, denoting the number of the test cases.

For each test case, the first line contains an integer n, denoting the number of songs you have played. The second line contains n integers a1, a2, …, an separated by a single space, denoting the score of each song.

T<=20, n<=50, 1<=ai<=500.

Output

For each test case, output one line for the answer.

Your answers will be considered correct if its absolute error is smaller than 1e-5.

Sample Input

1
2
530 478

Sample Output

984.1000000000
水题,套题目描述里面的公式即可。
#include
#include
#include 
#include
#include
using namespace std;
double ai[100];
int main(){
	int n, i, j, cas;
	double res;
	scanf("%d",&cas);
	while (cas--){
		scanf("%d", &n);
		res = 0;
		for (i = 0; i= 0; i--){
			res += pow(0.95, n - 1 - i)*ai[i];
		}
		printf("%.10lfn", res);
	}
	return 0;
}

HDOJ1005:Number Sequence——递归问题

Problem Description
A number sequence is defined as follows:

f(1) = 1, f(2) = 1, f(n) = (A * f(n – 1) + B * f(n – 2)) mod 7.

Given A, B, and n, you are to calculate the value of f(n).

Input
The input consists of multiple test cases. Each test case contains 3 integers A, B and n on a single line (1 #include int main(){     long a,b,n,i,func[100];     while(scanf(“%ld%ld%ld”,&a,&b,&n) && a!=0){         func[1]=func[2]=1;         for(i=3;i48)?n%48:n]);     }     return 0; }

HDOJ2016:数据的交换输出【第二弹】

Problem Description 输入n(n<100)个数,找出其中最小的数,将它与最前面的数交换后输出这些数。

Input 输入数据有多组,每组占一行,每行的开始是一个整数n,表示这个测试实例的数值的个数,跟着就是n个整数。n=0表示输入的结束,不做处理。

Output 对于每组输入数据,输出交换后的数列,每组输出占一行。

Sample Input

4 2 1 3 4

5 5 4 3 2 1

0

Sample Output

1 2 3 4

1 4 3 2 5

 

#include
#include
int main()
{
	int n, ni;
	int num[100];
	int a, cache,ci,min;
	while (~scanf("%d", &n)&&n!=0)
	{
		memset(num, 0, sizeof(num));
		for (ni = 0; ni