zoukankan      html  css  js  c++  java
  • PTA A1005&A1006

    第三天

    A1005 Spell It Right (20 分)

    题目内容

    Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English.

    Input Specification:

    Each input file contains one test case. Each case occupies one line which contains an N (≤10100).

    Output Specification:

    For each test case, output in one line the digits of the sum in English words. There must be one space between two consecutive words, but no extra space at the end of a line.

    Sample Input:

    12345

    Sample Output:

    one five

    单词

    consecutive

    英 /kən'sekjʊtɪv/ 美 /kən'sɛkjətɪv/
    adj. 连贯的;连续不断的

    compute

    英 /kəm'pjuːt/ 美 /kəm'pjʊt/
    n. 计算;估计;推断
    vt. 计算;估算;用计算机计算
    vi. 计算;估算;推断

    题目分析

    这题还是很简单的,10的100次方显然超过了int的范围,long int可能都不行,所以这题肯定不能用处理数字的方法解决,所以我把这个数字看作字符,相加时把每个字符减去一个'0',然后再用sprintf把数字改为字符串,代码如下。

    具体代码

    #include<stdio.h>
    #include<stdlib.h>
    
    const char *ch[] = { "zero","one","two","three","four","five","six","seven","eight","nine" };
    int N;
    int main(void)
    {
    	char c[101];
    	int i = 0;
    	while ((c[i] = getchar()) != '
    ')i++;
    	int sum = 0;
    	while (i--)
    	{
    		sum += c[i] - '0';
    	}
    	char c2[101];
    	sprintf(c2, "%d", sum);
    	printf("%s", ch[c2[0] - '0']);
    	i = 1;
    	while (c2[i] != '')
    	{
    		printf(" %s", ch[c2[i] - '0']);
    		i++;
    	}
    	system("pause");
    }
    

    A1006 Sign In and Sign Out (25 分)

    题目内容

    At the beginning of every day, the first person who signs in the computer room will unlock the door, and the last one who signs out will lock the door. Given the records of signing in's and out's, you are supposed to find the ones who have unlocked and locked the door on that day.

    Input Specification:

    Each input file contains one test case. Each case contains the records for one day. The case starts with a positive integer M, which is the total number of records, followed by M lines, each in the format:
    ID_number Sign_in_time Sign_out_time
    where times are given in the format HH:MM:SS, and ID_number is a string with no more than 15 characters.

    Output Specification:

    For each test case, output in one line the ID numbers of the persons who have unlocked and locked the door on that day. The two ID numbers must be separated by one space.
    Note: It is guaranteed that the records are consistent. That is, the sign in time must be earlier than the sign out time for each person, and there are no two persons sign in or out at the same moment.

    Sample Input:

    3
    CS301111 15:30:28 17:00:10
    SC3021234 08:00:00 11:25:25
    CS301133 21:45:00 21:58:40

    Sample Output:

    单词

    题目分析

    过于简单,无话可说。

    具体代码

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #define EARLY 1
    #define LATE -1
    
    int M;
    struct time
    {
    	int h;
    	int m;
    	int s;
    };
    
    struct time earliest_time;
    struct time lastest_time;
    
    char earliest_id[16];
    char lastest_id[16];
    
    int compare_time(struct time t1, struct time t2);
    
    int main(void)
    {
    	earliest_time.h = 23;
    	earliest_time.m = 59;
    	earliest_time.s = 59;
    	lastest_time.h = 0;
    	lastest_time.m = 0;
    	lastest_time.s = 0;
    	scanf("%d", &M);
    	while (M--)
    	{
    		struct time in;
    		struct time out;
    		char id[16];
    		scanf("%s %d:%d:%d %d:%d:%d", id, &in.h, &in.m, &in.s, &out.h, &out.m, &out.s);
    		if (compare_time(in, earliest_time) == EARLY)
    		{
    			earliest_time.h = in.h;
    			earliest_time.m = in.m;
    			earliest_time.s = in.s;
    			strcpy(earliest_id, id);
    		}
    		if (compare_time(out, lastest_time) == LATE)
    		{
    			lastest_time.h = out.h;
    			lastest_time.m = out.m;
    			lastest_time.s = out.s;
    			strcpy(lastest_id, id);
    		}
    	}
    	printf("%s %s", earliest_id, lastest_id);
    	system("pause");
    }
    
    int compare_time(struct time t1, struct time t2)
    {
    	if (t1.h > t2.h)
    		return LATE;
    	else if (t1.h < t2.h)
    		return EARLY;
    	else
    	{
    		if (t1.m > t2.m)
    			return LATE;
    		else if (t1.m < t2.m)
    			return EARLY;
    		else
    		{
    			if (t1.s > t2.s)
    				return LATE;
    			else if (t1.s < t2.s)
    				return EARLY;
    		}
    	}
    }
    
  • 相关阅读:
    SQLServer性能杀手
    SqlServer内存瓶颈分析SQLServer:Buffer Manager
    HTML5变化 (一)
    对于using嵌套的改进
    String.IsNullOrEmpty()和String.IsNullOrWhiteSpace()
    querySelector & querySelectorAll
    Action<T> 委托
    Func<T, TResult>
    SL4.背景图片拖动
    JavaScript日志
  • 原文地址:https://www.cnblogs.com/z-y-k/p/11522606.html
Copyright © 2011-2022 走看看