HDOJ2046:骨牌铺方格——递推问题

Problem Description
在2×n的一个长方形方格中,用一个1× 2的骨牌铺满方格,输入n ,输出铺放方案的总数.
例如n=3时,为2× 3方格,骨牌的铺放方案有三种,如下图:

Input
输入数据由多行组成,每行包含一个整数n,表示该测试实例的长方形方格的规格是2×n (0<n<=50)。

Output
对于每个测试实例,请输出铺放方案的总数,每个实例的输出占一行。

Sample Input
1
3
2

Sample Output
1
3
2

斐波那契数列解决,注意输出要用int64才行,否则WA。不要问我怎么知道的……

代码:

#include
#include
using namespace std;
int main()
{
	_int64 fibn[51] = {1,2,3};
	int i,j;
	while (~scanf("%d", &i))
	{
		for (j = 3; j 
	

HDOJ1274:展开字符串——字符串处理

Problem Description
在纺织CAD系统开发过程中,经常会遇到纱线排列的问题。
该问题的描述是这样的:常用纱线的品种一般不会超过25种,所以分别可以用小写字母表示不同的纱线,例如:abc表示三根纱线的排列;重复可以用数字和括号表示,例如:2(abc)表示abcabc;1(a)=1a表示a;2ab表示aab;如果括号前面没有表示重复的数字出现,则就可认为是1被省略了,如:cd(abc)=cd1(abc)=cdabc;这种表示方法非常简单紧凑,也易于理解;但是计算机却不能理解。为了使计算机接受,就必须将简单紧凑的表达方式展开。某ACM队接受了此项任务。现在你就是该ACM队的一员,请你把这个程序编写完成。
已知条件:输入的简单紧凑表达方式的长度不超过250个字符;括号前表示重复的数不超过1000;不会出现除了数字、括号、小写字母以外的任何其他字符;不会出现括号不配对等错误的情况(错误处理已由ACM其他队员完成了)。

Input
本题有多个测试数据组,第一行输入的就是数据组数N,接着就是N行表达式,表达式是按照前面介绍的意义书写的。

Output
输出时含有N行,每行对应一个输入的表达式。

Sample Input
2
1(1a2b1(ab)1c)
3(ab2(4ab))

Sample Output
abbabc
abaaaabaaaababaaaabaaaababaaaabaaaab

栈问题。
代码:

#include
#include
#include
using namespace std;
string process(string s)
{
	string ans;
	stack x;
	for(int i=0;i='0'&&s[i]='a'&&s[i]='0')
			{
				int num=x.top()-'0';
				x.pop();
				while(num--)
					x.push(s[i]);
			}
			else
				x.push(s[i]);
		}
		else if(s[i]==')')
		{
			string temp;
			while(x.top()!='(')
			{
				temp.insert(temp.begin(),x.top());
				x.pop();
			}
			x.pop();
			int num;
			if(x.empty()||!(x.top()>='0'&&x.top()>test;
	while(test--)
	{
		cin>>s;
		cout
	

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]