C++編程作業: 理性認識C程序 導論 編程題

來源: POJ

編程題#1:蘋果和蟲子

注意: 總時間限制: 1000ms 內存限制: 65536kB

1.1 描述

你買了一箱n個蘋果,很不幸的是買完時箱子裏混進了一條蟲子。蟲子每x小時能吃掉一個蘋果,假設蟲子在吃完一個蘋果之前不會吃另一個,那麼經過y小時你還有多少個完整的蘋果?

輸入
輸入僅一行,包括n,x和y(均爲整數)。

輸出
輸出也僅一行,剩下的蘋果個數

樣例輸入

第一組
10 4 9
第二組
10 4 36
第三組
10 4 100

樣例輸出

第一組
7
第二組
1
第三組
0

提示
注意:是要求完整的蘋果數。

1.2 初始解答

#include <iostream>
#include <math.h>

using namespace std;

int result[3];

void calc(double num1, double num2, double num3, int no){
    double speed, remain;

    speed = 1 / num2;
    remain = num1 - ceil(speed * num3);
    if (remain >= 0)
        result[no] = (int)remain;
    else
        result[no] = 0;
}

int main() {
    char str1[20], str2[20], str3[20];
    int num1, num2, num3;

    scanf("%s", str1);
    scanf("%d%d%d", &num1, &num2, &num3);
    calc(num1, num2, num3, 0);
    scanf("%s", str2);
    scanf("%d%d%d", &num1, &num2, &num3);
    calc(num1, num2, num3, 1);
    scanf("%s", str3);
    scanf("%d%d%d", &num1, &num2, &num3);
    calc(num1, num2, num3, 2);

    cout << str1 << endl;
    cout << result[0] << endl;
    cout << str2 << endl;
    cout << result[1] << endl;
    cout << str3 << endl;
    cout << result[2] << endl;

    return 0;
}

1.3 重做解答

在Coursera上上傳上面的代碼,本地編譯器能通過,Coursera不通過。而且最爲曹丹的是Coursera的報錯是隻有類型沒有錯誤提示的……經過反覆試驗發現好像不能寫函數,行與行之間也不能有空格……
重新寫的能通過的版本(以下同):

#include <iostream>
using namespace std;
int main()
{
	int n, x, y, result;
	n = y = y = result=0;
	cin >> n >> x >> y;
	if (y%x==0)
	{
		result = n - y / x;
	}
	else
	{
		result = n - (y / x+1);
	}
	if (result < 0) result = 0;
	cout << result << endl;
    return 0;
}

編程題#2:大象喝水

注意: 總時間限制: 1000ms 內存限制: 65536kB

2.1 描述

一隻大象口渴了,要喝20升水才能解渴,但現在只有一個深h釐米,底面半徑爲r釐米的小圓桶(h和r都是整數)。問大象至少要喝多少桶水纔會解渴。

輸入
輸入有一行:包行兩個整數,以一個空格分開,分別表示小圓桶的深h和底面半徑r,單位都是釐米。

輸出
輸出一行,包含一個整數,表示大象至少要喝水的桶數。

樣例輸入

第一組
23 11
第二組
1 1

樣例輸出

第一組
3
第二組
6367

2.2 提示

如果一個圓桶的深爲h釐米,底面半徑爲r釐米,那麼它最多能裝Pi * r * r * h立方厘米的水。(設Pi=3.14159)
1升 = 1000毫升
1毫升 = 1 立方厘米

2.3 初始解答

#include <iostream>
#include <math.h>

using namespace std;

int result[2];
static double pi = 3.14159;

void calc(int h, int r, int no) {
    double v = pi * h * r * r;
    int calc = (int) ceil(20000 / v);

    if (v <= 20000)
        result[no] = calc;
    else
        result[no] = 1;
}

int main() {
    char str1[20], str2[20];
    int num1, num2;

    scanf("%s", str1);
    scanf("%d%d", &num1, &num2);
    calc(num1, num2, 0);
    scanf("%s", str2);
    scanf("%d%d", &num1, &num2);
    calc(num1, num2, 1);

    cout << str1 << endl;
    cout << result[0] << endl;
    cout << str2 << endl;
    cout << result[1] << endl;

    return 0;
}

2.4 重做解答

#include <iostream>
using namespace std;
#define PI 3.14159
#define SUM 20000
int main() { 
    int h, r, n;
    double total; 
    cin >> h >> r;
    total = PI * r * r * h; 
    n = SUM / total;
    if( n < SUM / total)
    { 
        cout << n + 1 << endl;
 } 
    else
    { 
        cout << n << endl;
 }
    return 0;
}

編程題#3:最高的分數

注意: 總時間限制: 1000ms 內存限制: 65536kB

3.1 描述

孫老師講授的《計算概論》這門課期中考試剛剛結束,他想知道考試中取得的最高分數。因爲人數比較多,他覺得這件事情交給計算機來做比較方便。你能幫孫老師解決這個問題嗎?

輸入
輸入兩行,第一行爲整數n(1 <= n < 100),表示參加這次考試的人數.第二行是這n個學生的成績,相鄰兩個數之間用單個空格隔開。所有成績均爲0到100之間的整數。

輸出
輸出一個整數,即最高的成績。

樣例輸入

5
85 78 90 99 60

樣例輸出

99

3.2 解答

一開始我不知道cin方法是可以把空格作爲分隔符的,導致我最初的代碼構象是收集完輸入的字符串之後按空格鍵分割然後分別賦值給數組元素……不過很明顯這道題不需要這麼複雜的方法:

#include <iostream>
using namespace std;
int main() {
    int n, max = 0, score;
    cin >> n;
    while (n--) {
        cin >> score; //cin可以以空格作爲分隔符!
        if (score > max)
            max = score;
    }
    cout << max << endl;
    return 0;
}

編程題#4:最大奇數與最小偶數之差的絕對值

注意: 總時間限制: 1000ms 內存限制: 65536kB

4.1 描述

輸入6個正整數,且這6個正整數中至少存在一個奇數和一個偶數。
設這6個正整數中最大的奇數爲a,最小的偶數爲b,求出|a-b|的值

輸入
輸入爲一行,6個正整數,且6個正整數都小於100
輸入保證這6個數中至少存在一個奇數和一個偶數

輸出
輸出爲一行,輸出最大的奇數與最小的偶數之差的絕對值

樣例輸入

第一組
1 2 3 4 5 6
第二組
1 6 3 8 5 10

樣例輸出

第一組
3
第二組
1

4.2 初始解答

這個提示錯誤答案了,但是我試了多組數據都沒問題。於是也按照之前的一樣改寫成沒有函數的形式了

#include <iostream>
#include <math.h>

using namespace std;

int result[2];

void calc(int no) {
    int length = 6, min = 999, max = 0, num;
    while (length--) {
        cin >> num;
        if (num % 2 != 0) {
            if (num > max) {
                max = num;
            }
        }
        if (num % 2 == 0) {
            if (num < min) {
                min = num;
            }
        }
    }
    result[no] = abs(max - min);
}

int main() {
    char str1[20], str2[20];

    cin >> str1;
    calc(0);
    cin >> str2;
    calc(1);

    cout << str1 << endl << result[0] << endl;
    cout << str2 << endl << result[1] << endl;
}

4.3 重做解答

#include <iostream>
using namespace std;
int main()
{
	int x=0;
	int oddmax = 0;
	int evenmin = 100;
	int result = 0;
	for (int i = 0; i < 6; i++)	{
		cin >> x;
		if (x % 2 == 0)	{
			if (x <evenmin) evenmin = x;
		}
		else {
			if (x >oddmax) oddmax = x;
		}
	}
	result = oddmax - evenmin;
	if (result < 0) result = -result;
	cout << result << endl;
    return 0;
}

編程題#5:分離整數的各個數位

注意: 總時間限制: 1000ms 內存限制: 65536kB

5.1 描述

從鍵盤輸入一個任意的三位整數,要求正確地分離出它的百位、十位和個位數,並分別在屏幕上輸出,輸出採用每行輸出一個數的方式,不帶其它符號。

輸入
一個任意的三位整數

輸出
一個任意的三位整數

樣例輸入

123

樣例輸出

1
2
3

5.2 解答

#include <iostream>
#include <math.h>
using namespace std;
int main() {
    int num, h, t, o;
    cin >> num;
    o = num % 10;
    t = (num % 100 - o) / 10;
    h = (num - o - 10 * t) / 100;
    cout << h << endl << t << endl << o << endl;
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章