程序员

枚举算法作业(五)

2018-09-25  本文已影响116人  续袁

时间限制 1000 ms
内存限制 64 MB
题目描述
李老师的lucky number 是3,5和7,他爱屋及乌,还把所有质因数只有3,5,7的数字认定为lucky number,比如9, 15, 21, 25等等。请聪明的你帮忙算一算小于等于x的lucky number有多少个?

输入数据
一个正整数x,3=<x<=1000000000000

输出数据
小于等于x的lucky number的个数。

样例输入
49
样例输出
11
样例说明
int存不下

#include<stdio.h>
#include<math.h>
#include<iomanip>
#include "stdlib.h"
#include <iostream>
using namespace std;

int main(){
    long x;
    long count = 0;
    cin >> x;
    for (long i = 0; i < x / 3+1; i++)
        for (long j = 0; j < x / 5+1; j++)
            for (long k = 0; k < x / 7+1; k++){
                if ((pow(3, i)* pow(5, j) * pow(7, k) <= x)&&(i+j+k)>0) {
                    count = count + 1;
                    //cout << "i:" << i << "  j:" << j << "  k:" << k << endl;
                      }
    }
    cout << count << endl;

    return 0;
}
#include<stdio.h>
#include<math.h>
#include<iomanip>
#include "stdlib.h"
#include <iostream>
using namespace std;
long rooting(long x, int a);
int main(){
    long x;
    long count = 0;
    cin >> x;

    long three_times =rooting(x,3);
    long five_times = rooting(x, 5);
    long seven_times = rooting(x,7);

    //count = three_times + five_times + seven_times;

    for (long i = 0; i <three_times + 1; i++)
        for (long j = 0; j < five_times + 1; j++)
            for (long k = 0; k < seven_times + 1; k++){
                if ((pow(3, i)* pow(5, j) * pow(7, k) <= x) && (i + j + k)>0) {
                    count = count + 1;
                    //cout << "i:" << i << "  j:" << j << "  k:" << k << endl;
                }
            }


    cout << count << endl;

    return 0;
}

long rooting(long x, int a){
    int three_m = x % a;
    x = x - three_m;
    long times = 0;
    while (x > 1){
        x = x / a;
        times = times + 1;
    }
    return times;
}
上一篇 下一篇

猜你喜欢

热点阅读