第三章53题
#include <iostream>
using namespace std;
bool main1(int year)
{
return ((year % 400 == 0 || year % 4 == 0 && year % 100 != 0));
}
int main2(int year, int month)
{
int day[] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
if (month == 2 && main1(year))
{
return 29;
}
else
{
return day[month - 1];
}
}
int main3(int year, int month, int day)
{
int sum = 0;
for (int i = 1; i < month; i++)
{
sum += main2(year, i);
}
sum += day;
return sum;
}
int main()
{
int year, month, day;
cout << "请输入年份:";
cin >> year;
cout << "请输入月份:";
cin >> month;
cout << "请输入日期:";
cin >> day;
int sum = main3(year, month, day);
cout << year << "年" << month << "月" << day << "日是该年的第" <<sum << "天" << endl;
return 0;
}
第三章第51题
#include <iostream>
using namespace std;
int main()
{
int t;
int i = 0;
int a[10] = {};
cout << "替换前的数组:";
for (i = 0; i < 10; i++)
{
cin >> a[i];
}
cout << endl;
for (i = 1 ; i <9; i++)
{
t = a[i + 1];
a[i + 1] = a[i];
a[i] = t;
}
t = a[9];
a[9] = a[0];
a[0] = t;
cout << endl;
cout << "替换后的数组:";
for (i = 0; i < 10; i++)
{
cout << a[i]<<" ";
}
return 0;
}
第三章第42题
#include <iostream>
using namespace std;
int Count(double a[], int n)
{
double average = 0;
int geshu = 0;
cout << "请输入数组中的数:" << endl;
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
for (int i = 0; i < n; i++)
{
average += a[i] / n;
}
for (int i = 0; i < n; i++)
{
if (a[i] >=average)
{
geshu++;
cout << a[i] << " ";
}
}
cout << "大于平均值的个数为:" << geshu;
return geshu;
}
int main()
{
double k[5] = {};
Count(k, 6);
return 0;
}
第三章第47题
#include <iostream>
using namespace std;
int comm(int n, int k)
{
if (k>n)
{
return 0;
}
else if (k == n || k == 0)
{
return 1;
}
else
{
return comm(n - 1, k) + comm(n - 1, k - 1);
}
}
int main()
{
int n, k;
cout << "共有多少人:";
cin >> n;
cout << "选几个人:";
cin >> k;
cout << "共有组数:" << comm(n, k);
return 0;
}