期末考试补充测验.ppt_第1页
期末考试补充测验.ppt_第2页
期末考试补充测验.ppt_第3页
期末考试补充测验.ppt_第4页
期末考试补充测验.ppt_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

期末考试补充测验,1. Sum of even numbers,2,Description Given a number N (even or odd), calculate 2 + 4 + . + n (n is the largest even number smaller than N). Print out the sum. Input The first line is an integer m, indicating the number of test cases. Then follow m lines. The i line contains a positive number N_i. Output For each number N_i, print the sum of 2 + 4 + . + n_i (n_i is the largest even number smaller than N_i) in a single line. Sample Input 2 5 8 Sample Output 6 20,1. Sum of even numbers,3,#include using namespace std; int main() int count, num, sum; cincount; for(int i =0; inum; sum =0; for(int j=2; j=num; j+=2) sum+=j; coutsumendl; ,2. Grade Record Management,4,Description There are some students grade records, in format of “name grade”. In order to ease the view and management of the records easily, you are asked to develop a program to print out the records according to the grades. That is, the names are printed in the order of grades. Input Totally, there are 8 records and each record occupies a line. Only letters and digits appear in the name of a student (i.e. there will be no space in a name). Each name contains at most 20 letters or (and) digits. All grades are integers in the range of 0100. Output Each grade per line, followed by the students having this grade. For students having the same grade, they are listed in the same line, with the same order as in the input. Hints: You can use two arrays to store the names and grades. Sample Input WangYi 90 LiMing 90 HuRui 50 ZhangLi 65 LiLei 80 ZhangFei 65 ZhaoSan 70 ChenZi 100 Sample Output 100 ChenZi 90 WangYi LiMing 80 LiLei 70 ZhaoSan 65 ZhangLi ZhangFei 50 HuRui,2. Grade Record Management,5,#define SIZE 8 #define LEN 25 int main() char nameSIZELEN; int gradeSIZE; for (int i = 0; i namei; cin gradei; /rank for (int i = SIZE - 1; i 0; - i) for (int j = 0; j gradei + 1) cout endl; ,期末考试,1. Calculation of Income,7,Description The monthly salary of a person is increased by a fixed percentage every year. With the current salary and target amount given, please calculate how many years are needed to reach or just exceed the target. Input There are multiple test cases. Each line contains one case (three integers). The first number in a line is the current monthly salary, the second number is the percentage of increase per year and the third one is the target amount. The initial monthly salary is no less than 100 and it is always an integral value even after the increase every year. For example, the number 160 will change to be 164 after increase by 3%. The percentage is a number no less than 1. The target amount is no more than 1000000. The line with “0 0 0” implies the end of input. Output For each test case, print out in a single line the number of years needed, in integer. Sample Input 300 5 10000 120 1 150000 0 0 0 Sample Output 3 79,Calculation of Income,8,int main() int sal, pcnt, target, sum; int yr; cinsalpcnttarget; while(sal0) yr = 1; sum = 12*sal; while(sumsalpcnttarget; ,2. String Reversion,9,Description Given a valid identifier in C programs, please write a program to reverse it by respectively reversing two parts separated by _. Input The first line is an integer m, indicating the number of test cases. Then there are m lines and each line contains a string. A string will contain no more than 100 characters and there is one or less _. Output For each test case, print out the string reversed. Sample Input 2 John_Smith int_45 _me Sample Output nhoJ_htimS tni_54 _em,String Reversion,10,void reversePart(char, int, int); /reverse part of the string int main() int count; cincount; for(int x=0; xstr; for(int i =0; ihigh) return; for(int i = low; i=(low+high)/2; i+) tmp = stri; stri = strlow+high-i; strlow+high-i=tmp; ,3. Count of Consecutive Numbers,11,Description Given an sequence of digits, 09, you are required to count the maximum number of consecutive occurrences of each digit and sort the digits in the descendant order of occurrence number. If two digits have the same maximum number of consecutive occurrences, the larger digit should be put before the smaller. Input There are multiple test cases and each is given in one line. In each test case, there are at most 100,000 digits. The character # indicates the end of one line. The line begins with # means the end of all the input. Output For each test case, please print out the digits that appear and the maximum consecutive occurrence number, in the descendant order of the later, as described above. The printout of each test case is put in one line, in the format as follows: digit1(occurrence number1), digit2(occurrence number2), Sample Input 011000888993# 33922# # Sample Output 8(3),0(3),9(2),1(2),3(1) 3(2),2(2),9(1),Count of Consecutive Numbers,12,int findMax(int2, int); /find the maximum occurrence int main() int count102=; char ch; int digit, last; cinch; while(ch!=#) /Initialze vairalbe for each line of input for(int i =0; i-1,/Sort the numbers int max, tmp1, tmp2; /Set the second column to be digits 09; for(int i = 0; i0) cout0) coutch; ,int findMax(int count2, int low) int max = low; for(int i = low+1; icountmax0) max = i; else if(counti0=countmax0 ,4. Change to upper case,13,Description Given a line contains multiple words, change the first character in each word to upper case. Input A line containing multiple words. The line contains less than 100 characters. Output The same line with the first character of each word being upper case. Sample Input Please change me to upper-case. Sample Output Please Change Me To Upper-case.,Change to upper case,14,int length(char * string1) for (int i = 0; ; + i) if (string1i = 0) return I; char toUpperCase(char c) if (c = a ,5. Cycling the words,15,Description Given a line contains N words, print the N lines. The first line is the same as the input. After printing each line, the first word of this line in put to the end of the next line. Input A line contains N words. N is not given in the problem: you need to count the number of words in the input line to find it out. The line contains less than 100 characters. Output N lines. The first line equals the input. For the rest N -1 lines, the first word in a line in put to the end in the next line. Sample Input Please think about it carefully Sample Output Please think about it carefully think about it carefully Please about it carefully Please think it carefully Please think about carefully Please think about it,Cycling the words,16,int length(char * string1) for (int i =

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论