中矿大新生赛 A 求解位数和【字符串】

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld

题目描述

给出一个数x,求x的所有位数的和。

输入描述:

第1行输入组数T,代表有T组数据。
第2-T+1行,每行输入一个数x。
输入数据保证:0≤x≤10200

输出描述:

每行输出对应行的数的位数和。
示例1

输入

2
10
101

输出

1
2
示例2

输入

2
111111111111111111111111111111111111
222222222222222222222222222222222222

输出

36
72

【代码】:
#include<bits/stdc++.h>
using namespace std;

int main()
{
    char s[250];
    int t,x;
    int ans;
    cin>>t;
    while(t--)
    {
        ans=0;
        scanf("%s",s);
        //printf("length = %d\n",strlen(s));
        for(int i=0;i<strlen(s);i++)
        {
             //if(s[i]==0) continue;
             ans+=(s[i]-'0')%10;
             x=s[i]-'0';
             x/=10;
        }
        printf("%d\n",ans);
    }
}
字符串

 

posted @ 2017-11-26 19:03  Roni_i  阅读(129)  评论(0编辑  收藏  举报