HDU 5011 Game Nim博弈 (涉及scanf和cin效率比较)

10 篇文章 0 订阅
scanf是格式化输入,printf是格式化输出。

cin是输入流,cout是输出流。效率稍低,但书写简便。

格式化输出效率比较高,但是写代码麻烦。

流输出操作效率稍低,但书写简便。

cout之所以效率低,正如一楼所说,是先把要输出的东西存入缓冲区,再输出,导致效率降低。

缓冲区比较抽象,举个例子吧:

曾经就遇到过这样的情况(类似的),
int i;
cout<<'a';
cin>>i;
cout<<'b';
运行结果什么都没看到输出,输入一个整型比如3再按回车后ab同时显示出来了。
但是这样的情况并不是经常发生,是在一些比较大型的工程中偶尔出现,原因是字符a先到了缓冲区,但是没输出,等输入了i,b进入
缓冲区后再一并输出的。
流输入也是差不多的。

关于C++中用iostream好还是stdio.h比较好

这个问得的确不好回答了。。

iostream.h和stdio.h是C++的两个头文件,里面是对于一些类,操作符,函数的定义,头文件本身应该没有好坏之分吧~~

—————

———————————————————————————————————————————————

又查了一下,最新的G++,cin已经比scanf快了,ACM如果scanf超时的话可以用G++提交一下试一下,呵呵,不过,我以后还是打算老老实实的用scanf。。


cin的实时性较差,因为它使用了缓冲区,一般情况下满了才刷新的。
对于字符:cin的输入忽略空格和回车。scanf("%c",&i)等价于i = getchar(),换行符和回车都会被读入。

对于上述问题是深有体会,拿HDU5011 博弈题目来说,就是因为在多次循环中要到了cin导致严重超时。相反,用scanf却AC了。这里就体现饿了,scanf 的好处。。。
#include <iostream>
#include <algorithm>
#define ss(a)  scanf("%d",&a)
#define ss64(a)  scanf("%I64d",&a)
using namespace std;

int main()
{
	int n,m,i,j;
	int sum,v;
	while (cin>>n)
	{
		sum=0;
        while(n--)
        {
			//scanf("%d",&m);  //用cin>>m  竟然超时了。看来scanf比cin在处理大数据的时候更占优势啊
			ss(m);
			sum ^= m;
        }
		if (sum)
		{
			cout<<"Win"<<endl;
		}
		else
			cout<<"Lose"<<endl;
	}
    return 0;
}
简单博弈之NIM博弈
对于nim游戏的某个位置(x1,x2,x3),当且仅当它各部分的nim-sum等于0时(即x1⊕x2⊕x3=0),则当前位于必败点。

Game

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 692    Accepted Submission(s): 471


Problem Description
Here is a game for two players. The rule of the game is described below: 

● In the beginning of the game, there are a lot of piles of beads.

● Players take turns to play. Each turn, player choose a pile i and remove some (at least one) beads from it. Then he could do nothing or split pile i into two piles with a beads and b beads.(a,b > 0 and a + b equals to the number of beads of pile i after removing) 

● If after a player's turn, there is no beads left, the player is the winner.

Suppose that the two players are all very clever and they will use optimal game strategies. Your job is to tell whether the player who plays first can win the game.
 

Input
There are multiple test cases. Please process till EOF.

For each test case, the first line contains a postive integer n(n < 10 5) means there are n piles of beads. The next line contains n postive integer, the i-th postive integer a i(a i < 2 31) means there are a i beads in the i-th pile.
 

Output
For each test case, if the first player can win the game, ouput "Win" and if he can't, ouput "Lose"
 

Sample Input
    
    
1 1 2 1 1 3 1 2 3
 

Sample Output
    
    
Win Lose Lose

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值