用c语言输入一个整数字列,以及基准,要求计算并输出该序列中基准以上的素数个数
#include <stdio.h>
/* 判断一个数是否为素数 */
int is_prime(int n) {
if (n <= 1) { // 1 不是素数
return 0;
}
int i;
for (i = 2; i*i <= n; i++) {
if (n % i == 0) {
return 0;
}
}
return 1;
}
int main() {
int n, base, num_primes = 0;
printf("请输入整数字列的长度:");
scanf("%d", &n);
int a[n];
printf("请输入整数字列(以空格分隔):");
int i;
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
printf("请输入基准:");
scanf("%d", &base);
for (i = 0; i < n; i++) {
if (a[i] > base && is_prime(a[i])) {
num_primes++;
}
}
printf("整数字列中大于基准%d的素数个数为%d\n", base, num_primes);
return 0;
}
问题答案由GPT-4Plus模型(语音)与博主程序员之梦@共同编写:
c
#include <stdio.h>
int isPrime(int n) {
if (n <= 1) return 0;
for (int i = 2; i < n; i++) {
if (n % i == 0) return 0;
}
return 1;
}
int main() {
printf("请输入数字序列,以空格隔开:");
int nums[100];
int n = 0;
scanf("%d", &nums[n++]);
while (getchar() != '\n') {
scanf("%d", &nums[n++]);
}
printf("请输入基准:");
int base;
scanf("%d", &base);
int count = 0;
for (int i = 0; i < n; i++) {
if (nums[i] >= base && isPrime(nums[i])) {
count++;
}
}
printf("基准%d以上素数个数为:%d\n", base, count);
}
这个程序的逻辑是:
举个例子,如果输入:
2 5 8 11 13 17 10
则输出:
基准10以上素数个数为:3
因为11,13,17都是大于等于10的素数。
**
snake.c
#include <curses.h>
#include <stdlib.h>
#include <pthread.h>
#include "LIST.h"
#define MAP_WIDTH 20
#define MAP_HEIGHT 20
extern struct Snake *g_snake;
extern struct Snake g_food;
int g_flag = 0;
int g_direction = KEY_RIGHT;
int commitsuicide() {
struct Snake *p;
p = g_snake;
struct Snake *tmp;
tmp = g_snake->next;
while (tmp != NULL) {
if ((p->x == tmp->x) && (p->y == tmp->y)) {
return 0;
}
tmp = tmp->next;
}
return -1;
}
void initFood() {
int judge = 0;
do {
int x = rand() % 20 + 1;
int y = rand() % 20 + 1;
g_food.x = x;
g_food.y = y;
g_food.next = NULL;
if ((g_food.x == g_snake->x) && (g_food.y == g_snake->y)) {
judge = 1;
}
} while (g_food.x == 0 || g_food.x > 18 || g_food.y == 0 || g_food.y > 18 ||
judge == 1);
}
int foodFind(int x, int y) {
if ((g_food.x == x) && (g_food.y == y)) {
return 0;
}
return -1;
}
int headTofood() {
if ((g_food.x == g_snake->x) && (g_food.y == g_snake->y)) {
return 0;
}
return -1;
}
void initMap() {
int x;
int y;
for (x = 0; x < MAP_WIDTH; x++) {
for (y = 0; y < MAP_HEIGHT; y++) {
if (x == 0 || x == MAP_HEIGHT - 1) {
if (x == 0) {
printw("* ");
} else {
printw("* ");
}
} else {
if (y == 0 || y == MAP_WIDTH - 1) {
printw("* ");
} else {
if ((snakeFind(g_snake, x, y) == 0)) {
printw("[]");
} else if (foodFind(x, y) == 0) {
printw("++");
} else {
printw(" ");
}
}
}
}
printw("\n");
}
}
void handleKey() {
int key;
noecho();
keypad(stdscr, TRUE);
while (1) {
key = getch();
if (adjustDirection(key) == 0) {
continue;
}
}
}
int adjustDirection(int key) {
if (key == KEY_DOWN || key == KEY_UP || key == KEY_RIGHT ||
key == KEY_LEFT) {
if (((g_direction == KEY_RIGHT) && (key == KEY_LEFT)) ||
((g_direction == KEY_LEFT) && (key == KEY_RIGHT)) ||
((g_direction == KEY_UP) && (key == KEY_DOWN)) ||
((g_direction == KEY_DOWN) && (key == KEY_UP))) {
return 0;
}
}
g_direction = key;
return -1;
}
void moveSnakeauto() {
while (1) {
switch (g_direction) {
case KEY_DOWN:
if (headTofood() == 0) {
insertSnake(&g_snake, g_snake->x + 1, g_snake->y);
g_flag = -1;
} else {
insertSnake(&g_snake, g_snake->x + 1, g_snake->y);
deleteSnake(&g_snake);
}
break;
case KEY_UP:
if (headTofood() == 0) {
insertSnake(&g_snake, g_snake->x - 1, g_snake->y);
g_flag = -1;
} else {
insertSnake(&g_snake, g_snake->x - 1, g_snake->y);
deleteSnake(&g_snake);
}
break;
case KEY_LEFT:
if (headTofood() == 0) {
insertSnake(&g_snake, g_snake->x, g_snake->y - 1);
g_flag = -1;
} else {
insertSnake(&g_snake, g_snake->x, g_snake->y - 1);
deleteSnake(&g_snake);
}
break;
case KEY_RIGHT:
if (headTofood() == 0) {
insertSnake(&g_snake, g_snake->x, g_snake->y + 1);
g_flag = -1;
} else {
insertSnake(&g_snake, g_snake->x, g_snake->y + 1);
deleteSnake(&g_snake);
}
break;
default:
break;
}
clear();
if (g_flag == -1) {
initFood();
g_flag = 0;
}
if (snakeDie() == 0 || commitsuicide() == 0) {
destorySnake();
createSnake();
}
initMap();
refresh();
usleep(100000);
}
}
int main() {
pthread_t t_movesnake;
pthread_t t_getinput;
initscr();
createSnake();
initFood();
initMap();
pthread_create(&t_movesnake, NULL, (void *)moveSnakeauto, NULL);
pthread_create(&t_getinput, NULL, (void *)handleKey, NULL);
while (1);
endwin();
return 0;
}
list.c
#include <curses.h>
#include <stdlib.h>
#include "LIST.h"
struct Snake *g_snake;
struct Snake g_food;
int snakeDie() // panduansiwang
{
if (g_snake->x == 0 || g_snake->x == 19 || g_snake->y == 0 ||
g_snake->y == 19) {
return 0;
}
return -1;
}
void destorySnake() // bianlishanchu
{
struct Snake *p;
while (g_snake != NULL) {
p = g_snake;
g_snake = g_snake->next;
free(p);
}
}
void insertSnake(struct Snake **head, int x, int y) // touchafa
{
struct Snake *node;
node = (struct Snake *)malloc(sizeof(struct Snake));
node->x = x;
node->y = y;
node->next = NULL;
node->next = *head;
*head = node;
}
void createSnake() {//chushihuashe
g_snake = (struct Snake *)malloc(sizeof(struct Snake));
g_snake->x = 3;
g_snake->y = 3;
g_snake->next = NULL;
insertSnake(&(g_snake), 3, 4);
insertSnake(&(g_snake), 3, 5);
insertSnake(&(g_snake), 3, 6);
}
int snakeFind(struct Snake *head, int x, int y) {//bianlipanduan
struct Snake *p;
p = head;
while (p != NULL) {
if ((p->x == x) && (p->y == y)) {
return 0;
}
p = p->next;
}
return -1;
}
void deleteSnake(struct Snake **head) {//shanchuweiba
struct Snake *p;
struct Snake *tmp;
tmp = NULL;
p = *head;
while ((p != NULL) && (p->next != NULL)) {
if ((p->next->next == NULL)) {
tmp = p->next;
p->next = NULL;
free(tmp);
break;
}
p = p->next;
}
}
list.h
#ifndef _LIST_H_
#define _LIST_H_
struct Snake
{
int x;
int y;
struct Snake *next;
};
void insertSnake(struct Snake **head,int x,int y);
void createSnake();
int snakeFind(struct Snake *head,int x,int y);
void deleteSnake(struct Snake **head);
int foodFind(int x,int y);
int snakeDie();
void destorySnake();
#endif
Makefile
snake:
gcc snake_final.c LIST.c -lncurses -lpthread -o snakepro