作业帮 > 综合 > 作业

谁能帮我解一下这道程序题,请用c++语言.

来源:学生作业帮 编辑:搜搜考试网作业帮 分类:综合作业 时间:2024/05/29 17:42:08
谁能帮我解一下这道程序题,请用c++语言.
Problem Description A number whose only prime factors are 2,3,5 or 7 is called a humble number.The sequence 1,2,3,4,5,6,7,8,9,10,12,14,15,16,18,20,21,24,25,27,...shows the first 20 humble numbers.Write a program to find and print the nth element in this sequence Input The input consists of one or more test cases.Each test case consists of one integer n with 1
谁能帮我解一下这道程序题,请用c++语言.
#include <iostream>
using namespace std;

const int SIZE = 5842;

int main() {
int humbernumber[SIZE];
int ptr[4][2] = { { 2, 0 }, { 3, 0 }, { 5, 0 }, { 7, 0 } };
int index = 1, nextnumber, candidate;
int n;
humbernumber[0] = 1;
while (index < SIZE) {
nextnumber = -1;
for (int i = 0; i < 4; i++) {
while (humbernumber[ptr[i][1]] * ptr[i][0] <= humbernumber[index - 1]) {
ptr[i][1]++;
}
candidate = humbernumber[ptr[i][1]] * ptr[i][0];
if (nextnumber == -1 || nextnumber > candidate) {
nextnumber = candidate;
}
}
humbernumber[index] = nextnumber;
index++;
}
while (cin >> n && n > 0) {
cout << "The " << n;
if (n % 100 >= 11 && n % 100 <= 13) {
cout << "th";
}
else if (n % 10 == 1) {
cout << "st";
}
else if (n % 10 == 2) {
cout << "nd";
}
else if (n % 10 == 3) {
cout << "rd";
}
else {
cout << "th";
}
cout << " humble number is " << humbernumber[n - 1] << "." << endl;
}
}