codeforces C1. The Great Julya Calendar 解题报告
题目链接: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 解题报告的更多相关文章
- codeforces B. Calendar 解题报告
题目链接:http://codeforces.com/problemset/problem/304/B 题目意思:给出两个日期,需要算出这两个日期之间有多少日. 细心模拟就可以了.特别要注意的是,两个 ...
- codeforces A. Rook, Bishop and King 解题报告
题目链接:http://codeforces.com/problemset/problem/370/A 题目意思:根据rook(每次可以移动垂直或水平的任意步数(>=1)),bishop(每次可 ...
- codeforces B. Vasya and Public Transport 解题报告
题目链接:http://codeforces.com/problemset/problem/355/B 题目意思:给出四种票种,c1: 某一部bus或者trolley的单程票(暗含只可以乘坐一次):c ...
- codeforces B. Eugeny and Play List 解题报告
题目链接:http://codeforces.com/problemset/problem/302/B 题目意思:给出两个整数n和m,接下来n行给出n首歌分别的奏唱时间和听的次数,紧跟着给出m个时刻, ...
- codeforces 433C. Ryouko's Memory Note 解题报告
题目链接:http://codeforces.com/problemset/problem/433/C 题目意思:一本书有 n 页,每页的编号依次从 1 到 n 编排.如果从页 x 翻到页 y,那么| ...
- codeforces A. Kitahara Haruki's Gift 解题报告
题目链接:http://codeforces.com/problemset/problem/433/A 题目意思:给定 n 个只由100和200组成的数,问能不能分成均等的两份. 题目其实不难,要考虑 ...
- codeforces 556B. Case of Fake Numbers 解题报告
题目链接:http://codeforces.com/problemset/problem/556/B 题目意思:给出 n 个齿轮,每个齿轮有 n 个 teeth,逆时针排列,编号为0 ~ n-1.每 ...
- codeforces 510B. Fox And Two Dots 解题报告
题目链接:http://codeforces.com/problemset/problem/510/B 题目意思:给出 n 行 m 列只有大写字母组成的字符串.问具有相同字母的能否组成一个环. 很容易 ...
- codeforces 505A. Mr. Kitayuta's Gift 解题报告
题目链接:http://codeforces.com/problemset/problem/505/A 题目意思:给出一个长度不大于10的小写英文字符串 s,问是否能通过在字符串的某个位置插入一个字母 ...
随机推荐
- bzoj 1491 floyd
我们用w[i][j]表示i到j的最短路的数量,dis[i][j]表示i到j的最短路,那么我们在floyd的时候,如果dis[i][k]+dis[k][j]==dis[i][j],根据乘法原理我们就w[ ...
- Ubuntu 12.10安装OpenGL
http://wiki.ubuntu-tw.org/index.php?title=Howto_Install_OpenGL_Development_Environment 前言 OpenGL 是一套 ...
- 深入浅出Redis04使用Redis数据库(lists类型)
一 lists类型及操作 List是一个链表结构,主要功能是push,pop,获取一个范围的所有值等等,操作中key理解为链表的名字. Redis的list类型其实就是一个每个子元素都是sring类 ...
- MyEclipse------在特定目录创建文件和书写内容
readwrite.jsp <%@ page language="java" import="java.util.*" pageEncoding=&quo ...
- SpringServletContext简单案例
一.项目结构及相应jar包,如下图 二.UserService代码 package com.hjp.service; /** * Created by JiaPeng on 2015/11/15. * ...
- 初学JDBC,JDBC工具类的简单封装
//工具类不需要被继承 public final class JdbcUtils{ //封装数据库连接参数,便于后期更改参数值 private static String url="jdbc ...
- Get与Post比较
- WPF 窗体拖转时不触发MouseLeftButtonUpEvent
解决方案:手动添加Handler,因为e.Handled这个属性是用在路由事件中的,当某个控件得到一个RoutedEvent,就会检测Handled是否为true,为true则忽略该事件. //手动注 ...
- firefox访问失败的时间设置错误问题
在新装系统, 安装firefox后, 访问网页: baidu时 总是自动将http转换为https, 这个是baidu服务器的设置问题, 怪不到ff bd说,是ocsp证书错误, 然后将ocsp认证q ...
- sql复制数据表和表结构
SQL复制数据表 (select * into 与 insert into) select * into 目标表名 from 源表名 insert into 目标表名(fld1, fld2) sele ...