Auto commit

This commit is contained in:
smallkun 2025-02-25 08:30:32 +08:00
parent 5e7ef30e2c
commit 0c901eeaaa
3 changed files with 41 additions and 95 deletions

View File

@ -1,62 +0,0 @@
/*----------------------------------------------------------------------
------------------------------------------------------------------------
23n个碗使?
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**********/
for(x=3;x<n;x++){//n为碗的总数 x为当前吃饭人数
*riceBowls = x;//饭碗数量等于人数
*vegetableBowls = x%2==0?x/2:x/2+1;//菜碗数量计算 每2人一个碗不足2人也要有碗
*soupBowls = x%3==0?x/3:x/3+1;//汤碗数量计算
totalBowls = *riceBowls + *vegetableBowls + *soupBowls;
if(totalBowls == n){
return x;
}
}
return -1;
/********** End **********/
}

View File

@ -1,54 +1,62 @@
/*---------------------------------------------------------------------- /*----------------------------------------------------------------------
------------------------------------------------------------------------ ------------------------------------------------------------------------
23n个碗使? 23n个碗使?
n: 25 n: 25
13 13
使13 使13
使7 使7
使5 使5
------------------------------------------------------------------------ ------------------------------------------------------------------------
main或其它函数中给出的内容 main或其它函数中给出的内容
Program-End之间填入若干语句 Program-End之间填入若干语句
Program-End之外的内容否则不得分 Program-End之外的内容否则不得分
----------------------------------------------------------------------*/ ----------------------------------------------------------------------*/
#include <stdio.h> #include <stdio.h>
#include <math.h> #include <math.h>
// 函数声明,用于计算吃饭的人数以及饭碗、菜碗和汤碗的数量 // 函数声明,用于计算吃饭的人数以及饭碗、菜碗和汤碗的数量
int calculateDiningDetails(int n, int *riceBowls, int *vegetableBowls, int *soupBowls); int calculateDiningDetails(int n, int *riceBowls, int *vegetableBowls, int *soupBowls);
int main() { int main() {
int n; int n;
int riceBowls, vegetableBowls, soupBowls,people; int riceBowls, vegetableBowls, soupBowls,people;
printf("【请输入总碗数n: 】"); printf("【请输入总碗数n: 】");
scanf("%d", &n); scanf("%d", &n);
// 调用函数计算吃饭的人数以及饭碗、菜碗和汤碗的数量 // 调用函数计算吃饭的人数以及饭碗、菜碗和汤碗的数量
people = calculateDiningDetails(n, &riceBowls, &vegetableBowls, &soupBowls); people = calculateDiningDetails(n, &riceBowls, &vegetableBowls, &soupBowls);
// 输出结果 // 输出结果
if (people!= -1) { if (people!= -1) {
printf("一共有%d人吃饭\n使用了%d个饭碗\n使用了%d个菜碗\n使用了%d个汤碗\n", people, riceBowls, vegetableBowls, soupBowls); printf("一共有%d人吃饭\n使用了%d个饭碗\n使用了%d个菜碗\n使用了%d个汤碗\n", people, riceBowls, vegetableBowls, soupBowls);
} else { } else {
printf("没有符合条件的组合。\n"); printf("没有符合条件的组合。\n");
} }
return 0; return 0;
} }
// 函数定义,用于计算吃饭的人数以及饭碗、菜碗和汤碗的数量 // 函数定义,用于计算吃饭的人数以及饭碗、菜碗和汤碗的数量
int calculateDiningDetails(int n, int *riceBowls, int *vegetableBowls, int *soupBowls) { int calculateDiningDetails(int n, int *riceBowls, int *vegetableBowls, int *soupBowls) {
int x,totalBowls; int x,totalBowls;
/**********Program**********/ /**********Program**********/
for(x=3;x<n;x++){//n为碗的总数 x为当前吃饭人数
*riceBowls = x;//饭碗数量等于人数
*vegetableBowls = x%2==0?x/2:x/2+1;//菜碗数量计算 每2人一个碗不足2人也要有碗
*soupBowls = x%3==0?x/3:x/3+1;//汤碗数量计算
totalBowls = *riceBowls + *vegetableBowls + *soupBowls;
if(totalBowls == n){
return x;
}
}
return -1;
/********** End **********/ /********** End **********/
} }

View File

@ -2,7 +2,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
// 动态分配内存以存储替换后的字符串 // 动态分配内存以存储替换后的字符串
char* replaceSubstring(const char *str, const char *oldSubstr, const char *newSubstr) { char* replaceSubstring(const char *str, const char *oldSubstr, const char *newSubstr) {
int strLen = strlen(str); int strLen = strlen(str);
int oldLen = strlen(oldSubstr); int oldLen = strlen(oldSubstr);
@ -26,26 +26,26 @@ int main() {
char newSubstr[100]; char newSubstr[100];
char *replacedStr; char *replacedStr;
// 获取用户输入 // 获取用户输入
printf("【请输入原字符串:】"); printf("【请输入原字符串:】");
fgets(str, sizeof(str), stdin); fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = 0; // 去除换行符 str[strcspn(str, "\n")] = 0; // 去除换行符
printf("【请输入要替换的子串:】"); printf("【请输入要替换的子串:】");
fgets(oldSubstr, sizeof(oldSubstr), stdin); fgets(oldSubstr, sizeof(oldSubstr), stdin);
oldSubstr[strcspn(oldSubstr, "\n")] = 0; // 去除换行符 oldSubstr[strcspn(oldSubstr, "\n")] = 0; // 去除换行符
if (strstr(str, oldSubstr) == NULL) { if (strstr(str, oldSubstr) == NULL) {
printf("原字符串不包含所输入的子串。\n"); printf("原字符串不包含所输入的子串。\n");
return 0; return 0;
} }
printf("【请输入替换后的子串:】"); printf("【请输入替换后的子串:】");
fgets(newSubstr, sizeof(newSubstr), stdin); fgets(newSubstr, sizeof(newSubstr), stdin);
newSubstr[strcspn(newSubstr, "\n")] = 0; // 去除换行符 newSubstr[strcspn(newSubstr, "\n")] = 0; // 去除换行符
replacedStr = replaceSubstring(str, oldSubstr, newSubstr); replacedStr = replaceSubstring(str, oldSubstr, newSubstr);
printf("【示例输出:】%s\n", replacedStr); printf("【示例输出:】%s\n", replacedStr);
// 释放动态分配的内存 // 释放动态分配的内存
free(replacedStr); free(replacedStr);
return 0; return 0;