我先在这里附上原题(担心你们打不开这个网址,呵呵,校园网进OJ相当快):
In these days, Kinfkong likes to send mobile phone message to his girl friend Viorith in order to promote the friendship bewteen them. But after a while she is bored with him because his behavior often interferes with her busy study. Now Viorith sets a difficult problem about sending moble phone message for him. The problem is like this : There are n students in Viorith's class and each person has a private mobile phone. At first each person knows a piece of news and all of the news is different. In order to let all the students know the n pieces of news ,they can send message to their classmate by their mobile phone. When someone sends message to another one, he must send all the news which he knows. No Matter how many pieces news one have, he can send it at one time in a piece of mobile phone message. Viorith just want to know how many times should they send at least when all the students knows all the news.
If Kinfkong can't answer this question correctly, he will lose such a pretty and smart girl friend forever. How poor Kinfkong is! Can you help him? Just have a try.^_^
(0<N<=2^31-1)
我想你们的英文水平一定比我高吧,我就不翻译了。首先我那到这个题时,读了一遍就首先想到是个(N-1)--则完全图,很快就敲了一段代码,sumbit了,是个WA(晕……),反过头来又看了一遍原题(
When someone sends message to another one, he must send all the news which he knows. No Matter how many pieces news one have, he can send it at one time in a piece of mobile phone message. )
眼前一亮,想到线性正反向遍历。1-2-3……n,先从正向1遍历到n,然后在从n处遍历1。sum=2*(n-1).
果然AC了!
参考代码(比较简单):
/**/
/*
Name:
Copyright:
Author: LonelyTree
Date: 15-08-08 21:25
Description: 1-2-3……n,先按这个顺序正着遍历一次,然后反着遍历一次。
*/
#include
<
stdio.h
>
int
main(
void
)
{
int
count;
unsigned
long
long
sum,data;
//
printf("%d",sizeof( long long));
scanf(
"
%d
"
,
&
count);
while
(count
--
)
{
scanf(
"
%llu
"
,
&
data);
sum
=
2
*
(data
-
1
);
printf(
"
%llu\n
"
,sum);
}
return
0
;
}
但是在实现我第一个想法时,出现了一个很奇怪的结果,题目限制是int的,(devcpp)
unsigned long long sum,data;(其实data最大是int型的)
我的一段代码:
scanf("%llu",&data);
sum=data*(data-(unsigned long long)1);
printf("%llu\n",sum);
可是我对data输入的是987654321是这个,应该可以得出正确结果的,为什么输出sum不对呢?
这段代码,我让网友运行了一下(vc2008)得出的正确的
我在机子上测试了unsigned long long的大小是8(64位)
希望各位网友帮我解决一下!