1. 程式人生 > >計算機程式設計(c++)第3周程式設計作業

計算機程式設計(c++)第3周程式設計作業

3-1 列印3個相鄰字母

題目內容:

當用戶輸入一個英文字母后,程式能夠按照字母表的順序打印出3個相鄰的字母,其中使用者輸入的字母在中間。

程式執行結果如下:

d

cde

這裡假設字母表首尾兩個字母是相連的。若輸入字母Z,則程式輸出YZA。

輸入格式:

一個字母字元,可能為大寫,也可能為小寫。

輸出格式:

連續3個字元,大小寫不變。

輸入樣例:

d

輸出樣例:

cde

#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
	char x, a, b;
	cin >> x;
	if (x >= 65 && x <= 90) {
		a = (x - 'A' - 1 + 26) % 26 + 'A';
		b = (x - 'A' + 1) % 26 + 'A';
	}
	else {
		a = (x - 'a' - 1 + 26) % 26 + 'a';
		b = (x - 'a' + 1) % 26 + 'a';
	}
	cout << a << x << b << endl;
	system("pause");
	return 0;
}

3-2 歌唱大賽選手成績計算

題目內容:

歌唱大賽選手成績計算方法如下:去掉一個最高分,去掉一個最低分,將剩下分數的平均值作為選手的最後成績。這裡假設共有10位評委,都是按照百分制打分。

程式執行結果如下:

88 90 97 89 85 95 77 86 92 83

88.5

如果評委給出的成績不在0~100分之間,將給出錯誤提示。

程式執行結果如下:

101 90 97 89 85 95 77 86 92 83

the score is invalid.

輸入格式:

10個[0,100]之間的數.

輸出格式:

1個可以表示小數的數或者提示資訊“the score is invalid.”

平均值變數使用double型。

輸入樣例:

88 90 97 89 85 95 77 86 92 83

輸出樣例:

88.5

#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
	int a[10];
	int max, min, sum;
	max = sum = 0;
	min = 101;
	double ave;
	for (int i = 1; i <= 10; i++) 
	{
		cin >> a[i];
		if (a[i]>100 || a[i]<0) 
		{
			cout << "the score is invalid." << endl;
			return 0;
		}
		if (a[i]>max) 
		{
			max = a[i];
		}
		if (a[i]<min) 
		{
			min = a[i];
		}
		sum = sum + a[i];
	}
	ave = double(sum - max - min) / 8;
	cout << ave << endl;
	system("pause");
	return 0;
}

3-3 猴子吃桃

題目內容:

有一天,某隻猴子摘了一些桃子,當時吃了一半,又不過癮,於是就多吃了一個。以後每天如此,到第n天想吃時,發現就只剩下一個桃子。輸入n,表示到第n天剩下1個桃子,請計算第一天猴子摘的桃子數。

程式執行結果如下:

10

1534

輸入格式:

輸入一個整數n,n>0,表示到第n天剩下1個桃子。

輸出格式:

一個整數,表示第1天摘的桃子數。

輸入樣例:

10

輸出樣例:

1534

#include <iostream>
#include<stdlib.h>
using namespace std;


int peach(int n)       //表示第x天有幾個桃子
{
	if (1 < n)          //如果x不是最後一天,那麼這一天桃子的數量等於明天桃子數加1再乘以2
		return 2 * (peach(n-1) + 1);
	else if (1 == n)    //如果是最後一天,返回1
		return 1;
	else
		return -1;    //異常

}

int main()
{
	int n;
	cin >> n;
	cout << peach(n) << endl;
	system("pause");
	return 0;
}

3-4 搬磚問題

題目內容:

現有n塊磚,要由n人一次搬完,假定男人一次可以搬4塊,女人一次可以搬3塊,兩個小孩搬1塊,計算這n人中男人、女人和小孩的人數。輸入人數和磚數n,輸出可能的解決方案。

程式執行結果如下:

50

men0

women10

children40

men5

women3

children42

如果沒有滿足的情況,顯示提示資訊“no result!”

程式執行結果如下:

1

no result!

輸入格式:

表示人數的整型數

輸出格式:

所有滿足條件的男人、女人和孩子的人數或者提示資訊“no result!”

輸出格式見樣例。有多組方案時,按男人數量從少到多的順序輸出。男人數量相同時,女人數量從少到多。

輸入樣例:

50

輸出樣例:

men0

women10

children40

men5

women3

children42

#include <iostream>
#include <cmath>
#include<stdlib.h>
using namespace std;
int main()
{
	int men, women, children;
	int n;
	int x, y, z;
	cin >> n;
	for (men = 0; men <= n / 4; men++)
	{
		for (women = 0; women <= n / 3; women++)
		{
			children = 2 * (n - 4 * men - 3 * women);
			if (children>0 && children == n - men - women)
			{
				x = men; y = women; z = children;
				cout << "men" << men << endl;
				cout << "women" << women << endl;
				cout << "children" << children << endl;
			}
		}
	}
	if (x == 0 || y == 0 || z == 0)
		cout << "no result!" << endl;
	system("pause");
	return 0;
}

3-5 美分找錢

題目內容:

將n美分轉換成25、10、5和1美分的硬幣總共有多少種轉換方法?

執行結果如下:

25

13

如果n不在0~99之間,輸出提示資訊“the money is invalid!”

執行結果如下:

101

the money is invalid!

輸入格式:

整數,表示美分數

輸入可能不是[0,99]之間的整數。輸入不在該區間時,輸出為“the money is invalid!”。

輸出格式:

轉換方法數或者提示資訊“the money is invalid!”(不帶引號啊,單詞間只有一個空格)

輸入樣例:

25

輸出樣例:

13

#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
	int n, sum;
	sum = 0;
	cin >> n;
	if (n<0 || n>99) 
	{
		cout << "the money is invalid!" << endl;
	}
	else
	{
		for (int a = 0; a <= n; a++)
		{
			for (int b = 0; b <= n; b++) 
			{
				for (int c = 0; c <= n; c++)
				{
					for (int d = 0; d <= n; d++)
					{
						if (25 * a + 10 * b + 5 * c + d == n) 
						{
							sum = sum + 1;
						}
					}
				}
			}
		}
		cout << sum << endl;
	}
	system("pause");
	return 0;
}