北京大学计算机系2009应试硕士生上机考试(AC)

一共A-G七道题:

A:

代码
/**********************************************************************************
1004 - Financial Management

Time Limit:
    1000ms
Memory limit:
    10000kB

题目描述
    Larry graduated this year and finally has a job. He's making a lot of money, but somehow never seems to have enough. Larry has decided that he needs to grab hold of his financial portfolio and solve his financing problems. The first step is to figure out what's been going on with his money. Larry has his bank account statements and wants to see how much money he has. Help Larry by writing a program to take his closing balance from each of the past twelve months and calculate his average account balance. 
输入
    The input will be twelve lines. Each line will contain the closing balance of his bank account for a particular month. Each number will be positive and displayed to the penny. No dollar sign will be included. 
输出
    The output will be a single number, the average (mean) of the closing balances for the twelve months. It will be rounded to the nearest penny, preceded immediately by a dollar sign, and followed by the end-of-line. There will be no other spaces or characters in the output. 
样例输入

    100.00
    489.12
    12454.12
    1234.10
    823.05
    109.20
    5.27
    1542.25
    839.18
    83.99
    1295.01
    1.75

样例输出

    $1581.42
*********************************************************************************
*/

/**********************************************************************************
解题思路:
平均数。。
*********************************************************************************
*/

#include 
<iostream>
#include 
<cmath>
#include 
<cctype>
#include 
<string>
#include 
<map>
#include 
<set>
#include 
<vector>
#include 
<algorithm>
#include 
<list>
//#include <stdlib.h>
//#include <iomanip>

using namespace std;

int main()
{
    
float money, sum = 0;

    
for (int i = 0; i < 12; i++)
    {
        cin 
>> money;
        sum 
+= money;
    }

    printf(
"$%.2f\n", sum/12.0);

    
return 0;
}
 


B:

代码
/**********************************************************************************
2977 - 生理周期

Time Limit:
1000ms
Memory limit:
65536kB

题目描述
人生来就有三个生理周期,分别为体力、感情和智力周期,它们的周期长度为23天、28天和33天。每一个周期中有一天是高峰。在高峰这天,人会在相应的方面表现出色。例如,智力周期的高峰,人会思维敏捷,精力容易高度集中。因为三个周期的周长不同,所以通常三个周期的高峰不会落在同一天。对于每个人,我们想知道何时三个高峰落在同一天。对于每个周期,我们会给出从当前年份的第一天开始,到出现高峰的天数(不一定是第一次高峰出现的时间)。你的任务是给定一个从当年第一天开始数的天数,输出从给定时间开始(不包括给定时间)下一次三个高峰落在同一天的时间(距给定时间的天数)。例如:给定时间为 10,下次出现三个高峰同天的时间是12,则输出2(注意这里不是3)。
输入
输入四个整数:p, e, i和d。 p, e, i分别表示体力、情感和智力高峰出现的时间(时间从当年的第一天开始计算)。d 是给定的时间,可能小于p, e, 或 i。 所有给定时间是非负的并且小于365, 所求的时间小于21252。
输出
从给定时间起,下一次三个高峰同天的时间(距离给定时间的天数)。
样例输入

0 0 0 0
0 0 0 100
5 20 34 325
4 5 6 7
283 102 23 320
203 301 203 40
-1 -1 -1 -1

样例输出

Case 1: the next triple peak occurs in 21252 days.
Case 2: the next triple peak occurs in 21152 days.
Case 3: the next triple peak occurs in 19575 days.
Case 4: the next triple peak occurs in 16994 days.
Case 5: the next triple peak occurs in 8910 days.
Case 6: the next triple peak occurs in 10789 days.
*********************************************************************************
*/

/**********************************************************************************
解题思路:

枚举。尽量减少判断次数,也就是缩小区间。
*********************************************************************************
*/

#include 
<iostream>
#include 
<cmath>
#include 
<cctype>
#include 
<string>
#include 
<map>
#include 
<set>
#include 
<vector>
#include 
<algorithm>
#include 
<list>
//#include <stdlib.h>
//#include <iomanip>

using namespace std;

int main()
{
    
int physical, emotion, intelligence, day, date, num = 1;
    cin 
>> physical >> emotion >> intelligence >> day;

    
while (physical >= 0)
    {
        date 
= day + 1;
        
while (date < 21252)
        {
            
if ((date-physical) % 23 == 0)
                
break;
            date
++;
        }
        
while (date < 21252)
        {
            
if ((date-emotion) % 28 == 0)
                
break;
            date 
+= 23;
        }
        
while (date < 21252)
        {
            
if ((date-intelligence) % 33 == 0)
                
break;
            date 
+= 23*28;  //23和28互素
        }
        cout 
<< "Case " << num << ": the next triple peak occurs in " << date-day << " days." << endl;
        cin 
>> physical >> emotion >> intelligence >> day;
        num
++;
    }

    
return 0;
}


C:

代码
/**********************************************************************************
2562 - Primary Arithmetic

Time Limit:
    1000ms
Memory limit:
    65536kB

题目描述
    Children are taught to add multi-digit numbers from right-to-left one digit at a time. Many find the "carry" operation - in which a 1 is carried from one digit position to be added to the next - to be a significant challenge. Your job is to count the number of carry operations for each of a set of addition problems so that educators may assess their difficulty.
输入
    Each line of input contains two unsigned integers less than 10 digits. The last line of input contains 0 0.
输出
    For each line of input except the last you should compute and print the number of carry operations that would result from adding the two numbers, in the format shown below. 
样例输入

    123 456
    555 555
    123 594
    0 0

样例输出

    No carry operation.
    3 carry operations.
    1 carry operation.
*********************************************************************************
*/

/**********************************************************************************
解题思路:
就是说两个数字相加要有几次进位。。
*********************************************************************************
*/

#include 
<iostream>
#include 
<cmath>
#include 
<cctype>
#include 
<string>
#include 
<map>
#include 
<set>
#include 
<vector>
#include 
<algorithm>
#include 
<list>
//#include <stdlib.h>
//#include <iomanip>

using namespace std;

int res[11];

int compute(string &str1, string &str2)
{
    
int ans = 0, len1 = str1.length(), len2 = str2.length();
    
//相加
    for (int i = len1-1, j = len2-1, k = 0; i >= 0; i--, k++, j--)
    {
        
if (j >= 0)
            res[k] 
= str1[i] - '0' + str2[j] - '0';
        
else
            res[k] 
= str1[i] - '0';
    }
    
//进位
    for (int i = 0; i < len1; i++)
    {
        
if (res[i] >= 10)
        {
            ans
++;
            res[i
+1]++;
        }
    }
    
return ans;
}

int main()
{
    
string str1, str2;
    
int ans;

    cin 
>> str1 >> str2;

    
while (str1 != "0" || str2 != "0")
    {
        
if (str1.length() >= str2.length())
            ans 
= compute(str1, str2);
        
else
            ans 
= compute(str2, str1);
        
if (ans == 0)
            cout 
<< "No carry operation." << endl;
        
else if(ans == 1)
            cout 
<< ans << " carry operation." << endl;
        
else
            cout 
<< ans << " carry operations." << endl;

        cin 
>> str1 >> str2;
    }

    
return 0;
}


原文地址:https://www.cnblogs.com/CCBB/p/1686658.html