通用版1.M - 枚举答案能不能做?

xiaoxiao2021-02-28  23

#include <stdio.h> #include <string.h> int main() { int i,n,t; char s[10000]; while(gets(s)){ if(strcmp(s,"0")==0)break; n=strlen(s); int ans=0; for(i=0;i<=n-1;i++){ ans+=s[i]-'0'; } while(ans>=10){ t=ans; ans=0; while(t){ ans+=t; t=t/10; } } printf("%d\n",ans); } return 0; }

把数字先存入数组中,加起来(字符型应该每次-'0'),这些数字加起来一定不会超出int范围,所以之后就不用再存入数组

每次对该数字对10取余,将余数加起来,然后将该数字除以10,一直这么重复直到该数字变为个位数

转载请注明原文地址: https://www.6miu.com/read-2623856.html

最新回复(0)