Van*_*rna 5 c algorithm function
我是竞争性编程的新手,我在Hacker Rank上遇到了问题.问题陈述如下:
"如果我们列出10以下的所有自然数是3或5的倍数,我们得到3,5,6和9.这些倍数的总和是23.
找出N以下3或5的所有倍数之和.
输入格式第一行包含表示测试用例数的T. 接下来是T行,每行包含一个整数N.
输出格式对于每个测试用例,打印一个整数,表示N以下所有3或5的倍数之和."
约束
1≤T≤10^ 5
1≤N≤10^ 9
我编写了以下代码,成功满足3个测试用例,但在剩下的两个测试用例中失败了.
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int func(int p,int n)
{
int j;
n=n-1;
j=n/p;
return (p*j*(j+1))/2;
}
int main()
{
unsigned long int n;
int t,j,count;
scanf("%d",&t);
if(t>=1 && t<=100000){
for(j=0;j<t;j++)
{
scanf("%lu",&n);
if(n>=1 && n<=1000000000)
{
count=func(3,n)+func(5,n)-func(15,n);
printf("%d\n",count);
}
}}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的代码中有什么错误.为什么不被接受?
有几个问题。
int从 回来的时候你确实已经满溢了func。另外,你的printf陈述应该是printf("%llu\n", count);
func因此,从、count和局部变量,的返回值j都应该是unsigned long long,并且您的打印输出也应该反映这一点。你需要这样做j unsigned long long是因为 return 语句中的算术运算func(至少在 VS 2013 中是这样)。