HDOJ5302:Connect the Graph——无向图构造

Problem Description

Once there was a special graph. This graph had n vertices and some edges. Each edge was either white or black. There was no edge connecting one vertex and the vertex itself. There was no two edges connecting the same pair of vertices. It is special because the each vertex is connected to at most two black edges and at most two white edges.

One day, the demon broke this graph by copying all the vertices and in one copy of the graph, the demon only keeps all the black edges, and in the other copy of the graph, the demon keeps all the white edges. Now people only knows there are w0 vertices which are connected with no white edges, w1 vertices which are connected with 1 white edges, w2 vertices which are connected with 2 white edges, b0 vertices which are connected with no black edges, b1 vertices which are connected with 1 black edges and b2 vertices which are connected with 2 black edges.

The precious graph should be fixed to guide people, so some people started to fix it. If multiple initial states satisfy the restriction described above, print any of them.

Input

The first line of the input is a single integer T (T≤700), indicating the number of testcases.

Each of the following T lines contains w0,w1,w2,b0,b1,b2. It is guaranteed that 1≤w0,w1,w2,b0,b1,b2≤2000 and b0+b1+b2=w0+w1+w2.

It is also guaranteed that the sum of all the numbers in the input file is less than 300000.

Output

For each testcase, if there is no available solution, print −1. Otherwise, print m in the first line, indicating the total number of edges. Each of the next m lines contains three integers x,y,t, which means there is an edge colored t connecting vertices x and y. t=0 means this edge white, and t=1 means this edge is black. Please be aware that this graph has no self-loop and no multiple edges. Please make sure that 1≤x,y≤b0+b1+b2.

Sample Input

2
1 1 1 1 1 1
1 2 2 1 2 2

Sample Output

-1
6
1 5 0
4 5 0
2 4 0
1 4 1
1 3 1
2 3 1

 

题目大意是构造一个无向图。先考虑白色边,构造常链连接1和2即可。注意排重。

参考代码:

 

#include
#include
#include
#include
#include
using namespace std;
#define sf scanf
int T;
int a[5],b[5];
int d[1000005];
int main()
{
    sf("%d",&T);
    while(T--)
    {
        for(int i = 0;i
	

HDOJ1085:Holding Bin-Laden Captive!——母函数

Problem Description
We all know that Bin-Laden is a notorious terrorist, and he has disappeared for a long time. But recently, it is reported that he hides in Hang Zhou of China!
“Oh, God! How terrible! ”

Don’t be so afraid, guys. Although he hides in a cave of Hang Zhou, he dares not to go out. Laden is so bored recent years that he fling himself into some math problems, and he said that if anyone can solve his problem, he will give himself up!
Ha-ha! Obviously, Laden is too proud of his intelligence! But, what is his problem?
“Given some Chinese Coins (硬币) (three kinds– 1, 2, 5), and their number is num_1, num_2 and num_5 respectively, please output the minimum value that you cannot pay with given coins.”
You, super ACMer, should solve the problem easily, and don’t forget to take $25000000 from Bush!

Input
Input contains multiple test cases. Each test case contains 3 positive integers num_1, num_2 and num_5 (0<=num_i<=1000). A test case containing 0 0 0 terminates the input and this test case is not to be processed.

Output
Output the minimum positive value that one cannot pay with given coins, one line for one case.

Sample Input
1 1 3
0 0 0

Sample Output
4

 

参考代码:

#include
#include
#include
int c1[1000],c2[10000];
int main()
{
	int n1,n2,n5;
	while(scanf("%d%d%d",&n1,&n2,&n5)==3,n1+n2+n5)
	{
		int n,i,j,k;
		n=n1+n2*2+n5*5;
		for(i=0;i

 

HDOJ1038:Biker’s Trip Odometer——数学题

Problem Description
Most bicycle speedometers work by using a Hall Effect sensor fastened to the front fork of the bicycle. A magnet is attached to one of the spokes on the front wheel so that it will line up with the Hall Effect switch once per revolution of the wheel. The speedometer monitors the sensor to count wheel revolutions. If the diameter of the wheel is known, the distance traveled can be easily be calculated if you know how many revolutions the wheel has made. In addition, if the time it takes to complete the revolutions is known, the average speed can also be calculated.
For this problem, you will write a program to determine the total distance traveled (in miles) and the average speed (in Miles Per Hour) given the wheel diameter, the number of revolutions and the total time of the trip. You can assume that the front wheel never leaves the ground, and there is no slipping or skidding.

Input
Input consists of multiple datasets, one per line, of the form:

diameter revolutions time

The diameter is expressed in inches as a floating point value. The revolutions is an integer value. The time is expressed in seconds as a floating point value. Input ends when the value of revolutions is 0 (zero).

Output
For each data set, print:

Trip #N: distance MPH

Of course N should be replaced by the data set number, distance by the total distance in miles (accurate to 2 decimal places) and MPH by the speed in miles per hour (accurate to 2 decimal places). Your program should not generate any output for the ending case when revolutions is 0.

Constants

For p use the value: 3.1415927.
There are 5280 feet in a mile.
There are 12 inches in a foot.
There are 60 minutes in an hour.
There are 60 seconds in a minute.
There are 201.168 meters in a furlong.

Sample Input
26 1000 5
27.25 873234 3000
26 0 1000

Sample Output
Trip #1: 1.29 928.20
Trip #2: 1179.86 1415.84

一道数学题。简单水过。注意格式即可。
代码:

#include
#define Pi 3.1415927

int main()
{
	int n,c=0;
	double t,d,v,s;
	while(scanf("%lf%d%lf",&d,&n,&t)!=EOF && n!=0)
	{
		s=n*Pi*d/12/5280;
		v=s/(t/60/60);
		printf("Trip #%d: %.2lf %.2lf\n",++c,s,v);
	}
		return 0;
}

HDOJ2127:Polish notation——表达式转换

Problem Description
Reverse Polish notation (RPN) is a method for representing expressions in which the operator symbol is placed after the arguments being operated on.
Polish notation, in which the operator comes before the operands, was invented in the 1920s by the Polish mathematician Jan Lucasiewicz.
In the late 1950s, Australian philosopher and computer scientist Charles L. Hamblin suggested placing the operator after the operands and hence created reverse polish notation.

RPN has the property that brackets are not required to represent the order of evaluation or grouping of the terms.
RPN expressions are simply evaluated from left to right and this greatly simplifies the computation of the expression within computer programs.
As an example, the arithmetic expression (3+4)*5 can be expressed in RPN as 3 4 + 5 *.

Reverse Polish notation, also known as postfix notation, contrasts with the infix notation of standard arithmetic expressions in which the operator symbol appears between the operands. So Polish notation just as prefix notation.

Now, give you a string of standard arithmetic expressions, please tell me the Polish notation and the value of expressions.

Input
There’re have multi-case. Every case put in one line, the expressions just contain some positive integers(all less than 100, the number of integers less than 20), bi-operand operators(only have 3 kinds : +,-,*) and some brackets'(‘,’)’.
you can assume the expressions was valid.

Output
Each case output the Polish notation in first line, and the result of expressions was output in second line.
all of the answers are no any spaces and blank line.the answer will be not exceed the 64-signed integer.

Sample Input
1+2-3*(4-5)
1+2*(3-4)-5*6

Sample Output
Case 1:
– + 1 2 * 3 – 4 5
6
Case 2:
– + 1 * 2 – 3 4 * 5 6
-31

题目考察表达式的转换将中缀表达式转换成前置表达式(波兰式),并计算出结果。可以直接套模板……需要注意表达式中空格的位置;字符串需要用__int64存贮。

关于前置表达式和后置表达式的转换方法可以参考这篇博文:波兰式、逆波兰式与表达式求值

参考代码:

#include 
#include 
#include 
using namespace std;

char stack[500];                    
int top;                        //栈顶指针       
char output[500], input[500];        
int outLen;

int priority(char op)           //定义运算符优先级     
{
    if (op=='+' || op=='-')
        return 1;
    if (op=='*' || op=='/')
        return 2;
    else
        return 0;
}

bool isOperator(char op)                
{
    return (op=='+' || op=='-' || op=='*' || op=='/');
}

void Polish(char *s,int len)            
{
    memset(output,'\0',sizeof output);    
    outLen = 0;
    for (int i=len-1; i >= 0; --i)        
    {
        if (isdigit(s[i]))                
        {
            output[outLen++] = s[i];    
            while (i-1 >= 0 && isdigit(s[i-1]))
            {
                output[outLen++] = s[i-1];
                --i;
            }
            output[outLen++] = ' ';        
        }
        if (s[i]==')')                    
        {
            ++top;
            stack[top] = s[i];
        }
        while (isOperator(s[i]))        
        {                                                
            if (top==0 || stack[top]==')' || priority(s[i]) >= priority(stack[top])) 
            {
                ++top;
                stack[top] = s[i];
                break;
            }
            else
            {
                output[outLen++] = stack[top];
                output[outLen++] = ' ';
                --top;
            }
        }
        if (s[i]=='(')                    
        {
            while (stack[top]!=')')
            {
                output[outLen++] = stack[top];
                output[outLen++] = ' ';
                --top;
            }
            --top;    
        }
    }
    while (top!=0)                        
    {
        output[outLen++] = stack[top];
        output[outLen++] = ' ';
        --top;
    }
}

char DstBuf[200];
char* OP(char* op1,char* op2,char op)
{
    __int64 res = 0;
    if (op=='+')
        res = _atoi64(op1) + _atoi64(op2);
    else if (op=='-')
        res = _atoi64(op1) - _atoi64(op2);
    else if (op=='*')
        res = _atoi64(op1) * _atoi64(op2);
    else if (op=='/')
        res = _atoi64(op1) / _atoi64(op2);
    _i64toa(res,DstBuf,10);
    return DstBuf;
}

char cSt1[200][80], cSt2[200][80];
__int64 calc(char *s)                
{
    int top1=0, top2=0, i;
    for (i=0; s[i]; ++i)
    {
        if (s[i] && s[i] != ' ')
        {
            ++top1;
            sscanf(s+i,"%s",cSt1[top1]);        
            while (s[i] && s[i] != ' ')
                ++i;
        }
    }
    while (top1 != 0)    
    {
        if (!isdigit(cSt1[top1][0]))    
        {
            OP(cSt2[top2], cSt2[top2-1], cSt1[top1][0]);
            memcpy(cSt2[top2-1],DstBuf,sizeof DstBuf);
            --top2;                    
            --top1;                        
        }
        else
        {
            ++top2;                        
            memcpy(cSt2[top2],cSt1[top1],sizeof cSt1[top1]);
            --top1;                        
        }
    }
    return _atoi64(cSt2[1]);
}
int main()
{
    int T = 1;
    while (gets(input))
    {
        Polish(input, strlen(input));
        reverse(output,output+outLen-1);
        output[outLen-1] = '\0';
        printf("Case %d:\n%s\n",T++,output);
        printf("%I64d\n",calc(output));//注意要__int64
    }
    return 0;
}

HDOJ1042:N!–大数

Problem Description
Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!

Input
One N in one line, process to the end of file.

Output
For each N, output N! in one line.

Sample Input
1
2
3

Sample Output
1
2
6

要求计算10000以内的阶乘,典型的大数问题.这里用的是 五位一存的方法. 关于X位一存的解释可以看这篇文章. 注意特殊情况(0,1)下的处理.

#include 
#include 
#define MOD 100000
int main()
{
    int a[8000];    
    int i,k,n,t,la;
    while(~scanf("%d",&n))
    {
        if(n==0||n==1) {printf("1\n");continue;}

        a[0] = 1; 
        la = 1;
        t = 0;
        for(k=2; k 0)   
            {    
                a[la++] = t;
                t = 0; 
            } 
        }
        printf("%d",a[la-1]);
        for(i=la-2; i>=0; i--)
            printf("%05d",a[i]);
        printf("\n");
    }
    return 0;
}

HDOJ1052:Tian Ji — The Horse Racing–简单贪心

Here is a famous story in Chinese history.

“That was about 2300 years ago. General Tian Ji was a high official in the country Qi. He likes to play horse racing with the king and others.”

“Both of Tian and the king have three horses in different classes, namely, regular, plus, and super. The rule is to have three rounds in a match; each of the horses must be used in one round. The winner of a single round takes two hundred silver dollars from the loser.”

“Being the most powerful man in the country, the king has so nice horses that in each class his horse is better than Tian’s. As a result, each time the king takes six hundred silver dollars from Tian.”

“Tian Ji was not happy about that, until he met Sun Bin, one of the most famous generals in Chinese history. Using a little trick due to Sun, Tian Ji brought home two hundred silver dollars and such a grace in the next match.”

“It was a rather simple trick. Using his regular class horse race against the super class from the king, they will certainly lose that round. But then his plus beat the king’s regular, and his super beat the king’s plus. What a simple trick. And how do you think of Tian Ji, the high ranked official in China?”

Were Tian Ji lives in nowadays, he will certainly laugh at himself. Even more, were he sitting in the ACM contest right now, he may discover that the horse racing problem can be simply viewed as finding the maximum matching in a bipartite graph. Draw Tian’s horses on one side, and the king’s horses on the other. Whenever one of Tian’s horses can beat one from the king, we draw an edge between them, meaning we wish to establish this pair. Then, the problem of winning as many rounds as possible is just to find the maximum matching in this graph. If there are ties, the problem becomes more complicated, he needs to assign weights 0, 1, or -1 to all the possible edges, and find a maximum weighted perfect matching…

However, the horse racing problem is a very special case of bipartite matching. The graph is decided by the speed of the horses — a vertex of higher speed always beat a vertex of lower speed. In this case, the weighted bipartite matching algorithm is a too advanced tool to deal with the problem.

In this problem, you are asked to write a program to solve this special case of matching problem.

Input
The input consists of up to 50 test cases. Each case starts with a positive integer n (n 田忌最快的马比齐王最快的快–直接跑赢;

  • 田忌最快的马比齐王最快的慢–用田忌最慢的马去跟齐王的马跑;
  • 田忌最快的马跟齐王最快的一样快–再比较田忌最慢的马是否比齐王的最慢的马快,如果是就直接跑赢,如果不是就用田忌最慢的马去跟齐王最快的马跑.
  • 放上代码:

    #include
    #include
    #include
    int cmp(const void*a ,const void*b)
    {
    	return *(int *)b-*(int *)a;
    }
    int main()
    {
    	int i,n;
    	int tian,king;
    	int fa,fb,la,lb;
    	while(~scanf("%d",&n)&&n!=0)
    	{
    		int a[1001]={0},b[1001]={0};
    		getchar();
    		tian=king=0;
    		for(i=0;ib[fb])
    			{
    				tian++;
    				fa++;
    				fb++;
    			}
    			else if(a[la]>b[lb])
    			{
    				tian++;
    				la--;
    				lb--;
    			}
    			else if(a[la]b[fb])
    			tian++;
    		else if(a[fa]
    	

    HDOJ5131:Song Jiang’s rank list

    Problem Description
    《Shui Hu Zhuan》,also 《Water Margin》was written by Shi Nai’an — an writer of Yuan and Ming dynasty. 《Shui Hu Zhuan》is one of the Four Great Classical Novels of Chinese literature. It tells a story about 108 outlaws. They came from different backgrounds (including scholars, fishermen, imperial drill instructors etc.), and all of them eventually came to occupy Mout Liang(or Liangshan Marsh) and elected Song Jiang as their leader.

    In order to encourage his military officers, Song Jiang always made a rank list after every battle. In the rank list, all 108 outlaws were ranked by the number of enemies he/she killed in the battle. The more enemies one killed, one’s rank is higher. If two outlaws killed the same number of enemies, the one whose name is smaller in alphabet order had higher rank. Now please help Song Jiang to make the rank list and answer some queries based on the rank list.

    Input
    There are no more than 20 test cases.

    For each test case:

    The first line is an integer N (0<n&<200), indicating that there are N outlaws.

    Then N lines follow. Each line contains a string S and an integer K(0<k<300), meaning an outlaw’s name and the number of enemies he/she had killed. A name consists only letters, and its length is between 1 and 50(inclusive). Every name is unique.

    The next line is an integer M (0<m<200) ,indicating that there are M queries.

    Then M queries follow. Each query is a line containing an outlaw’s name.
    The input ends with n = 0

    Output
    For each test case, print the rank list first. For this part in the output ,each line contains an outlaw’s name and the number of enemies he killed.

    Then, for each name in the query of the input, print the outlaw’s rank. Each outlaw had a major rank and a minor rank. One’s major rank is one plus the number of outlaws who killed more enemies than him/her did.One’s minor rank is one plus the number of outlaws who killed the same number of enemies as he/she did but whose name is smaller in alphabet order than his/hers. For each query, if the minor rank is 1, then print the major rank only. Or else Print the major rank, blank , and then the minor rank. It’s guaranteed that each query has an answer for it.

    Sample Input
    5
    WuSong 12
    LuZhishen 12
    SongJiang 13
    LuJunyi 1
    HuaRong 15
    5
    WuSong
    LuJunyi
    LuZhishen
    HuaRong
    SongJiang
    0

    Sample Output
    HuaRong 15
    SongJiang 13
    LuZhishen 12
    WuSong 12
    LuJunyi 1
    3 2
    5
    3
    1
    2

    简单排序,直接上代码:

    #include
    #include
    #include
    #include
    using namespace std;
    #define N 205
    
    struct hero_t{
        string s;
        int i, n;
    } heros[N];
    
    struct node_t {
        int mmin, mmax;
        node_t() {}
        node_t(int a, int b) {
            mmin = a; mmax = b;
        }
    } node;
    
    //struct heros[N];
    
    bool comp(hero_t a, hero_t b) {
        if (a.n == b.n)
            return a.s  b.n;
    }
    
    int main() {
        int n, m;
        int i, j, k, id = 0;
        string s;
        node_t nd;
    
        while (cin>>n && n) {
            for (i=0; i>heros[i].s>>heros[i].n;
            }
    
            sort(heros, heros+n, comp);
            for (i=0; i tb;
            i = 0;
            while (i  1) {
                    for (k=0; k>m;
            while (m--) {
                cin >>s;
                nd = tb[s];
                if (nd.mmin == 1)
                    cout