zoj1115题目不难,由于数字较大,所以用字符串存储。还是不熟练,多多练习,就会好多了。
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- int charinit(const char *p)
- {
- char *q=p;
- int i=0;
- while(*q != '/0')
- {
- i+=*q-'0';
- q++;
- }
- return i;
- }
- int root(int x)
- {
- if(x<10)
- return x;
- else
- {
- int z=0;
- while(x>=10)
- {
- z=z+x%10;
- x/=10;
- }
- z+=x;
- root(z);
- }
- }
- int main()
- {
- char *a=malloc(1024);
- int i,j;
- while(scanf("%s",a)!=EOF && strcmp(a,"0")!=0)
- {
- i=charinit(a);
- j=root(i);
- printf("%d/n",j);
- }
- return 0;
- }