一个简单的C++问题,求高手指教
64-bitunsignedinteger是什么意思,在C++中用什么表示?原C++题如下:DescriptionThisisaclassicalproblem.Your...
64-bit unsigned integer是什么意思,在C++中用什么表示?
原C++题如下:
Description
This is a classical problem. Your job is to calculate the combination formula ,
C(n, m) = n! / (m! (n - m)!)
since n, m and C(n,m) fit a 64-bit unsigned integer.
Input
The first line of input there is one integer T (T<=100), giving the number of test cases in the input. Each test case contains a line with two integer n and m (0<=m<=n<2^64).
Output
For each case, output the answer C(n,m) corresponding to the input.
Sample Input
Copy sample input to clipboard
1
4 2
Sample Output
6 展开
原C++题如下:
Description
This is a classical problem. Your job is to calculate the combination formula ,
C(n, m) = n! / (m! (n - m)!)
since n, m and C(n,m) fit a 64-bit unsigned integer.
Input
The first line of input there is one integer T (T<=100), giving the number of test cases in the input. Each test case contains a line with two integer n and m (0<=m<=n<2^64).
Output
For each case, output the answer C(n,m) corresponding to the input.
Sample Input
Copy sample input to clipboard
1
4 2
Sample Output
6 展开
7个回答
展开全部
64-bit unsigned integer是指64位无符号数
#include <stdlib.h>
#include <iostream>
using namespace std;
unsigned long long jiechen(unsigned long long n,unsigned long long m=1){
unsigned long long i,results=1;
for (i=n;i>m;i--) results *= i;
return results;
}
int main(){
unsigned long long sum;
int cnum,i;
unsigned long long *m,*n;
cout<<"Enter the number of test cases:"<<endl;
cin>>cnum;
m=new unsigned long long[cnum];
n=new unsigned long long[cnum];
cout<<"Enter test cases in Order:"<<endl;
for (i=0;i<cnum;i++){
cin>>n[i];
cin>>m[i];}
for (i=0;i<cnum;i++){
sum=1;
//if (m[i]>n[i]/2) cout<<n[i]<<endl;
if (m[i]> n[i]/2)
sum = jiechen(n[i],m[i])/jiechen(n[i]-m[i]);
else
sum = jiechen(n[i],n[i]-m[i])/jiechen(m[i]);
cout<<sum<<endl;
}
delete m;
delete n;
system("pause");
return 1;
}
#include <stdlib.h>
#include <iostream>
using namespace std;
unsigned long long jiechen(unsigned long long n,unsigned long long m=1){
unsigned long long i,results=1;
for (i=n;i>m;i--) results *= i;
return results;
}
int main(){
unsigned long long sum;
int cnum,i;
unsigned long long *m,*n;
cout<<"Enter the number of test cases:"<<endl;
cin>>cnum;
m=new unsigned long long[cnum];
n=new unsigned long long[cnum];
cout<<"Enter test cases in Order:"<<endl;
for (i=0;i<cnum;i++){
cin>>n[i];
cin>>m[i];}
for (i=0;i<cnum;i++){
sum=1;
//if (m[i]>n[i]/2) cout<<n[i]<<endl;
if (m[i]> n[i]/2)
sum = jiechen(n[i],m[i])/jiechen(n[i]-m[i]);
else
sum = jiechen(n[i],n[i]-m[i])/jiechen(m[i]);
cout<<sum<<endl;
}
delete m;
delete n;
system("pause");
return 1;
}
展开全部
64-bit unsigned integer意思是64位无符号整数
C++里是unsigned long long
C++里是unsigned long long
追问
是不是unsigned long int?这道题会做吗?
追答
long long是64位有符号整数
要用unsigned long long
这道题就是一个裸的组合数。你可以网上搜索一下递推式,会有助你解决这道题
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
选择法排序 每一趟找的是较小数的下标(用的p记录) 后面是交换 if (p!=j)
这样的算法最好找个较小的测试数据从头到尾走一遍 加深印象
祝你成功
这样的算法最好找个较小的测试数据从头到尾走一遍 加深印象
祝你成功
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<iostream>
using namespace std;
int func(int n);
int C( int n, int m);
void main()
{
long int n,m,T;
cout<<"Please intput the number of test cases:"<<endl;
cin>>T;
for( int j=0;j<T;j++)
{
cout<<"The number is "<<j+1<<" now"<<endl;
cin>>n>>m;
cout<<C(n,m)<<endl;
}
}
int func(int n)
{
if(n==1)return n;
else
{
return n*func(n-1);
}
}
int C(int n,int m)
{
return func(n)/(func(m)*func(n-m));
}
尽力啦 64位不太清楚 用long修饰结果出错了 呵呵 希望有人能给点指点
using namespace std;
int func(int n);
int C( int n, int m);
void main()
{
long int n,m,T;
cout<<"Please intput the number of test cases:"<<endl;
cin>>T;
for( int j=0;j<T;j++)
{
cout<<"The number is "<<j+1<<" now"<<endl;
cin>>n>>m;
cout<<C(n,m)<<endl;
}
}
int func(int n)
{
if(n==1)return n;
else
{
return n*func(n-1);
}
}
int C(int n,int m)
{
return func(n)/(func(m)*func(n-m));
}
尽力啦 64位不太清楚 用long修饰结果出错了 呵呵 希望有人能给点指点
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <iostream>
using namespace std;
unsigned long long C(unsigned long long n,unsigned long long m)
{
/* C(n, m) = n! / (m! (n - m)!) */
unsigned long long n_factorial=1;
unsigned long long n_temp=n;
unsigned long long stop=m;
while(n_temp!=stop){
n_factorial*=n_temp--;
}
cout<<n_factorial<<endl;
unsigned long long n_m_factorial=1UL;
unsigned long long n_m_temp=n-m;
while(n_m_temp){
n_m_factorial*=n_m_temp--;
}
return n_factorial/n_m_factorial;
}
int main(){
unsigned long long val=C(100,50);
cout<<val<<endl;
return 0;
}
using namespace std;
unsigned long long C(unsigned long long n,unsigned long long m)
{
/* C(n, m) = n! / (m! (n - m)!) */
unsigned long long n_factorial=1;
unsigned long long n_temp=n;
unsigned long long stop=m;
while(n_temp!=stop){
n_factorial*=n_temp--;
}
cout<<n_factorial<<endl;
unsigned long long n_m_factorial=1UL;
unsigned long long n_m_temp=n-m;
while(n_m_temp){
n_m_factorial*=n_m_temp--;
}
return n_factorial/n_m_factorial;
}
int main(){
unsigned long long val=C(100,50);
cout<<val<<endl;
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
__int64 n;
前面使用两个下划线定义;
前面使用两个下划线定义;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询