题目链接:http://codeforces.com/problemset/problem/331/C1

这是第一次参加codeforces比赛(ABBYY Cup 3.0 - Finals (online version))成功AC的题目(n ≤ 106),解题的突破口是:Take the magic number, subtract a digit from it (the digit must occur in the number) and get a new magic number. Repeat this operation until a magic number equals zero.

用到了贪心思想,给出一个  0 ≤ n ≤ 106      的数,要想得到最少的减法次数,就要每次减去当前数中最大的位数。第一次提交时CE(没有包含头文件string.h),第二次和第三次的wa是因为自己太粗心了:对于n为0和除0外的个位数要加个特判。

 #include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std; int get_next(int x)
{
int a, n, i, j;
char s[], t;
sprintf(s, "%d", x); // 把整型的x转化为字符串
n = strlen(s);
for (i = ; i < n; i++) // 保证数组s按照从小到大的顺序排序
{
for (j = i+; j < n; j++)
{
if (s[i] > s[j])
{
t = s[i];
s[i] = s[j];
s[j] = t;
}
}
}
sscanf(s, "%d", &a); // 把字符串转化成整型,以便函数返回
return (a % ); // 得到最大的位数
} int main()
{
int n, count, flag;
while (cin >> n)
{
flag = count = ;
while (n >= )
{
n -= get_next(n);
count++;
flag = ;
}
if (flag)
cout << count + << endl; // 加1是因为个位数到0的转化还需一次的count
else if (n == )
cout << "" << endl;
else // 除0以外的个位数
cout << "" << endl;
}
return ;
}

codeforces C1. The Great Julya Calendar 解题报告的更多相关文章

  1. codeforces B. Calendar 解题报告

    题目链接:http://codeforces.com/problemset/problem/304/B 题目意思:给出两个日期,需要算出这两个日期之间有多少日. 细心模拟就可以了.特别要注意的是,两个 ...

  2. codeforces A. Rook, Bishop and King 解题报告

    题目链接:http://codeforces.com/problemset/problem/370/A 题目意思:根据rook(每次可以移动垂直或水平的任意步数(>=1)),bishop(每次可 ...

  3. codeforces B. Vasya and Public Transport 解题报告

    题目链接:http://codeforces.com/problemset/problem/355/B 题目意思:给出四种票种,c1: 某一部bus或者trolley的单程票(暗含只可以乘坐一次):c ...

  4. codeforces B. Eugeny and Play List 解题报告

    题目链接:http://codeforces.com/problemset/problem/302/B 题目意思:给出两个整数n和m,接下来n行给出n首歌分别的奏唱时间和听的次数,紧跟着给出m个时刻, ...

  5. codeforces 433C. Ryouko's Memory Note 解题报告

    题目链接:http://codeforces.com/problemset/problem/433/C 题目意思:一本书有 n 页,每页的编号依次从 1 到 n 编排.如果从页 x 翻到页 y,那么| ...

  6. codeforces A. Kitahara Haruki's Gift 解题报告

    题目链接:http://codeforces.com/problemset/problem/433/A 题目意思:给定 n 个只由100和200组成的数,问能不能分成均等的两份. 题目其实不难,要考虑 ...

  7. codeforces 556B. Case of Fake Numbers 解题报告

    题目链接:http://codeforces.com/problemset/problem/556/B 题目意思:给出 n 个齿轮,每个齿轮有 n 个 teeth,逆时针排列,编号为0 ~ n-1.每 ...

  8. codeforces 510B. Fox And Two Dots 解题报告

    题目链接:http://codeforces.com/problemset/problem/510/B 题目意思:给出 n 行 m 列只有大写字母组成的字符串.问具有相同字母的能否组成一个环. 很容易 ...

  9. codeforces 505A. Mr. Kitayuta's Gift 解题报告

    题目链接:http://codeforces.com/problemset/problem/505/A 题目意思:给出一个长度不大于10的小写英文字符串 s,问是否能通过在字符串的某个位置插入一个字母 ...

随机推荐

  1. 【Matplotlib】图例分开显示

    作图时图例往往都会出现一个图例框内,如果需要不同类型的图例分别显示,比如显示两个图例. 基本上,出现两个图例的话,需要调用两次 legend .第一次调用,你需要将图例保存到一个变量中,然后保存下来. ...

  2. Android 使用Parcelable序列化对象

    转:http://ipjmc.iteye.com/blog/1314145       Android序列化对象主要有两种方法,实现Serializable接口.或者实现Parcelable接口.实现 ...

  3. DLUTOJ 1142 高中的公式

    传送门 Time Limit: 1 Sec  Memory Limit: 128 MB Description 据说...高中学习了好多公式.所以...萌学长不知道该用什么公式来解决下面这个问题.对于 ...

  4. core dump gdb调试

    core dump又叫核心转储, 当程序运行过程中发生异常, 程序异常退出时, 由操作系统把程序当前的内存状况存储在一个core文件中, 叫core dump. (linux中如果内存越界会收到SIG ...

  5. Spring AOP中定义切点(PointCut)和通知(Advice)

    如果你还不熟悉AOP,请先看AOP基本原理,本文的例子也沿用了AOP基本原理中的例子.切点表达式 切点的功能是指出切面的通知应该从哪里织入应用的执行流.切面只能织入公共方法.在Spring AOP中, ...

  6. TOJ3540Consumer(有依赖的背包)

    http://acm.tju.edu.cn/toj/showp3540.html3540.   Consumer Time Limit: 2.0 Seconds   Memory Limit: 655 ...

  7. 浅谈IOC--说清楚IOC是什么

    http://www.cnblogs.com/DebugLZQ/archive/2013/06/05/3107957.html 博文目录 1.IOC的理论背景 2.什么是IOC 3.IOC也叫依赖注入 ...

  8. autofac 初步学习

    //数据处理接口 public interface IDal<T> where T : class { void Insert (T model); void Update(T model ...

  9. Mysql数据库的工作原理

  10. 转 web项目中的web.xml元素解析

    转 web项目中的web.xml元素解析 发表于1年前(2014-11-26 15:45)   阅读(497) | 评论(0) 16人收藏此文章, 我要收藏 赞0 上海源创会5月15日与你相约[玫瑰里 ...