A.
题目描述
小亮想知道任意指定的两个数字之间有多少个素数,请帮他编程实现以下功能:用户输入两个正整数,系统自动输出两个数之间素数的个数。
输入描述:
输入为一小一大两个正整数,范围为(1~2000),用空格隔开。
输出描述:
针对输入的两个正整数,输出它们之间(包括这两个数)素数的个数并换行。
示例1
输入
1 20
输出
8
示例2
输入
100 200
输出
21
#include <iostream>
#include <cmath>
using namespace std;
const int maxn=100005;
int sushu[maxn]={0};
void intt(){//筛法求素数
sushu[0]=1;
sushu[1]=1;
for(int i=2; i<=maxn-5; i++)
{
if(sushu[i]==0)
{
for(int j=i+i; j<=maxn-5; j+=i)
{
sushu[j]=1;
}
}
}
}
int main(int argc, char *argv[]) {
intt();
int x,y;
cin>>x>>y;
int count=0;
if(x>y)
{
swap(x,y);
}
for(int i=x; i<=y; i++)
{
if(sushu[i]==0)
{
count++;
}
}
cout<<count<<endl;
return 0;
}
B.
题目描述
输入一个字符串,输出该字符串是否回文。回文是指顺读和倒读都一样的字符串。
输入描述:
输入为一行字符串(字符串中没有空白字符,字符串长度不超过100)。
输出描述:
如果字符串是回文,输出yes;否则,输出no。
示例1
输入
aabaa
输出
yes
示例2
输入
aaababc
输出
no
#include <iostream>
#include <string>
using namespace std;
int main() {
string input;
cin >> input;
int left = 0, right = input.length() - 1;
while (left < right) {
if (input[left] != input[right]) {
cout << "no" << endl;
return 0;
}
left++;
right--;
}
cout << "yes" << endl;
return 0;
}
C.
题目描述
定义一个结构体变量(包括年月日)。计算该日在本年中是第几天。
输入描述:
一行三个整数,空格隔开,分别表示年月日。
输出描述:
输出一个整数,表示该日是这一年的第几天。
示例1
输入
2023 12 8
输出
342
#include <iostream>
using namespace std;
struct Date {
int year;
int month;
int day;
};
int main() {
Date date;
cin >> date.year >> date.month >> date.day;
int days_in_month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (date.year % 4 == 0 && (date.year % 100 != 0 || date.year % 400 == 0)) {
days_in_month[2] = 29;
}
int day_of_year = 0;
for (int i = 1; i < date.month; ++i) {
day_of_year += days_in_month[i];
}
day_of_year += date.day;
cout << day_of_year << endl;
return 0;
}
D.
题目描述
定义学生结构体类型,包括学号,姓名,3科成绩,编写一个input函数(用于输入n个学生的数据记录),一个print函数(用于输出学生的数据记录),在主函数中,用input函数输入这些记录,用print函数输出这些记录。
输入描述:
输入n+1行,
第一行输入一个整数n(1 ≤ n ≤ 20),
接下来n行,每行输入一个学生的信息,包括: 学号,姓名,3科成绩。
输出描述:
按照输入的顺序输出n行学生的信息。
示例1
输入
3 1 kiki 98 99 100 2 rere 99 97 96 3 huhu 99 99 100
输出
1 kiki 98 99 100 2 rere 99 97 96 3 huhu 99 99 100
#include <iostream>
#include <string>
using namespace std;
struct Student {
int id;
string name;
int score1;
int score2;
int score3;
};
void input(Student students[], int n) {
for (int i = 0; i < n; i++) {
cin >> students[i].id >> students[i].name >> students[i].score1 >> students[i].score2 >> students[i].score3;
}
}
void print(Student students[], int n) {
for (int i = 0; i < n; i++) {
cout << students[i].id << " " << students[i].name << " " << students[i].score1 << " " << students[i].score2 << " " << students[i].score3 << endl;
}
}
int main() {
int n;
cin >> n;
Student students[n];
input(students, n);
print(students, n);
return 0;
}
E.
题目描述
输入若干个学生的信息(学号、姓名、成绩),当输入学号为0时结束,用单向链表组织这些学生信息后,再按顺序输出。
输入描述:
输入多行,直到一行开始输入0结束,每行包括,学号(长度小于等于9),姓名(长度小于等于100),成绩,空格分隔。
输出描述:
按照输入顺序输出每位学生信息,一行一个学生信息,学号,姓名,成绩,空格分隔。
示例1
输入
1 linchengda 92 2 chenjiatai 88 3 caoyuxuan 90 4 limengqing 86 0
输出
1 linchengda 92 2 chenjiatai 88 3 caoyuxuan 90 4 limengqing 86
#include <iostream>
#include <string>
using namespace std;
struct Student {
int id;
string name;
int score;
Student* next;
};
int main() {
Student* head = nullptr;
Student* tail = nullptr;
int id, score;
string name;
while (cin >> id) {
if (id == 0) {
break;
}
cin >> name >> score;
Student* new_student = new Student{id, name, score, nullptr};
if (!head) {
head = new_student;
tail = new_student;
} else {
tail->next = new_student;
tail = new_student;
}
}
Student* current = head;
while (current) {
cout << current->id << " " << current->name << " " << current->score << endl;
Student* temp = current;
current = current->next;
delete temp;
}
return 0;
}