C语言题目解答:#include <stdio.h> int a[100]; int game(int n, int m, int k) { int i, count = 0, g; for (i = 0; i < n; i++) { if (a[i] != 0) { count++; if (count == m + k) { a[i] = 0; return game(n, i + 1, k); } } } for (i = 0; i < n; i++) { if (a[i] != 0) { g = a[i]; break; } } if (count == 1) { return g; } return game(count, (m + k - 1) % count + 1, k); } int main() { int i, n, r; scanf("%d %d", &n, &r); for (i = 0; i < n; i++) { a[i] = i + 1; } printf("The left child is NO %d.\n", game(n, 1, r)); return 0; }
时间: 2024-01-24 13:02:39 浏览: 18
这是一个约瑟夫问题的解法,给定一个人数 n 和报数的数字 m 和从第 k 个人开始数起,求最后剩下的人的编号。这个算法使用了递归方法求解。
该算法的基本思路是对数组中不为 0 的元素进行遍历,当遍历到第 m + k 个元素时,将其设置为 0 并返回从下一个元素开始的递归调用,如果遍历完整个数组后都没有找到第 m + k 个元素,则从第一个非 0 元素重新开始计数。
当只剩下一个元素时,直接返回该元素的编号。否则,将数组大小变为不为 0 的元素个数 count,计算新的起点 (m + k - 1) % count + 1 并递归调用函数。
对于给定的 n 和 r,代码使用 for 循环对数组进行初始化,然后调用 game 函数进行计算,并输出最后剩下的人的编号。
注意,该代码中没有对输入进行校验,如有需要,应该在程序中进行相应的校验处理。
相关问题
#include<stdio.h> #include<stdlib.h> int main(void) { struct book { char bkname[30]; char author[15]; float price; int stock; }b[3]={{"C_programming","Tony",38.5,6},{"English","Amy",43.5,4},{"Computer_game","Wiya",47,5}}; int i;补全程序并且输出至屏幕(C语言,保存位置为E盘文件名books.txt)
你可以使用以下代码来完成程序并将输出保存到指定文件中:
```c
#include <stdio.h>
#include <stdlib.h>
int main(void) {
// 定义结构体
struct book {
char bkname[30];
char author[15];
float price;
int stock;
} b[3] = {
{"C_programming", "Tony", 38.5, 6},
{"English", "Amy", 43.5, 4},
{"Computer_game", "Wiya", 47, 5}
};
int i;
// 打开文件
FILE* fp = fopen("E:\\books.txt", "w");
// 判断文件是否打开成功
if (fp == NULL) {
printf("Failed to open the file.");
return 1;
}
// 输出到屏幕和文件
printf("Book\t\tAuthor\t\tPrice\tStock\n");
fprintf(fp, "Book\t\tAuthor\t\tPrice\tStock\n");
for (i = 0; i < 3; i++) {
printf("%s\t%s\t%.2f\t%d\n", b[i].bkname, b[i].author, b[i].price, b[i].stock);
fprintf(fp, "%s\t%s\t%.2f\t%d\n", b[i].bkname, b[i].author, b[i].price, b[i].stock);
}
// 关闭文件
fclose(fp);
return 0;
}
```
这段代码会将输出同时输出到屏幕和文件中,文件名为E盘下的books.txt。如果文件打开失败则会输出错误信息并退出程序。
#include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <curses.h> #include"kbhit.h" //定义全局变量 int high,width;//画面大小 int x,y;//鸟的位置 int score; int bar_x,bar_down,bar_top;//障碍物相关坐标 void start() { high=15; width=32; x=width/8; y=0; bar_x=width/2; bar_down=high/2; bar_top=high/3; score=0; } void show() //打印鸟的位置 障碍物的位置 { system("clear"); int i,n; for(i=0;i<high;i++){ for(n=0;n<width;n++){ if((i==y)&&(n==x)) printf("@"); // 打印鸟的位置 else if(i==high-1) printf("-"); //最后一行打印横线 else if((n==bar_x)&&((i<bar_top)||(i>bar_down))) printf("*"); // 打印障碍物 else printf(" "); // 打印空格 } printf("\n"); } printf("your score:%d",score); } void unput() //失败的时候暂停并且输入game over { y++; bar_x--; if(score <2){ usleep(120000); }else if(score>=2 && score <8){ usleep(100000); }else if(score>=8&&score<11){ usleep(80000); }else if(score>=11&&score<14){ usleep(60000); } if(x==bar_x) //如果小鸟的水平位置 x=bar_x障碍物的水平位置 { if((y>bar_down)||(y<bar_top)) // 进一步判断小鸟的位置y是否满足大于上障碍物垂直高度的最低点或者小于下障碍物垂直高度的最高点 { printf("game over!\n"); system("pause"); exit(0); } else score++; } if(bar_x<=0){ int k=high*0.8; bar_x=width; int temp=rand()%k; bar_down=temp+high/10; bar_top=temp-high/10; } } void input() //检测键盘是否有输入 { char put; if(kbhit()) { put=getchar(); if(put==' ') y-=2; } } int main() //主函数 { start(); while(1) { show(); unput(); input(); } return 0; }修改c语言小游戏代码使得功能更加的丰富
下面是修改后的代码,使得功能更加丰富:
```c
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <curses.h>
#include "kbhit.h"
// 定义全局变量
int high, width; // 画面大小
int x, y; // 鸟的位置
int score;
int bar_x, bar_down, bar_top; // 障碍物相关坐标
int level; // 难度级别
void start() {
high = 15;
width = 32;
x = width / 8;
y = 0;
bar_x = width / 2;
bar_down = high / 2;
bar_top = high / 3;
score = 0;
level = 1;
}
void show() {
system("clear");
int i, n;
for (i = 0; i < high; i++) {
for (n = 0; n < width; n++) {
if ((i == y) && (n == x))
printf("@"); // 打印鸟的位置
else if (i == high - 1)
printf("-"); // 最后一行打印横线
else if ((n == bar_x) && ((i < bar_top) || (i > bar_down)))
printf("*"); // 打印障碍物
else
printf(" "); // 打印空格
}
printf("\n");
}
printf("Score: %d\n", score);
printf("Level: %d\n", level);
}
void unput() {
y++;
bar_x--;
// 根据得分调整难度
if (score >= 5 && score < 10) {
level = 2;
usleep(100000);
} else if (score >= 10 && score < 15) {
level = 3;
usleep(80000);
} else if (score >= 15) {
level = 4;
usleep(60000);
} else {
usleep(120000);
}
if (x == bar_x) {
if ((y > bar_down) || (y < bar_top)) {
printf("Game Over! Your Score: %d\n", score);
system("pause");
exit(0);
} else {
score++;
}
}
if (bar_x <= 0) {
int k = high * 0.8;
bar_x = width;
int temp = rand() % k;
bar_down = temp + high / 10;
bar_top = temp - high / 10;
}
}
void input() {
char put;
if (kbhit()) {
put = getchar();
if (put == ' ')
y -= 2;
}
}
int main() {
start();
while (1) {
show();
unput();
input();
}
return 0;
}
```
在这个修改后的代码中,添加了以下功能:
1. 增加了难度级别(level),根据得分来调整游戏的难度。随着得分的增加,游戏速度将会加快。
2. 在游戏界面上增加了显示当前的得分和难度级别。
通过以上修改,游戏变得更加有挑战性和有趣。你还可以根据需要进一步修改和扩展代码,添加更多的功能和特效。