码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-11-26 19:38:39      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:opened   一个   isp   closed   strlen   describe   mes   nbsp   示例   

时间限制: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);
    }
}
字符串

 

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

标签:opened   一个   isp   closed   strlen   describe   mes   nbsp   示例   

原文地址:http://www.cnblogs.com/Roni-i/p/7899858.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!