杭电oj上第1099题,下面是我的代码从1到22都能运行出正确结果,为什么总是WA呢
#include<stdio.h>intmain(){__int64n,sum,sum1,i,a,b,l,c,d,m,k,k1,k2;while(scanf("%I64d...
#include<stdio.h>
int main()
{
__int64 n,sum,sum1,i,a,b,l,c,d,m,k,k1,k2;
while(scanf("%I64d",&n)!=EOF)
{
sum=1;
sum1=0;
for(i=1;i<=n;i++)
{
if(i>=17)
sum=sum/128;
sum=sum*i;
}
for(i=1;i<=n;i++)
sum1=sum1+sum/i;
a=(n*sum1)/sum;
b=(n*sum1)%sum;
if(b==0)
printf("%I64d\n\n",a);
else
{
k1=sum;
k2=b;
for(;;)
{
k=k1%k2;
if(k==0)
break;
k1=k2;
k2=k;
}
sum=sum/k2;
b=b/k2;
c=0;
d=a;
while(d!=0)
{
c++;
d=d/10;
}
m=sum;
l=0;
while(m!=0)
{
l++;
m=m/10;
}
for(i=0;i<=c;i++)
printf(" ");
printf("%I64d\n",b);
printf("%I64d ",a);
for(i=1;i<=l;i++)
printf("-");
printf("\n");
for(i=0;i<=c;i++)
printf(" ");
printf("%I64d\n",sum);
}
}
return 0;
}
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 展开
int main()
{
__int64 n,sum,sum1,i,a,b,l,c,d,m,k,k1,k2;
while(scanf("%I64d",&n)!=EOF)
{
sum=1;
sum1=0;
for(i=1;i<=n;i++)
{
if(i>=17)
sum=sum/128;
sum=sum*i;
}
for(i=1;i<=n;i++)
sum1=sum1+sum/i;
a=(n*sum1)/sum;
b=(n*sum1)%sum;
if(b==0)
printf("%I64d\n\n",a);
else
{
k1=sum;
k2=b;
for(;;)
{
k=k1%k2;
if(k==0)
break;
k1=k2;
k2=k;
}
sum=sum/k2;
b=b/k2;
c=0;
d=a;
while(d!=0)
{
c++;
d=d/10;
}
m=sum;
l=0;
while(m!=0)
{
l++;
m=m/10;
}
for(i=0;i<=c;i++)
printf(" ");
printf("%I64d\n",b);
printf("%I64d ",a);
for(i=1;i<=l;i++)
printf("-");
printf("\n");
for(i=0;i<=c;i++)
printf(" ");
printf("%I64d\n",sum);
}
}
return 0;
}
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 展开
展开全部
超过17的都错了。
主要问题在
if(i>=17)
sum=sum/128;
这是我写的代码
#include <stdio.h>
int g(int a, int b)
{
if(b==0) return a;
return g(b,a%b);
}
int main()
{
int zhengshu, fenzi, fenmu, n, i, gg, newfenmu,newfenzi;
while(scanf("%d",&n)==1)
{
zhengshu = fenzi = 0; // 整数部分和分子都是0
fenmu = 1; // 分母是1
for(i=1;i<=n;i++) // 加上 n/i
{
newfenmu = fenmu * i; // 新的分母
newfenzi = fenzi * i + fenmu * n; // 新的分子
gg = g(newfenmu, newfenzi); // 分子分母的最大公因数
fenmu = newfenmu / gg; // 约分后的分母
fenzi = newfenzi / gg; // 约分后的分子
zhengshu += fenzi / fenmu; // 提出整数部分
fenzi = fenzi % fenmu; // 化分真分数
}
if(fenzi==0) // 最后结果为整数
{
printf("%d\n",zhengshu);
}
else // 结果为非整数
{
gg = zhengshu;
while(gg)
{
putchar(' ');
gg/=10;
}
printf(" %d\n%d ",fenzi,zhengshu);
gg = fenmu;
while(gg)
{
putchar('-');
gg/=10;
}
putchar(10);
gg = zhengshu;
while(gg)
{
putchar(' ');
gg/=10;
}
printf(" %d\n",fenmu);
}
}
}
附带1-22的答案
1
3
1
5 -
2
1
8 -
3
5
11 --
12
7
14 --
10
3
18 --
20
26
21 --
35
129
25 ---
280
73
29 ---
252
551
33 ----
2520
551
37 ----
2310
9473
41 -----
27720
13433
45 -----
25740
18581
49 -----
24024
4129
54 -----
45045
340463
58 ------
720720
620743
62 ------
680680
1662439
67 -------
4084080
3704479
71 -------
3879876
408335
76 ------
739024
46533
81 ------
235144
主要问题在
if(i>=17)
sum=sum/128;
这是我写的代码
#include <stdio.h>
int g(int a, int b)
{
if(b==0) return a;
return g(b,a%b);
}
int main()
{
int zhengshu, fenzi, fenmu, n, i, gg, newfenmu,newfenzi;
while(scanf("%d",&n)==1)
{
zhengshu = fenzi = 0; // 整数部分和分子都是0
fenmu = 1; // 分母是1
for(i=1;i<=n;i++) // 加上 n/i
{
newfenmu = fenmu * i; // 新的分母
newfenzi = fenzi * i + fenmu * n; // 新的分子
gg = g(newfenmu, newfenzi); // 分子分母的最大公因数
fenmu = newfenmu / gg; // 约分后的分母
fenzi = newfenzi / gg; // 约分后的分子
zhengshu += fenzi / fenmu; // 提出整数部分
fenzi = fenzi % fenmu; // 化分真分数
}
if(fenzi==0) // 最后结果为整数
{
printf("%d\n",zhengshu);
}
else // 结果为非整数
{
gg = zhengshu;
while(gg)
{
putchar(' ');
gg/=10;
}
printf(" %d\n%d ",fenzi,zhengshu);
gg = fenmu;
while(gg)
{
putchar('-');
gg/=10;
}
putchar(10);
gg = zhengshu;
while(gg)
{
putchar(' ');
gg/=10;
}
printf(" %d\n",fenmu);
}
}
}
附带1-22的答案
1
3
1
5 -
2
1
8 -
3
5
11 --
12
7
14 --
10
3
18 --
20
26
21 --
35
129
25 ---
280
73
29 ---
252
551
33 ----
2520
551
37 ----
2310
9473
41 -----
27720
13433
45 -----
25740
18581
49 -----
24024
4129
54 -----
45045
340463
58 ------
720720
620743
62 ------
680680
1662439
67 -------
4084080
3704479
71 -------
3879876
408335
76 ------
739024
46533
81 ------
235144
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询