请按照以下要求用c++语言编写程序:输入一个正整数repeat (0<repeat<10),做repeat次下列运算: 定义一个指针数组将下面的星期信息组织起来,输入一个字符串,在表中查找,若存在,输出该字符串在表中的序号,否则输出-1。 Sunday Monday Tuesday Wednesday Thursday Friday Saturday 输入输出示例:括号内为说明,无需输入输出 输入样例 (repeat=3) : 3 Tuesday Wednesday year 输出样例: 3 4 -1 代码长度限制 16 KB 时间限制 400 ms 内存限制 64 MB
时间: 2024-03-07 22:47:56 浏览: 60
好的,我会根据您的要求编写程序,请您稍等片刻。
```c++
#include <iostream>
#include <string>
using namespace std;
int main() {
int repeat;
string week[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; // 星期信息
cin >> repeat;
while (repeat--) {
string str;
cin >> str;
int i;
for (i = 0; i < 7; i++) {
if (str == week[i]) { // 在表中查找
cout << i + 1 << " "; // 输出该字符串在表中的序号
break;
}
}
if (i == 7) { // 若不存在,输出-1
cout << "-1 ";
}
}
return 0;
}
```
这是一个
相关问题
c++输入一个正整数repeat(0<repeat<10),做repeat次下列运算:输入一个学生的数学成绩,如果它低于60,输出“Fail",否则,输出“Pass”。
好的,以下是C++实现代码:
```c++
#include <iostream>
using namespace std;
int main() {
int repeat, score;
cout << "请输入 repeat 的值:" << endl;
cin >> repeat;
for (int i = 0; i < repeat; i++) {
cout << "请输入第 " << i + 1 << " 个学生的数学成绩:" << endl;
cin >> score;
if (score < 60) {
cout << "Fail" << endl;
} else {
cout << "Pass" << endl;
}
}
return 0;
}
```
以上代码中,我们先读入变量 repeat 的值,然后使用一个 for 循环,重复执行 repeat 次下列操作:
1. 提示用户输入当前学生的数学成绩;
2. 读入该成绩并存储到变量 score 中;
3. 判断 score 是否小于 60,若是则输出 "Fail",否则输出 "Pass"。
循环结束后,程序退出。
用c++实现:输入一个正整数 repeat (0<repeat<10),做 repeat 次下列运算: 输入一个正整数 n(0<n<=9)和一组(n个)升序的整数,建立单向链表,再输入一个整数 x,把 x 插入到这组数据中,使该组数据仍然有序。
以下是使用C++实现的代码:
```cpp
#include <iostream>
using namespace std;
struct ListNode {
int val;
ListNode* next;
ListNode(int x) : val(x), next(NULL) {}
};
ListNode* insert(ListNode* head, int x) {
ListNode* node = new ListNode(x);
if (!head || x < head->val) {
node->next = head;
return node;
}
ListNode* curr = head;
while (curr->next && curr->next->val < x) {
curr = curr->next;
}
node->next = curr->next;
curr->next = node;
return head;
}
ListNode* createList(int n) {
ListNode* dummy = new ListNode(-1);
ListNode* curr = dummy;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
curr->next = new ListNode(x);
curr = curr->next;
}
return dummy->next;
}
void printList(ListNode* head) {
ListNode* curr = head;
while (curr) {
cout << curr->val << " ";
curr = curr->next;
}
cout << endl;
}
int main() {
int repeat;
cin >> repeat;
while (repeat--) {
int n, x;
cin >> n;
ListNode* head = createList(n);
cin >> x;
head = insert(head, x);
printList(head);
}
return 0;
}
```
首先定义了链表节点 `ListNode`,包含一个整数值 `val` 和指向下一个节点的指针 `next`。在 `insert` 函数中,首先创建一个新节点,然后遍历链表找到插入位置,最后插入新节点。在 `createList` 函数中,根据输入的升序整数序列创建链表。在主函数中,根据输入的 `repeat` 参数,循环执行多次插入操作,每次创建链表并插入一个新节点,最后输出插入后的链表。