注意64位数


Flying to the Mars

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5486    Accepted Submission(s): 1809


Problem Description

In the year 8888, the Earth is ruled by the PPF Empire . As the population growing , PPF needs to find more land for the newborns . Finally , PPF decides to attack Kscinow who ruling the Mars . Here the problem comes! How can the soldiers reach the Mars ? PPF convokes his soldiers and asks for their suggestions . “Rush … ” one soldier answers. “Shut up ! Do I have to remind you that there isn’t any road to the Mars from here!” PPF replies. “Fly !” another answers. PPF smiles :“Clever guy ! Although we haven’t got wings , I can buy some magic broomsticks from HARRY POTTER to help you .” Now , it’s time to learn to fly on a broomstick ! we assume that one soldier has one level number indicating his degree. The soldier who has a higher level could teach the lower , that is to say the former’s level > the latter’s . But the lower can’t teach the higher. One soldier can have only one teacher at most , certainly , having no teacher is also legal. Similarly one soldier can have only one student at most while having no student is also possible. Teacher can teach his student on the same broomstick .Certainly , all the soldier must have practiced on the broomstick before they fly to the Mars! Magic broomstick is expensive !So , can you help PPF to calculate the minimum number of the broomstick needed .
For example : 
There are 5 soldiers (A B C D E)with level numbers : 2 4 5 6 4;
One method :
C could teach B; B could teach A; So , A B C are eligible to study on the same broomstick.
D could teach E;So D E are eligible to study on the same broomstick;
Using this method , we need 2 broomsticks.
Another method:
D could teach A; So A D are eligible to study on the same broomstick.
C could teach B; So B C are eligible to study on the same broomstick.
E with no teacher or student are eligible to study on one broomstick.
Using the method ,we need 3 broomsticks.
……

After checking up all possible method, we found that 2 is the minimum number of broomsticks needed. 
 

Input
Input file contains multiple test cases. 
In a test case,the first line contains a single positive number N indicating the number of soldiers.(0<=N<=3000)
Next N lines :There is only one nonnegative integer on each line , indicating the level number for each soldier.( less than 30 digits);
 

Output
For each case, output the minimum number of broomsticks on a single line.
 

Sample Input
      
      
4 10 20 30 04 5 2 3 4 3 4
 

Sample Output
      
      
1 2
 

Author
PPF@JLU
 

Recommend
lcy

本题自己写的时候,只是用了LONG ,结果WA了,看了半天也不知道哪里出错,经一大神修改后,可以AC了,究其原因,问题出在数据类型上面
看来以后得注意题目给的数据范围大小







以下是从网上搜来的64位信息,另外,HDU是不支持LONG LONG数据类型的

在C/C++中,64为整型一直是一种没有确定规范的数据类型。现今主流的编译器中,对64为整型的支持也是标准不一,形态各异。一般来说,64位整型的定义方式有long long和__int64两种(VC还支持_int64),而输出到标准输出方式有printf(“%lld”,a),printf(“%I64d”,a),和cout << a三种方式。

本文讨论的是五种常用的C/C++编译器对64位整型的支持,这五种编译器分别是gcc(mingw32),g++(mingw32),gcc(linux i386),g++(linux i386),Microsoft Visual C++ 6.0。可惜的是,没有一种定义和输出方式组合,同时兼容这五种编译器。为彻底弄清不同编译器对64位整型,我写了程序对它们进行了评测,结果如下表。

变量定义 输出方式 gcc(mingw32) g++(mingw32) gcc(linux i386) g++(linux i386) MicrosoftVisual C++ 6.0
long long “%lld” 错误 错误 正确 正确 无法编译
long long “%I64d” 正确 正确 错误 错误 无法编译
__int64 “lld” 错误 错误 无法编译 无法编译 错误
__int64 “%I64d” 正确 正确 无法编译 无法编译 正确
long long cout 非C++ 正确 非C++ 正确 无法编译
__int64 cout 非C++ 正确 非C++ 无法编译 无法编译
long long printint64() 正确 正确 正确 正确 无法编译

上表中,正确指编译通过,运行完全正确;错误指编译虽然通过,但运行结果有误;无法编译指编译器根本不能编译完成。观察上表,我们可以发现以下几点:

  1. long long定义方式可以用于gcc/g++,不受平台限制,但不能用于VC6.0。
  2. __int64是Win32平台编译器64位长整型的定义方式,不能用于Linux。
  3. “%lld”用于Linux i386平台编译器,”%I64d”用于Win32平台编译器。
  4. cout只能用于C++编译,在VC6.0中,cout不支持64位长整型。

表中最后一行输出方式中的printint64()是我自己写的一个函数,可以看出,它的兼容性要好于其他所有的输出方式,它是一段这样的代码:

void printint64(long long a)
{
	if (a<=100000000)
		printf("%d\n",a);
	else
	{
		printf("%d",a/100000000);
		printf("%08d\n",a%100000000);
	}
}


这种写法的本质是把较大的64位整型拆分为两个32位整型,然后依次输出,低位的部分要补0。看似很笨的写法,效果如何?我把它和cout输出方式做了比较,因为它和cout都是C++支持跨平台的。首先printint64()和cout(不清空缓冲区)的运行结果是完全相同的,不会出现错误。我的试验是分别用两者输出1000000个随机数,实际结果是,printint64()在1.5s内跑完了程序,而cout需要2s。cout要稍慢一些,所以在输出大量数据时,要尽量避免使用。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C语言中,生成64位随机并存储在一个组中通常需要用到`<stdlib.h>`库中的`rand()`函和一些位操作来组合生成所需的随机精度。由于`rand()`返回的是32位整数,我们需要将多个调用的结果组合成一个64位。以下是一个简单的示例: ```c #include <stdio.h> #include <stdlib.h> #include <time.h> #define ARRAY_SIZE 8 // 假设我们想要一个包含8个元素的64位组 // 将两个32位随机连接为64位 unsigned long long combine_two_32_bits(unsigned int high, unsigned int low) { return ((unsigned long long)high << 32) | low; } int main() { srand(time(0)); // 设置随机种子,确保每次程序运行得到不同的结果 unsigned int high_part[ARRAY_SIZE / sizeof(unsigned int)]; unsigned int low_part[ARRAY_SIZE / sizeof(unsigned int)]; for (int i = 0; i < ARRAY_SIZE; i++) { // 生成两组32位随机 high_part[i] = rand(); low_part[i] = rand(); // 结合两部分生成64位,并存入组 unsigned long long combined_num = combine_two_32_bits(high_part[i], low_part[i]); unsigned char combined_array; // 预留足够的空间存储64位值(8字节) memcpy(combined_array, &combined_num, sizeof(combined_num)); printf("Array element %d: ", i); for (size_t j = 0; j < sizeof(combined_array); j++) { printf("%02X", combined_array[j]); // 输出十六进制表示 } printf("\n"); } return 0; } ``` 请注意,这个方法仅适用于32位系统,因为64位系统中`unsigned int`可能已经是64位。在64位系统上,直接使用`unsigned long long`类型即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值