判断整数的各位数字之和是否为质数 分数 10 作者 周强 单位 青岛大学 以下程序判断输入的正整数的各位数字之和是否为质数并打印相应的结果”。请在空格里填写缺失的部分代码。
时间: 2024-02-15 22:01:24 浏览: 62
以下是代码:
```c
#include<stdio.h>
int main()
{
int n, sum = 0, i;
scanf("%d", &n);
while(n > 0)
{
sum += n % 10;
n /= 10;
}
for(i = 2; i < sum; i++)
{
if(sum % i == 0)
{
printf("No\n");
return 0;
}
}
printf("Yes\n");
return 0;
}
```
需要填写的空格是:
```c
for(i = 2; i < sum; i++)
```
其中 `sum` 是输入的正整数的各位数字之和,循环判断其是否为质数。
相关问题
7-1 向整数问好 分数 5 作者 周强 单位 青岛大学 Say hello to integers? Yes! 你没看错! 现在我们来向整数说“你好~” 本题读入两个整数,然后输出对她们的问候语。 输入格式: 在一行中给出两个绝对值不超过32767的整数A和B,两数之间有一个空格 输出格式: 在一行中输出 "Hello, A and B!" (其中A和B用实际输入的整数代替)
题目描述:
输入两个整数A和B,输出问候语"Hello, A and B!"。
输入格式:
在一行中输入两个绝对值不超过32767的整数A和B,两数之间有一个空格。
输出格式:
在一行中输出"Hello, A and B!"(其中A和B用实际输入的整数代替)。
输入样例:
-1 100
输出样例:
Hello, -1 and 100!
解题思路:
题目要求输出一个问候语,其实就是给出一个输出格式。只需要读入两个整数,然后按照输出格式输出即可。
6-2 写出派生类构造方法(C++) 分数 5 作者 周强 单位 青岛大学 裁判测试程序样例中展示的是一段定义基类People、派生类Student以及测试两个类的相关C++代码,其中缺失了部分代码,请补充完整,以保证测试程序正常运行。 函数接口定义:
以下是完整的代码示例:
```c++
#include <iostream>
using namespace std;
class People {
protected:
string name;
int age;
public:
People(string n, int a) : name(n), age(a) {}
void display() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
}
};
class Student : public People {
private:
int score;
public:
Student(string n, int a, int s) : People(n, a), score(s) {}
void show() {
display();
cout << "Score: " << score << endl;
}
};
int main() {
Student stu("Tom", 18, 90);
stu.show();
return 0;
}
```
其中,派生类 `Student` 的构造方法如下所示:
```c++
Student(string n, int a, int s) : People(n, a), score(s) {}
```
在构造方法中,首先使用 `:` 符号调用基类 `People` 的构造方法,并传入参数 `n` 和 `a`,然后再初始化派生类 `Student` 的成员变量 `score`。