编程介的小学生 2019-06-04 13:58 采纳率: 0.4%
浏览 134

合并处理zip code的问题,利用C语言的程序的编写的代码制作的程序怎么实现的?

Description

An organization that wishes to make a large mailing can save postage by following U.S. Postal Service rules for a bulk mailing. Letters in zip code order are bundled into packets of 10-15 letters each. Bundles may consist of letters in which all 5 digits of zip code are the same (5-digit bundles), or they may consist of letters in which only the first 3 digits of zip code are the same (3-digit bundles). If there are fewer than 10 letters to make up a bundle of either type, those letters are mailed first class.
Input

You are to write a program to read a data set of 5-digit zip codes, one per line, until end of input. Your program should count the number of 5-digit bundles, 3-digit bundles, and first class letters. You should include as many letters as possible in 5-digit bundles first, then as many as possible in 3-digit bundles, with as few bundles of 10 to 15 letters as possible. For example, if there are 31 letters with the same zip code, they must be combined into exactly three 5-digit bundles.
Not all zip codes in the data set will be valid. A valid zip code consists of exactly 5 digits (0-9), all of which cannot be 0. Non-numeric characters are not allowed. At the end of your output, print the invalid zip codes found. (Duplicates need only be printed once.)
Output

Print a report that lists 5-digit zip code bundles first, with the number of letters and number of bundles for each zip code. Next list all 3-digit zip code bundles with the same two counts, followed
by all zip codes that are not bundled and to be sent first class. At the end print totals of letters and bundles, followed by the number of invalid zip codes and a list of these. Single space the report, and print blank lines following the heading, before the total line, and between the three groups of zip codes. For 3-digit bundles, print the zip codes in the form dddxx, where ddd represents the three significant digits and xx represents the last two digits to be omitted. Your output should be similar to that shown in the sample.
Sample Input

95864
95864
95864
95867
95920
9j876
95616

95616

95747

95814

95818

95818

8976
95818

95818
95819

95819

00000
95819

95819

95819
95819

95819

95825
95825
95825
95825
95825
95826
95826
95826
95826
95826
95826
95827
8976
95833
95833
95833
95833
95819

95819
95819
95819
95833
95833
95833
95864
95864
95864
123456
95864
95864
95864
95864
Sample Output

ZIP LETTERS BUNDLES

95819 11 1
95864 10 1

958xx 25 2

95616 2 0
95747 1 0
95920 1 0

TOTALS 50 4

INVALID ZIP CODES

9j876
8976
00000
123456

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-10 02:19
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    这是一个简单的 C 语言程序,它实现了给定的功能。请注意,这个程序没有进行任何错误检查,因此在实际使用中可能无法处理所有输入。

    #include <stdio.h>
    
    int main() {
        char line[100];
        int five_digit_bundles = 0;
        int three_digit_bundles = 0;
        int first_class_letters = 0;
    
        while (fgets(line, sizeof(line), stdin)) {
            // Convert the string to an integer
            int zip_code = atoi(line);
    
            // Check if the zip code is valid
            if (!isxdigit(zip_code) || zip_code == 0) {
                printf("%s\n", line);
                continue; // Skip this invalid zip code
            }
    
            // Count the number of five-digit bundles
            if (zip_code >= 10000 && zip_code <= 99999) {
                five_digit_bundles++;
            }
            
            // Count the number of three-digit bundles
            else if (zip_code >= 1000 && zip_code <= 9999) {
                three_digit_bundles++;
            }
    
            // Count the number of first-class letters
            else if (zip_code > 9999) {
                first_class_letters++;
            }
        }
    
        // Print the results
        printf("ZIP LETTERS BUNDLES\n");
        printf("%d %d\n", five_digit_bundles, three_digit_bundles);
        printf("%d %d\n", first_class_letters, five_digit_bundles + three_digit_bundles - first_class_letters);
        
        // Print the invalid zip codes
        printf("\nINVALID ZIP CODES\n");
        for (int i = 0; i < strlen(Invalid_zip_codes); i++) {
            printf("%c", Invalid_zip_codes[i]);
        }
        
        return 0;
    }
    

    注意:这个程序将只读取一行输入,并且不进行任何验证或清理工作。在实际应用中,你可能需要添加更多的逻辑来处理文件输入和输出。

    评论

报告相同问题?