我来我网
https://5come5.cn
 
您尚未 登录  注册 | 菠菜 | 软件站 | 音乐站 | 邮箱1 | 邮箱2 | 风格选择 | 更多 » 
 

本页主题: 上一届校赛的题解,我完全服了 显示签名 | 打印 | 加为IE收藏 | 收藏主题 | 上一主题 | 下一主题

yinx



性别: 帅哥 状态: 该用户目前不在线
等级: 人见人爱
家族: 丢丢
发贴: 2333
威望: 0
浮云: 1260
在线等级:
注册时间: 2006-09-15
最后登陆: 2009-05-11

5come5帮你背单词 [ relativity /relə'tiviti/ n. 相对论,相关(性) ]


上一届校赛的题解,我完全服了

原题:

Introduction to the problem

A well-known trick to know if an integer N is a multiple of nine is to compute
the sum S of its digits. If S is a multiple of nine, then so is N. This is a
recursive test, and the depth of the recursion needed to obtain the answer on N
is called the 9-degree of N.

Your job is, given a positive number N, determine if it is a multiple of nine
and,if it is, its 9-degree.

Description of the input

The input is a file such that each line contains a positive number. A line
containing the number 0 is the end of the input. The given numbers can contain
up to 1000 digits.

Description of the output

The output of the program shall indicate, for each input number, if it is a
multiple of nine, and in case it is, the value of its nine-degree. See the
sample output for an example of the expected formatting of the output.

Sample input:

999999999999999999999
9
9999999999999999999999999999998
0

Sample output

999999999999999999999 is a multiple of 9 and has 9-degree 3.
9 is a multiple of 9 and has 9-degree 1.
9999999999999999999999999999998 is not a multiple of 9.


在数学学院主页上下载的上一届校赛的这一题的题解.
我完全服了,很精彩!
Copy code
// Problem A
// [url]http://acm.uva.es/p/v109/10922.html[/url]
// Solution by semiconductor
#include   <cstdio>
#include   <cstring>
using namespace std;

char buf[1001];

void
Solve(int s)
{
  int deg = 1;
  while ( s >= 10 ) {
    int ns = 0;
    while ( s ) {
        ns += s % 10;
        s /= 10;
    }
    s = ns;
    ++deg;
  }
  if ( s == 9 ) printf("%s is a multiple of 9 and has 9-degree %d.\n", buf, deg);
  else printf("%s is not a multiple of 9.\n", buf);
}

int
main()
{
  while ( scanf("%s", buf), strcmp(buf, "0") ) {
    int s = 0;
    for ( int i = 0; buf[i]; i++ ) s += buf[i] - '0';
    Solve(s);
  }
}



[ 此贴被yinx在2007-03-31 11:53重新编辑 ]
顶端 Posted: 2007-03-29 22:55 | [楼 主]
richardxx





性别: 保密 状态: 该用户目前不在线
等级: 品行端正
发贴: 193
威望: 0
浮云: 1144
在线等级:
注册时间: 2005-10-01
最后登陆: 2009-02-28

5come5帮你背单词 [ botany /'botəni/ n. 植物学 ]


赞semiconductor!!!
顶端 Posted: 2007-04-10 13:06 | [1 楼]
我来我网·5come5 Forum » 程序员之家

Total 0.015817(s) query 6, Time now is:11-23 19:17, Gzip enabled
Powered by PHPWind v5.3, Localized by 5come5 Tech Team, 黔ICP备16009856号