HDOJ1009:FatMouse' Trade——简单贪心

Problem Description
FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean.
The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain.

Input
The input consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then N lines follow, each contains two non-negative integers J[i] and F[i] respectively. The last test case is followed by two -1’s. All integers are not greater than 1000.

Output
For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain.

Sample Input
5 3
7 2
4 3
5 2
20 3
25 18
24 15
15 10
-1 -1

Sample Output
13.333
31.500

简单贪心问题。先利用结构体对J和F进行排序。然后依次累加即可。

#include 
#include 
#include 
using namespace std;

struct Room
{
	int j, f;
};

Room room[1005];

bool cmp(const Room a, const Room b)
{
	return ((double)a.j / a.f > (double)b.j / b.f);
}

int main()
{
	int m, n;
	while (cin >> m >> n && m!=-1 && n!=-1)
	{
		int i;
		for (i = 0; i > room[i].j >> room[i].f;
		}
		sort(room , room + n, cmp);
		int jsum=0, fsum = 0;
		for (i = 0; i = m) { break; }
		}
		cout  m)
		{
			fsum -= room[i].f;
			jsum -= room[i].j;
			double javasum;
			javasum = jsum+(double)room[i].j *(double(m - fsum) / room[i].f);
			cout 
	

HDOJ1205:吃糖果——简单数学题

Problem Description
HOHO,终于从Speakless手上赢走了所有的糖果,是Gardon吃糖果时有个特殊的癖好,就是不喜欢将一样的糖果放在一起吃,喜欢先吃一种,下一次吃另一种,这样;可是Gardon不知道是否存在一种吃糖果的顺序使得他能把所有糖果都吃完?请你写个程序帮忙计算一下。

Input
第一行有一个整数T,接下来T组数据,每组数据占2行,第一行是一个整数N(0注意大数组要放在main()函数之外,否则RE。sum 必须用_int64开才行,long long int 会导致Runtime Error(OJ傲娇?!)
sort函数逆序排列的方法就是先定义一个比较函数cmp。然后直接在sort中使用~
悲剧的RE了无数次……
AC代码:

#include
#include
#include
using namespace std;
int cdy[1000001];
bool cmp(int a, int b)
{
	return a>b;
}
int main()
{
	int n, i,num;
	_int64 sum;
	scanf("%d", &n);
	while (n--)
	{
		sum = 0;
		scanf("%d", &num);
		for (i = 0; isum+1)
		{
			printf("Non");
		}
		else
		{
			printf("Yesn");
		}
	}
	return 0;
}

HDOJ:韩信点兵

Problem Description
相传古代有位将军清点军队人数时有妙招,不直接数人数,而是让士兵们按照3人一队排,再5人一队排,再7人一队排,每次变换都记下排尾人数,最后得出总人数N(10 #include int main() {     int i, a, b, c;     while (~scanf(“%d%d%d”, &a, &b, &c))     {         if (a == 3)             a = 0;         if (b == 5)             b = 0;         if (c == 7)             c = 0;         for (i = 10; i

HDOJ:数圈圈

Problem Description
传说中有一题小学生题,难倒了一万个大学生:
7111=0 5555=0
8809=6 8193=3
2171=0 8096=5
……
2889=?
这题居然是数圈圈!!数圈圈有木有啊数圈圈。
我也是醉了。。
好吧。今天我们就来数圈圈吧!
题目很简单 给你一个数 告诉我有多少个圈圈。来吧!
(注意:4是没有圈圈的!)
Input
输入数据有多个,每个占一行,每行一个整数,保证每个数不会大于5位,没有小数。
Output
对于每个输入数据,输出一行,有多少个圈圈。
Sample Input
7111
8809
2171
2889
Sample Output
0
6
0
5
考虑前导0(例如0001)、只有0和负数的情况。也可以用字符串做~

#include
#include
int main()
{
    int num,t,tl,aws;
    while (~scanf("%d", &num))
    {
        
        aws = 0;
        if (num 
	

HDOJ1425:sort——排序

Problem Description
给你n个整数,请按从大到小的顺序输出其中前m大的数。

Input
每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000),第二行包含n个各不相同,且都处于区间[-500000,500000]的整数。

Output
对每组测试数据按从大到小的顺序输出前m大的数。

Sample Input
5 3
3 -35 92 213 -644

Sample Output
213 92 3

Hint
Hint

请用VC/VC++提交

排序问题,注意算法复杂度。否则容易TLE超时……据说也可以用堆排序。

#include 
#include 
#include 
using namespace std;
int a[1000002];
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        memset(a,0,sizeof(a));
        int i,j;
        for(i=1;i1;m--,i--)
            printf(" %d",a[i]);
        printf("n");
    }
    return 0;
}

HDOJ1061:Rightmost Digit——简单数学题

Problem Description
Given a positive integer N, you should output the most right digit of N^N.

Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single positive integer N(1 #include using namespace std; int main(){ int n,m; int a[10][4] = {{0,0,0,0},{1,1,1,1},{2,4,8,6},{3,9,7,1},{4,6,4,6},{5,5,5,5},{6,6,6,6},{7,9,3,1},{8,4,2,6},{9,1,9,1}}; cin>>m; while(m–) cin>>n,cout=0?n%4-1:3]

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