用c语言编写(9)编写程序,先输入-一个需要组合的整数,再输入3个用来组合的整数,若可以组合(每个数字至少使用1次),则输出所有组合的可能;若不存在组合,则输出无组合(10) 修改第9题的程序,对于有组合的搭配,只输出第1个组合(11)编写程序,假设30个学生的成绩都是50~100分,成绩随机生成,要求每行输出6个学生的成绩,并最后统计输出最高分、最低分和平均分(保留2位小数)(12)编写万年历程序,输入年、月,输出该月的日历表。输入年,输出该年的日历表。已知1900年1月1日是星期一
时间: 2024-03-16 21:43:59 浏览: 91
(9)代码如下:
```c
#include <stdio.h>
#include <stdbool.h>
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
void combination(int arr[], int n, int m, int result[], int pos) {
if (m == 0) {
printArray(result, pos);
return;
}
for (int i = n - 1; i >= m - 1; i--) {
result[m - 1] = arr[i];
combination(arr, i, m - 1, result, pos + 1);
}
}
int main() {
int n, m;
printf("请输入需要组合的整数:");
scanf("%d", &n);
printf("请输入用来组合的三个整数:");
int arr[3];
for (int i = 0; i < 3; i++) {
scanf("%d", &arr[i]);
}
int result[3];
for (int i = 1; i <= 3; i++) {
combination(arr, 3, i, result, 0);
}
bool found = false;
for (int i = 0; i < 3; i++) {
if (n == arr[i]) {
found = true;
break;
}
}
if (found) {
printf("%d ", n);
}
for (int i = 1; i <= 3; i++) {
combination(arr, 3, i, result, 0);
for (int j = 0; j < i; j++) {
if (n == arr[j]) {
found = true;
break;
}
}
if (found) {
printArray(result, i);
break;
}
}
if (!found) {
printf("无组合\n");
}
return 0;
}
```
(10)修改后的代码如下:
```c
#include <stdio.h>
#include <stdbool.h>
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
void combination(int arr[], int n, int m, int result[], int pos, bool *found) {
if (m == 0) {
if (*found) {
return;
}
printArray(result, pos);
*found = true;
return;
}
for (int i = n - 1; i >= m - 1; i--) {
result[m - 1] = arr[i];
combination(arr, i, m - 1, result, pos + 1, found);
}
}
int main() {
int n, m;
printf("请输入需要组合的整数:");
scanf("%d", &n);
printf("请输入用来组合的三个整数:");
int arr[3];
for (int i = 0; i < 3; i++) {
scanf("%d", &arr[i]);
}
int result[3];
bool found = false;
for (int i = 0; i < 3; i++) {
if (n == arr[i]) {
found = true;
printf("%d\n", n);
break;
}
}
for (int i = 1; i <= 3; i++) {
combination(arr, 3, i, result, 0, &found);
if (found) {
break;
}
}
if (!found) {
printf("无组合\n");
}
return 0;
}
```
(11)代码如下:
```c
#include <stdio.h>
#include <stdbool.h>
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
void combination(int arr[], int n, int m, int result[], int pos, bool *found) {
if (m == 0) {
if (*found) {
return;
}
printArray(result, pos);
*found = true;
return;
}
for (int i = n - 1; i >= m - 1; i--) {
result[m - 1] = arr[i];
combination(arr, i, m - 1, result, pos + 1, found);
}
}
int main() {
int n, m;
printf("请输入需要组合的整数:");
scanf("%d", &n);
printf("请输入用来组合的三个整数:");
int arr[3];
for (int i = 0; i < 3; i++) {
scanf("%d", &arr[i]);
}
int result[3];
bool found = false;
for (int i = 0; i < 3; i++) {
if (n == arr[i]) {
found = true;
printf("%d\n", n);
break;
}
}
for (int i = 1; i <= 3; i++) {
combination(arr, 3, i, result, 0, &found);
if (found) {
break;
}
}
if (!found) {
printf("无组合\n");
}
return 0;
}
```
(12)代码如下:
```c
#include <stdio.h>
#include <stdbool.h>
bool isLeapYear(int year) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
return true;
} else {
return false;
}
}
int daysInMonth(int year, int month) {
int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (month == 2 && isLeapYear(year)) {
return 29;
} else {
return days[month - 1];
}
}
int daysInYear(int year) {
if (isLeapYear(year)) {
return 366;
} else {
return 365;
}
}
int daysFrom1900(int year, int month, int day) {
int days = 0;
for (int i = 1900; i < year; i++) {
days += daysInYear(i);
}
for (int i = 1; i < month; i++) {
days += daysInMonth(year, i);
}
days += day - 1;
return days;
}
int main() {
int year, month;
printf("请输入年份:");
scanf("%d", &year);
printf("请输入月份:");
scanf("%d", &month);
int days = daysFrom1900(year, month, 1);
int weekDay = (days + 1) % 7;
printf("日 一 二 三 四 五 六\n");
for (int i = 0; i < weekDay; i++) {
printf(" ");
}
int daysOfMonth = daysInMonth(year, month);
for (int i = 1; i <= daysOfMonth; i++) {
printf("%2d ", i);
if ((weekDay + i) % 7 == 0) {
printf("\n");
}
}
if ((weekDay + daysOfMonth) % 7 != 0) {
printf("\n");
}
return 0;
}
```
阅读全文