C++基础题
水仙花数
#include <iostream>
using namespace std;
int main() {
cout << "3位水仙花数:" << endl;
for (int num = 100; num <= 999; num++) {
int digit1 = num / 100; // 百位数字
int digit2 = (num / 10) % 10; // 十位数字
int digit3 = num % 10; // 个位数字
if (num == (digit1 * digit1 * digit1 + digit2 * digit2 * digit2 + digit3 * digit3 * digit3)) {
cout << num << " ";
}
}
cout << endl;
return 0;
}
五角星数
【问题描述】
求出所有的五角星数,所谓五角星数是指它的各个数位上数字的五次方等于它本身的数。例如54748=55555+
44444+77777+44444+88888
【输出格式】
一行所有的五角星数,以空格隔开。
#include <iostream>
#include <cmath>
bool isPentagonalNumber(int num) {
int originalNum = num;
int sum = 0;
while (num > 0) {
int digit = num % 10;
sum += pow(digit, 5);
num /= 10;
}
return (sum == originalNum);
}
void printAllPentagonalNumbers() {
cout << "所有的五角星数:" << std::endl;
for (int num = 0; num <= 99999; num++) {
if (isPentagonalNumber(num)) {
cout << num << " ";
}
}
cout << std::endl;
}
int main() {
printAllPentagonalNumbers();
return 0;
}
数字图形三角形一
#include <iostream>
int main() {
int n;
cout << "Enter the number of rows: ";
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
cout << i;
}
cout<<endl;
}
return 0;
}
统计15个同学中成绩为100分的人数
#include <iostream>
int main() {
const int numStudents = 15;
int scores[numStudents];
cout << "Enter the scores of " << numStudents << " students:" << endl;
// 输入15个整数作为同学们的成绩
for (int i = 0; i < numStudents; i++) {
cin >> scores[i];
}
int count = 0;
// 统计成绩为100分的人数
for (int i = 0; i < numStudents; i++) {
if (scores[i] == 100) {
count++;
}
}
cout << "Number of students with a score of 100: " << count << endl;
return 0;
}
标签:numStudents,cout,int,基础,C++,num,五角星,include
From: https://www.cnblogs.com/lwp-nicol/p/17471242.html