杭电1099——Lottery(关于__int64的使用详解)

Lottery



Problem Description
Eddy's company publishes a kind of lottery.This set of lottery which are numbered 1 to n, and a set of one of each is required for a prize .With one number per lottery, how many lottery on average are required to make a complete set of n coupons?
 

Input
Input consists of a sequence of lines each containing a single positive integer n, 1<=n<=22, giving the size of the set of coupons.
 

Output
For each input line, output the average number of lottery required to collect the complete set of n coupons. If the answer is an integer number, output the number. If the answer is not integer, then output the integer part of the answer followed by a space and then by the proper fraction in the format shown below. The fractional part should be irreducible. There should be no trailing spaces in any line of ouput.
 

Sample Input
   
   
   
   
2 5 17
 

Sample Output
   
   
   
   
3 5 11 -- 12 340463 58 ------ 720720
 

这道题算是比较注重细心的一道题!

题目不算是很难!可是有一些细节部分的处理很费劲!我稍稍总结一下我遇到的问题!

关于__int64的使用!


类型  long long __int64 intmax_t
格式 %lld %I64d %I64d

在Dev C++中,三种类型均需用%I64d格式输出 ,c语言中intmax_t需要用到头文件stdint.h

C++采用cin输入时,两种类型均可。

eg1 eg2 eg3
复制代码
#include<stdio.h>
int main()
{
  long long a;

  scanf("%I64d",&a);
  print("%I64d\n",a);

  system("pause");
  return 0;
}
复制代码
复制代码
#include<stdio.h>
int main()
{
  __int64 a;

  scanf("%I64d",&a);
  printf("%I64d\n",a);
  
  system("pause");
  return 0;
} 
复制代码
复制代码
#include<stdio.h>
#include<stdint.h>
int main()
{
  intmax_t a;

  scanf("%I64d",&a);
  printf("%I64d\n",a);

  system("pause");
  return 0;
}
复制代码
eg4 eg5 eg6
复制代码
#include<stdio.h>
#include<stdint.h>
int main()
{
  intmax_t a,b;
  
  cin>>a>>b;
  cout<<a+b<<endl;
  system("pause");
  return 0;
}
复制代码

 

复制代码
#include<iostream>
using namespace std; int main()
{
  long long a,b;
  
  cin>>a>>b;
  cout<<a+b<<endl;
  system("pause");
  return 0;
}
复制代码

 

复制代码
#include<iostream>
using namespace std;
int main()
{
  intmax_t a,b;
  
  cin>>a>>b;
  cout<<a+b<<endl;
  system("pause");
  return 0;
}
复制代码

 

在做ACM题时,经常都会遇到一些比较大的整数。而常用的内置整数类型常常显得太小了:其中long 和 int 范围是[-2^31,2^31),即-2147483648~2147483647。而unsigned范围是[0,2^32),即 0~4294967295。也就是说,常规的32位整数只能够处理40亿以下的数。
那遇到比40亿要大的数怎么办呢?这时就要用到C++的64位扩展了。不同的编译器对64位整数的扩展有所不同。基于ACM的需要,下面仅介绍VC6.0与g++编译器的扩展。
VC的64位整数分别叫做__int64与unsigned __int64,其范围分别是[-2^63, 2^63)与[0,2^64),即-9223372036854775808~9223372036854775807与 0~18446744073709551615(约1800亿亿)。对64位整数的运算与32位整数基本相同,都支持四则运算与位运算等。当进行64位与 32位的混合运算时,32位整数会被隐式转换成64位整数。但是,VC的输入输出与__int64的兼容就不是很好了,如果你写下这样一段代码:
1 __int64 a;
2 cin >> a;
3 cout << a;

        那么,在第2行会收到“error C2679: binary '>>' : no operator defined which takes a right-hand operand of type '__int64' (or there is no acceptable conversion)”的错误;在第3行会收到“error C2593: 'operator <<' is ambiguous”的错误。cout,cin对于__int64无能为力,写法较复杂,不过也经常不用。那是不是就不能进行输入输出呢?当然不是,你可以使用C的写法:
scanf("%I64d",&a);
printf("%I64d",a);
就可以正确输入输出了。当使用unsigned __int64时,把"I64d"改为"I64u"就可以了。
OJ通常使用g++编译器。其64位扩展方式与VC有所不同,它们分别叫做long long 与 unsigned long long。处理规模与除输入输出外的使用方法同上。对于输入输出,它的扩展比VC好。既可以使用
1 long long a;
2 cin>>a;
3 cout<<a;
也可以使用
scanf("%lld",&a);
printf("%lld",a);

使用无符号数时,将"%lld"改成"%llu"即可。

差不多了吧!发代码吧!

#include<iostream>
#include<cstdio>
using namespace std;

int gbs(int a,int b)//求最小公倍数
{
	int min;
	if(a==b) return a;
	else
	{
		min=a>b?b:a;
		for(int i=2;;i++)
		{
			if(min*i%a==0 && min*i%b==0)
				return min*i;
		}
	}

}

__int64 gys(__int64 m,__int64 n)//求最小公约数
{
	__int64 r;
	if(m<n)
	{
		r=m;m=n;n=r;
	}
	r=m%n;
	while(r)
	{
		m=n;n=r;r=m%n;
	}
	return n;
}
int main()
{
	int i,n,temp,temp2,temp3;
	__int64 fz1,fm1,fm2;
	while(cin>>n)
	{
		if(n<=0) break;
		fm1=1;
		fz1=n;
		
		//printf("%I64d,%I64d\n",fz1,fm1);
		for(i=2;i<=n;i++)
		{
			fm2=fm1;
			fm1=gbs(fm1,i);
			fz1=fz1*(fm1/fm2)+n*(fm1/i);
			//printf("分子是:%d,分母是:%d\n",fz1,fm1);
		}
		//printf("%d,%d/%d\n",fz1/fm1,fz1%fm1,fm1);
		if(fz1%fm1!=0)
		{
			temp=fz1/fm1;
			fz1=fz1-temp*fm1;
			temp2=gys(fz1,fm1);
			//printf("%I64d  %I64d  %I64d\n",fz1,fm1,temp2);
			fz1=fz1/temp2; fm1=fm1/temp2;
			if(temp<10)
			printf("  %I64d\n",fz1);
			else
			printf("   %I64d\n",fz1);
			printf("%I64d ",temp);
			temp3=fm1;
			while(temp3>0)
			{
				printf("-");
               temp3=temp3/10;
			   
			}
			printf("\n");
			if(temp<10)
			printf("  %I64d\n",fm1);
			else
            printf("   %I64d\n",fm1);

		}
        else
			printf("%I64d\n",fz1/fm1);

	}
	return 0;
}


你可能感兴趣的:(Integer,System,扩展,each,编译器,output)