Exercise 3-3 the last 3 bits of the product

Source: Internet
Author: User

Enter a number of integers (which can be positive, negative, or 0) to output the last 3 bits of their product. These integers are mixed with a string of uppercase letters, and your program should ignore them. Tip: Try to enter a string when executing scanf ("%d").

Problem Solving Ideas:
Find out the number multiplier in the string, note the end of the string in the calculation process and overflow the exception.

 #include <stdio.h> #include <string.h> #include <ctype.h> int main () {
    Char strarr[1000];
    scanf ("%[^\n]s", Strarr);
    int len = strlen (Strarr);
    int i;
    int flag = 1;
    int sum = 1;
    int ret = 1;
        for (i = 0; i < len; i++) {flag = 1;
        sum = 1;
        while (!isdigit (Strarr[i]) && strarr[i]! = '-' && strarr[i]! = ' \ n ') {i++;
        } if (strarr[i] = = '-') {flag =-flag;
            } if (IsDigit (Strarr[i])) {sum = strarr[i]-' 0 ';
            i++;
                while (IsDigit (Strarr[i])) {sum%= 1000;
                sum = sum * ten + strarr[i]-' 0 ';
            i++;
    }} RET = flag * Sum * ret% 1000;
    } printf ("%d\n", ret);  
return 0; }  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.