Topic 1003:a+b

Source: Internet
Author: User

This problem is not very difficult, but to understand the different types of conversion. Talk less, first on the code

#include <iostream>#include<string>using namespacestd;intMain () {stringb;  while(cin>>a>>B) {        Longnum1=0, num2=0;  for(intI=0; I<a.length (); i++){            if(a[i]>='0'&& a[i]<='9') {NUM1=num1*Ten+a[i]-'0'; }        }         for(intI=0; I<b.length (); i++){            if(b[i]>='0'&& b[i]<='9') {num2=num2*Ten+b[i]-'0'; }        }        if(a[0]=='-') {NUM1=0-NUM1; }        if(b[0]=='-') {num2=0-num2; } cout<<num1+num2<<Endl; }    return 0;}/************************************************************** problem:1003 user:hg2013211626 language:c++ result:accepted time:0 Ms memory:1520 kb****************************************************************/

According to the requirements of the topic, each of the three numbers is separated, so I use string input here. Before I defined two long data, the first step of the program was to convert the string type to a long type. Here I use a For loop, in order to determine the string of each letter, if the content is greater than 0 less than 9, then convert it to a number, the specific method to see above (very ingenious method) and then directly calculated.

Topic 1003:a+b

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.