【qduoj】C语言_求整数各位数之和
生活随笔
收集整理的這篇文章主要介紹了
【qduoj】C语言_求整数各位数之和
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題干:
?
描述
?
輸入一個正整數r(0<r<10),做r次下列運算:輸入一個整數,輸出它的位數及各位數之和。
輸入
?
同上
輸出
?
同上
輸入樣例 1?
3 123456 -100 99輸出樣例 1
number=6,sum=21 number=3,sum=1 number=2,sum=18?
解題報告:
? ? ? ? 注意這題開longlong也是不夠的,,因為沒說整數有多大,所以用字符串讀入這個整數并進行處理。
AC代碼:
#include<cstdio> #include<queue> #include<cstring> #include<cmath> #include<map> #include<iostream> #include<algorithm> #define ll long long const ll mod = 1e9+7; using namespace std; char s[1000005]; int main() {ll t;cin>>t;ll ans2 = 0,ans1;while(t--) {ans1 = ans2 = 0;scanf("%s",s);if(s[0] == '-') {int len = strlen(s);ans1 = len-1;for(int i = 1; i<len; i++) {ans2 += s[i] - '0';}printf("number=%lld,sum=%lld\n",ans1,ans2);}else {int len = strlen(s);ans1 = len;for(int i = 0; i<len; i++) {ans2 += s[i] - '0';}printf("number=%lld,sum=%lld\n",ans1,ans2);}}return 0 ; } Share?
總結
以上是生活随笔為你收集整理的【qduoj】C语言_求整数各位数之和的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: qcwlicon.exe - qcwli
- 下一篇: qctray.exe - qctray进