杭电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;
}


这是一条SQL语句,用于查询t_lottery_statistics_prize表中按照抽奖中奖次数降序排列的前5条数据,条件是统计日期为当周。具体解释如下: - `SELECT *`: 查询t_lottery_statistics_prize表中的所有数据。 - `FROM t_lottery_statistics_prize`: 指定查询的数据表为t_lottery_statistics_prize表。 - `WHERE YEARWEEK(date_format(now(),'%Y-%m-%d')) = YEARWEEK(statistics_date)`: 指定查询条件为统计日期为当周。其中,YEARWEEK()函数可以返回日期所在的年份和周数,date_format()函数可以将当前日期格式化为年-月-日的形式。 - `ORDER BY lottery_win_count DESC`: 指定按照lottery_win_count字段降序排列,即中奖次数多的排在前面。 - `LIMIT 5`: 指定只查询前5条数据。 如果您使用的是SpringBoot和Mybatis框架,可以在对应的Mapper接口中定义对应的方法,用于执行这条SQL语句: ```java public interface LotteryStatisticsPrizeMapper { List<LotteryStatisticsPrize> getWeeklyRanking(); } ``` 在对应的Mapper.xml文件中编写SQL语句: ```xml <select id="getWeeklyRanking" resultType="com.example.demo.entity.LotteryStatisticsPrize"> SELECT * FROM t_lottery_statistics_prize WHERE YEARWEEK(date_format(now(),'%Y-%m-%d')) = YEARWEEK(statistics_date) ORDER BY lottery_win_count DESC LIMIT 5; </select> ``` 在Service中调用Mapper的方法: ```java @Service public class LotteryStatisticsPrizeService { @Autowired private LotteryStatisticsPrizeMapper lotteryStatisticsPrizeMapper; public List<LotteryStatisticsPrize> getWeeklyRanking() { return lotteryStatisticsPrizeMapper.getWeeklyRanking(); } } ``` 最后在Controller中调用Service的方法,并返回数据给前端: ```java @RestController public class LotteryStatisticsPrizeController { @Autowired private LotteryStatisticsPrizeService lotteryStatisticsPrizeService; @GetMapping("/weeklyRanking") public List<LotteryStatisticsPrize> getWeeklyRanking() { return lotteryStatisticsPrizeService.getWeeklyRanking(); } } ``` 以上是使用SpringBoot和Mybatis框架实现获取中奖奖品的周排名并返回数据的完整代码实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值