88 条题解

  • 0
    @ 2023-3-20 13:15:19

    虽然不敢说是最短的,但前5短肯定可以。

    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
        int a;
        cin>>a;
        if (a<0) cout<<a*-1;//因为是负数,所以乘-1(“负负得正”)。
        else cout<<a;
        return 0;
    }
    

    等等,别走,这里还有更简单的。

    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
        int a;
        cin>>a;
        cout<<abs(a);
        return 0;
    }
    

    这个还能化简。

    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
        int a;cin>>a;cout<<abs(a);
        return 0;
    }
    

    呵呵,这个绝对是最短的

    #include <bits/stdc++.h>
    int main(){int a;std::cin>>a;std::cout<<abs(a);
    

    这个才是

    • @ 2023-4-22 16:00:14

      8行险胜=)=)=) 我是后室迷

      #include <bits/stdc++.h>
      using namespace std;
      int main()
      {
          long long a;cin>>a;//long long我的神
          cout<<abs(a);
          return 0;
      }
      
  • 0
    @ 2023-2-24 22:36:32
    #include<cstdio>
    #include<iostream>
    #include<cmath>
    using namespace std;
    int main()
    {
        int a,d,n;
        cin>>n;
        a=-n;
        d=abs(a);
        cout<<d<<endl;
        return 0;
    }
    

    #include<cstdio> #include<iostream>

    • 0
      @ 2023-2-23 18:23:32
      #include <bits/stdc++.h> 
      using namespace std;
      int main()
      {
          long long a,n;
          cin>>n;
          a=n;
          if(n>=0)
          {
            cout<<n;
          }
          else
          {
            cout<<-n;
          }
          return 0;
      }
      
      • 0
        @ 2023-2-22 19:43:20
        #include <bits/stdc++.h>
        using namespace std;
        int n;
        int main()
        {
            cin>>n;
            cout<<abs(n);
            return 0;
        }
        
        • 0
          @ 2023-2-22 19:42:07

          #include <bits/stdc++.h> using namespace std; int n; int main() { cin>>n; cout<<abs(n); return 0; }

          • 0
            @ 2023-2-19 17:33:59

            #include <iostream> using namespace std; int main() { int n; cin>>n; if(n>=0) { cout<<n; } else { cout<<n*-1; } return 0; }

            • 0
              @ 2023-2-12 12:22:24
              #include<iostream>
              using namespace std;
              int main()
              {
                  int q;
                  cin>>q;
                  if(q>=0)
                  {
                      cout<<q;
                  }
                  else
                  {
                      cout<<q*-2*-3*-4/24;
                  }
                  return 0;
              } 
               😄 ~~*~~~~
              
              
              • 0
                @ 2023-2-4 9:46:56
                #include <bits/stdc++.h>
                using namespace std;
                int main()
                {
                    int n;
                    cin >> n;
                    if (n>=0)
                    {
                        cout << n;
                    }
                    else
                    {
                        cout << n*-1;//注意!不是-n或cout << "-" << n;
                    }
                    return 0;
                }
                

                先给赞,在看,栓q very much

                • 0
                  @ 2023-1-8 21:50:48

                  直接上一个abs就好了 abs(n)就是n的绝对值

                  • 0
                    @ 2023-1-3 21:49:34
                    #include <bits/stdc++.h>
                    using namespace std;
                    int n;
                    int main()
                    {
                        cin>>n;
                        cout<<abs(n);
                        return 0;
                    }
                    

                    ???

                    • 0
                      @ 2022-12-3 21:11:50
                      #include <bits/stdc++.h>//hetao3097453
                      using namespace std;//用abs求绝对值函数
                      int main()
                      {
                          int n;
                          cin >> n;
                          cout << abs(n);
                          return 0;
                      }
                      
                      
                      • 0
                        @ 2022-11-6 13:03:00
                        #include <bits/stdc++.h> 
                        using namespace std;
                        int main()
                        {
                            long long a,n;
                            cin>>n;
                            a=n;
                            if(n>=0)
                            {
                              cout<<n;
                            }
                            else
                            {
                              cout<<-n;
                            }
                            return 0;
                        }
                        
                        • 0
                          @ 2022-10-16 9:40:28
                          #include <bits/stdc++.h>
                          #define ll long long//宏替换。
                          using namespace std;
                          ll n;
                          int main()
                          {
                              ios::sync_with_stdio(0);//关闭流同步,加速cout。
                              cin >> n;
                              cout << abs(n);//求绝对值函数。
                              return 0;
                          }
                          
                          • 0
                            @ 2022-10-3 13:48:49
                            #include<bits/stdc++.h>
                            using namespace std;
                            int main()
                            {
                            	int n;
                            	cin>>n;
                            	cout<<abs(n)<<endl;
                            	return 0;
                            }
                            

                            首先: 因为题目中间说是一个正整数或者是负整数,所以可以直接使用abs。 其次: 如果时间要求不是特别高,完全用不着使用abs专用头,但这里也放在这里。 #include<cmath> 总而言之,这道题还是很简单的啦🚀️ 🚀️ 🎉️ 🎉️

                            • 0
                              @ 2022-9-27 22:52:06
                              # include <bits/stdc++.h>
                              using namespace std;
                              int main () 
                              {
                                  int n;    
                                  cin>>n;
                                  if (n<0)
                                  {
                                      cout<<0-n<<endl;
                                  }
                                  else
                                  {
                                      cout<<n<<endl;
                                  }
                                  return 0;
                              }
                              
                              • 0
                                @ 2022-9-1 20:54:47

                                负数判断是十分简单的,改为绝对值也不用这么麻烦,毕竟 负负得正

                                #include <bits/stdc++.h>
                                using namespace std;
                                int main()
                                {
                                    int a;
                                    cin >> a;
                                    if (a < 0)//判断是否是负数
                                    {
                                        cout << -a;//负负得正
                                    }
                                    else
                                    {
                                        cout << a;//这里也包括0了
                                    }
                                    return 0;
                                }
                                
                                • 0
                                  @ 2022-8-29 11:36:25

                                  #include<bits/stdc++.h> using namespace std; int main(){ int n; cin >> n; if (n >= 0){ cout << n; } else{ cout << 0 - n; } return 0; }🎉️ 🚀️

                                  • 0
                                    @ 2022-8-29 9:57:32
                                    #include <iostream>
                                    
                                    int main()
                                    {
                                        using namespace std;
                                    
                                        long long n;
                                        cin >> n;
                                        n >= 0 ? cout << n : cout << 0 - n;
                                        
                                        return 0;
                                    }
                                    

                                    依旧是三目 如果n是正数直接输出,否则输出0 - n(负负得正,0 - 负数的n 相当于 0 + 正数的n)

                                    • 0
                                      @ 2022-8-19 20:33:09

                                      我总结了一下几位同学的答案(和我自己的),共有以下几种方式: 第一种(不用函数)

                                      #include <bits/stdc++.h>
                                      using namespace std;
                                      int main()
                                      {
                                          int n;
                                          cin >> n;
                                          if (n >= 0)/*这里0可以直接输出*/
                                          {
                                              cout << n;
                                          }
                                          else
                                          {
                                              cout << 0 - n;/*因为是负数,
                                              减掉负数就是加正数*/
                                              /*cout << n * -1; */
                                              /*这是另一种,负数乘负数,
                                              负负得正,乘一结果不变*/
                                          }
                                          return 0;
                                      /*用这种方法的人不少,我就不专门写明作者了*/
                                      

                                      第二种(使用fabs函数和abs函数)

                                      #include <bits/stdc++.h>
                                      using namespace std;
                                      int main()
                                      {
                                          int a;
                                          cin >> a;
                                          cout << fabs(a); 
                                          /*cout << abs(a)*/
                                          return 0;
                                      }
                                      /*fabs是hetao794786的答案,好方便,确实厉害
                                      (abs的人也不少,我就不专门写明了)
                                      但是fabs函数和abs函数我不知道具体功能,我会去查的。*/
                                      

                                      第三种

                                      #include <iostream> 
                                      #include <cmath> 
                                      using namespace std;
                                      int main() 
                                      {
                                          int m;
                                          cin>>m;
                                          cout<<sqrt(m*m);
                                          return 0;
                                      }
                                      /*这是hetao23102384的答案
                                      开方我也想这么做的,然后做了个n * n / n然后输入负数输出还是负数,我人傻了,想了想,是我自己蠢了。*/
                                      

                                      好了,就总结这么多,如果有错请指出,我刚学C++错了正常,这么多方法我只试过一个。

                                      • 0
                                        @ 2022-8-18 9:17:35
                                        #include <iostream>
                                        using namespace std;
                                        int main()
                                        {
                                            int n;
                                            cin >> n;
                                            if (n >= 0)
                                            {
                                                cout << n;
                                            }
                                            else
                                            {
                                                cout << 0 - n;
                                            }
                                        }
                                        

                                        信息

                                        ID
                                        345
                                        时间
                                        1000ms
                                        内存
                                        16MiB
                                        难度
                                        5
                                        标签
                                        递交数
                                        10963
                                        已通过
                                        4510
                                        上传者