ACM_absolute value

Don't give me 100 bucks

Time Limit: 2000/1000ms (Java/Others)

Problem Description:

  Today is Guangcai's ACM Weekly Competition. Xiaoguang came to Guangcai's experimental building and wanted to get some Sutuo points.
Xu Nengshen: "I came all the way to question A, took blood, and didn't give me 100 yuan"
"What am I robbing you, am I the one who robs you? Shameless and still smiling, don't give me 100 yuan"
。。。。。。
Xiaoguang pushed aside the crowd and finally knew what the topic of Xu Nengshen's blood was:
Given a number, find its absolute value.

Input:

The input contains sets of test data, each set of data contains an integer (in the range of long long, %lld).

Output:

For each set of data, output the answer on one line.

Sample Input:

8
-2

Sample Output:

8
2 
Problem-solving ideas: There is a trap in this question, that is, the absolute value of the minimum value of a negative number is 1 larger than the maximum positive number. Using abs directly will cause overflow, so it is processed with a string. It is enough to open 30 bits in length, long long 20 bits
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main()
 4 {
 5     char s[30];
 6     while(cin>>s){
 7         if(s[0]!='-')cout<<s<<endl;
 8         else{
 9             for(int i=1;i<(int)strlen(s);++i)cout<<s[i];
10             cout<<endl;
11         }
12     }
13     return 0;
14 }
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325173719&siteId=291194637