与7无关的数

一个正整数,如果它能被7整除,或者它的十进制表示法中某个位数上的数字为7,则称其为与7相关的数。求所有小于等于N的与7无关的正整数的平方和。

 
例如:N = 8,<= 8与7无关的数包括:1 2 3 4 5 6 8,平方和为:155。

Input第1行:一个数T,表示后面用作输入测试的数的数量。(1 <= T <= 1000)
第2 - T + 1行:每行1个数N。(1 <= N <= 10^6)Output共T行,每行一个数,对应T个测试的计算结果。Sample Input

5
4
5
6
7
8

Sample Output

30
55
91
91
155


错误代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

long long int ans[2000000];
int ha[2000000];
int main()
{
    long long int T,a;
    int i;

    for(i=1; i<=1000000; i++)
    {
        a=i;
        if(a%7==0)
            ha[i]=0;
        else{
            while(1)
            {
                if(a==0)
                {
                    ha[i]=1;
                    break;
                }
                if( (a-a/10*10)==7 )
                {
                    ha[i]=0;
                    break;
                }
                a=a/10;
            }
        }
    }

    ans[1]=1;
    for(i=2;i<=1000000;i++)
    {
        if(ha[i]==1)
        {
            ans[i]=ans[i-1]+i*i;
        }
        else
            ans[i]=ans[i-1];
    }

    scanf("%d",&T);
    while(T--)
    {
        int a;
        scanf("%d",&a);
        printf("%lld\n",ans[a]);
    }
    return 0;
}

 

 

正确代码:

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

long long int ans[2000000];
int ha[2000000];
int main()
{
    long long int i,T,a;

    for(i=1; i<=1000000; i++)
    {
        a=i;
        if(a%7==0)
            ha[i]=0;
        else{
            while(1)
            {
                if(a==0)
                {
                    ha[i]=1;
                    break;
                }
                if( (a-a/10*10)==7 )
                {
                    ha[i]=0;
                    break;
                }
                a=a/10;
            }
        }
    }

    ans[1]=1;
    for(i=2;i<=1000000;i++)
    {
        if(ha[i]==1)
        {
            ans[i]=ans[i-1]+i*i;
        }
        else
            ans[i]=ans[i-1];
    }

    scanf("%d",&T);
    while(T--)
    {
        int a;
        scanf("%d",&a);
        printf("%lld\n",ans[a]);
    }
    return 0;
}

 

 

 

此题需要运用前缀和的技巧

(可能是编译器的问题,一直交不上)

1,答案错误    long long int 和 int  进行运算就会出错。

2,时间超限   sprintf 不能用

 

转载于:https://www.cnblogs.com/coder-tcm/p/9326401.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值