13 条题解

  • 5
    @ 2023-6-23 14:52:59

    解析

    输入一个整数,把每个数位分离出来,再累加到sum中。

    题解

    #include <iostream>
    using namespace std;
    int main()
    {
        int n;
        int sum = 0;
        cin >> n;
        while(n > 0)
        {
            sum += n % 10;
            n /= 10;
        }
        cout << sum;
        return 0;
    }
    
    
    • 3
      @ 2023-6-24 14:10:43
      #include <iostream>
      using namespace std;
      int main()
      {
          int n,q,a;
          q = 0;
          cin >> n;
          while(n > 0)
          {
              a = n % 10;
              q += a;
              n /= 10;
          }
          cout << q;
          return 0;
      }
      

      解析:

      1.因为数位不一定,所以我们使用 while 循环。

      2.设定一个统计变量(我随手点的 q )

      3.给 while 加一个条件:(n > 0)

      (别忘了给下级代码加大括号)

      我第一次就翻车在这里

      那个大聪明会在这翻车啊?

      4.用模号(%)取除以10后的余数(即个位数) q 加上余数。

      5.n 除以10,因为 n 是整数类型,所以不用担心出现小数。

      6.while 会一直执行下级代码直到 n 不大于0,循环结束后 q 中存储的就是个数位之和了,所以我们直接输出 q 就行了。

      7.看到这里就点个赞吧!👍

      • 2
        @ 2024-2-28 21:36:21

        字符串

        字符串做更简单 字符与数字是可以转换的。

        #include <bits/stdc++.h>
        using namespace std;
        string s;
        int main()
        {
            cin>>s;
            int cnt=0,len=s.size();
            for (int i=0;i<len;i++){
                cnt+=s[i]-'0';
            }
            cout<<cnt;
            return 0;
        }
        
        • 2
          @ 2024-2-1 17:52:44
          #include <iostream>
          using namespace std;
          int main()
          {
              int n,s=0;
              cin>>n;
              while(n!=0){
                  s+=n%10;
                  n/=10;
              }
              cout<<s;
              return 0;
          }
          
          
          • 1
            @ 2024-6-5 20:40:20

            题解 c++

            #include <bits/stdc++.h> //hetao4040809
            using namespace std;
            int main()
            {
                long long n,sum=0;
                cin>>n;
                while(n>0)
                {
                    sum+=n%10;
                    n/=10;
                }
                cout<<sum;
                return 0;
            }
            
            • 1
              @ 2024-5-12 20:42:57
              #include <iostream>
              using namespace std;
              int main()
              {
                  int n, ans = 0;
                  cin >> n;
                  while (n > 0)
                  {
                      ans += n % 10;
                      n /= 10;
                  }
                  cout << ans;
                  return 0;
              }
              //by @hetao3875363
              
              • 0
                @ 2024-5-24 17:13:19
                #include<iostream>
                #include<cstdio>
                #include<cmath>
                #include<iomanip>
                using namespace std;
                int main()
                {
                    int a,b;
                    cin >> a;
                    b = 0;
                    while (a > 0.1)
                    {
                        b += a % 10;
                        a /= 10;
                    }
                    cout << b;
                    return 0;
                }
                
                • 0
                  @ 2024-5-11 22:36:54

                  ` hhh

                  #include<iostream>
                  using namespace std;
                  int main()
                  {
                      int n,sum=0;
                      cin >> n;
                      while(n>0)
                      {
                          sum += n%10;
                          n /= 10;
                      }
                      cout << sum;
                      return 0;
                  }
                  
                  • 0
                    @ 2023-8-8 22:43:39

                    闲的没事干,就像编一个——~~这叫啥来的?奥对,~~题解!(bits/stdc++.h这个头文件是万能头文件 )第一个题解,以后吗,编不编还不知道呢…… HXmagician

                    #include <bits/stdc++.h>
                    using namespace std;
                    int main()
                    {
                        int n;
                        int sum = 0;
                        cin >> n;
                        while(n > 0)
                        {
                            sum += n % 10;
                            n /= 10;
                        }
                        cout << sum;
                        return 0;
                    }
                    
                    • 0
                      @ 2023-7-20 19:54:58

                      #include <iostream>

                      using namespace std;

                      int main()

                      {

                      int n,sum = 0;
                      
                      cin >> n;
                      
                      while (n > 0)
                      
                      {
                      
                          sum += n % 10;//把当前数位(n最后一位)的数字加上去
                      
                          n /= 10;//向前进一位,即把n删去一位
                      
                      }
                      
                      cout << sum;
                      
                      
                      return 0;
                      

                      }

                      • -1
                        @ 2023-7-8 18:25:11

                        解析

                        输入一个整数,把每个数位分离出来,再累加到sum中。😄 😄

                        #include <iostream>
                        using namespace std;
                        
                        int main()
                        {
                           int n,sum = 0;
                            cin >> n;
                            while (n > 0)
                            {
                                sum += n % 10;
                                n /= 10;
                            }
                            cout << sum;
                            return 0;
                        }
                        
                        • -1
                          @ 2023-6-23 21:21:01

                          先看题目

                          题目说求各个数位上的数字之和,拆分成具体的步骤就是先进行数位分离,然后进行求和,根据提示可得int是完全够用的,将这些思路整合亿下就成答案了

                          #include <iostream>
                          using namespace std;
                          
                          int main()
                          {
                              int n,sum = 0;
                              cin >> n;
                              while (n > 0)
                              {
                                  sum += n % 10;
                                  n /= 10;
                              }
                              cout << sum;
                              return 0;
                          }
                          
                          • -2
                            @ 2023-6-24 16:56:38

                            怎么没人发?我来!😄

                            #include <iostream>
                            using namespace std;
                            int main()
                            {
                                int n, sum = 0;  
                                cin >> n;
                                while (n > 0)   
                                {
                                    sum += n % 10;  
                                    n /= 10;
                                }
                                cout << sum;
                                return 0;
                            }
                            

                            代码不难,不会我们捋一捋思路回看课程,老师有法宝!

                            (把数位岔开,一个一个加入sum中就好了)

                            点个赞呗👍

                            • 1

                            信息

                            ID
                            222
                            时间
                            1000ms
                            内存
                            256MiB
                            难度
                            4
                            标签
                            递交数
                            1890
                            已通过
                            898
                            上传者