前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【题解】Dislike of Threes

【题解】Dislike of Threes

作者头像
MikeC
发布2022-09-21 15:02:03
4210
发布2022-09-21 15:02:03
举报
文章被收录于专栏:MikeC's Blog

题目描述

Polycarp doesn't like integers that are divisible by 3 or end with the digit 3 in their decimal representation. Integers that meet both conditions are disliked by Polycarp, too.

Polycarp starts to write out the positive (greater than 0 ) integers which he likes: 1, 2, 4, 5, 7, 8, 10, 11, 14, 16, \dots . Output the k -th element of this sequence (the elements are numbered from 1 ).

输入格式

The first line contains one integer t ( 1 \le t \le 100 ) — the number of test cases. Then t test cases follow.

Each test case consists of one line containing one integer k ( 1 \le k \le 1000 ).

输出格式

For each test case, output in a separate line one integer x — the k -th element of the sequence that was written out by Polycarp.

输入输出样例

输入 #1

代码语言:javascript
复制
10
1
2
3
4
5
6
7
8
9
1000

输出 #1

代码语言:javascript
复制
1
2
4
5
7
8
10
11
14
1666

分析

题意:给定一个序列,包含除了 3 的倍数和个位是 3 数之外的全部正整数,求序列的第 k 项。

  • 判断一个数是否是 3 的倍数,只需要检查这个数 \mod 3 的结果,如果为 0 则说明是 3 的倍数,反之则不是 3 的倍数。
  • 判断一个数的个位是否为 3,只需要检查这个数 \mod 10 的结果,得到的运算结果即为这个数的个位。

于是,我们可以从 1 开始从小到大扫描正整数,检查当前的数字是否满足要求,如果满足要求,则累加当前序列项数,判断是否满足条件输出即可。

时间复杂度 O(k)

代码

代码语言:javascript
复制
#include<bits/stdc++.h>
using namespace std;
int t,k;
int main(){
    scanf("%d",&t);
    while(t--){
        scanf("%d",&k);
        int now=1,x=0;
        for(int i=1;i<=k;i++){
            x++;
            while(x%3==0||x%10==3)x++;
        }
        printf("%d\n",x);
    }
    return 0;
}

最后修改:2021 年 08 月 19 日 06 : 19 PM

© 允许规范转载

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021 年 08 月,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目描述
  • 输入格式
  • 输出格式
  • 输入输出样例
    • 输入 #1
      • 输出 #1
      • 分析
      • 代码
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档