Auto commit

This commit is contained in:
2025-02-24 22:33:16 +08:00
parent 5d520d977f
commit 0fe5855aea
2 changed files with 106 additions and 0 deletions
+54
View File
@@ -0,0 +1,54 @@
/*----------------------------------------------------------------------
程序设计
------------------------------------------------------------------------
在餐桌上吃饭,每人一只饭碗,2人一只菜碗,3人一只汤碗,一共用了n个碗,请编写函数算一算,一共有几人吃饭,计算出使用了几个饭碗,几个菜碗,几个汤碗?请注意:计算过程中菜碗和汤碗的数量要保证够用且是整数
示例输入:
【请输入总碗数n: 】25
一共有13人吃饭
使用了13个饭碗
使用了7个菜碗
使用了5个汤碗
------------------------------------------------------------------------
注意:部分源程序给出如下。请勿改动主函数main或其它函数中给出的内容,仅在
Program-End之间填入若干语句。
不要删除标志否则不得分。
不要修改或删除Program-End之外的内容否则不得分。
----------------------------------------------------------------------*/
#include <stdio.h>
#include <math.h>
// 函数声明,用于计算吃饭的人数以及饭碗、菜碗和汤碗的数量
int calculateDiningDetails(int n, int *riceBowls, int *vegetableBowls, int *soupBowls);
int main() {
int n;
int riceBowls, vegetableBowls, soupBowls,people;
printf("【请输入总碗数n: 】");
scanf("%d", &n);
// 调用函数计算吃饭的人数以及饭碗、菜碗和汤碗的数量
people = calculateDiningDetails(n, &riceBowls, &vegetableBowls, &soupBowls);
// 输出结果
if (people!= -1) {
printf("一共有%d人吃饭\n使用了%d个饭碗\n使用了%d个菜碗\n使用了%d个汤碗\n", people, riceBowls, vegetableBowls, soupBowls);
} else {
printf("没有符合条件的组合。\n");
}
return 0;
}
// 函数定义,用于计算吃饭的人数以及饭碗、菜碗和汤碗的数量
int calculateDiningDetails(int n, int *riceBowls, int *vegetableBowls, int *soupBowls) {
int x,totalBowls;
/**********Program**********/
/********** End **********/
}
+52
View File
@@ -0,0 +1,52 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 动态分配内存以存储替换后的字符串
char* replaceSubstring(const char *str, const char *oldSubstr, const char *newSubstr) {
int strLen = strlen(str);
int oldLen = strlen(oldSubstr);
int newLen = strlen(newSubstr);
int count=0, i, j, k;
char *result, *p;
/**********Program**********/
/********** End **********/
return result;
}
int main() {
char str[10000];
char oldSubstr[100];
char newSubstr[100];
char *replacedStr;
// 获取用户输入
printf("【请输入原字符串:】");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = 0; // 去除换行符
printf("【请输入要替换的子串:】");
fgets(oldSubstr, sizeof(oldSubstr), stdin);
oldSubstr[strcspn(oldSubstr, "\n")] = 0; // 去除换行符
if (strstr(str, oldSubstr) == NULL) {
printf("原字符串不包含所输入的子串。\n");
return 0;
}
printf("【请输入替换后的子串:】");
fgets(newSubstr, sizeof(newSubstr), stdin);
newSubstr[strcspn(newSubstr, "\n")] = 0; // 去除换行符
replacedStr = replaceSubstring(str, oldSubstr, newSubstr);
printf("【示例输出:】%s\n", replacedStr);
// 释放动态分配的内存
free(replacedStr);
return 0;
}