UVALive2389 ZOJ1078 Palindrom Numbers

Regionals 2001 >> Latin America - South America

题链接:UVALive2389 ZOJ1078 Palindrom Numbers。入门训练题,用C语言编写程序。

题意简述:输入若干个整数,0作为结束。对于输入的整数n,问其几进制为回文数?

这是一个有关进制处理的问题,都是套路。

程序中,封装了一个函数ispalindrom()用于将整数转换为指定进制的字符串,同时判断是否为回文数。使用数组ans[]作为标志,其元素个数需要多一个,是否为回文数的标志放在ans[0]中。

AC的C语言程序如下:

/* UVALive2389 ZOJ1078 Palindrom Numbers */

#include <stdio.h>
#include <memory.h>

#define MAXN1 20000
#define MAXN2 16

char t[MAXN1];

int ispalindrom(int val, int base)
{
    int count=0, start, end;

    while(val) {
        t[count++] = val % base;
        val /= base;
    }

    start = 0;
    end = count - 1;
    count = 1;
    while(start < end) {
        if(t[start] != t[end]) {
            count = 0;
            break;
        }

        start++;
        end--;
    }

    return count;
}

int main(void)
{
    int n, i;
    int ans[MAXN2+1];

    while(scanf("%d", &n) != EOF && n != 0) {
        memset(ans, 0, sizeof(ans));

        for(i=2; i<=MAXN2; i++)
            if(ispalindrom(n, i)) {
                ans[0] = 1;
                ans[i] = 1;
            }

        if(ans[0]) {
            printf("Number %d is palindrom in basis", n);
            for(i=2; i<=MAXN2; i++)
                if(ans[i])
                    printf(" %d", i);
            printf("\n");
        } else
            printf("Number %d is not palindrom\n", n);
    }

    return 0;
}
时间: 2024-08-10 19:16:48

UVALive2389 ZOJ1078 Palindrom Numbers的相关文章

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive

zoj题目分类

饮水思源---zoj 转载自:http://bbs.sjtu.edu.cn/bbscon,board,ACMICPC,file,M.1084159773.A.html 注:所有不是太难的题都被归成了“简单题”,等到发现的时候已经太晚了,我太死脑筋 了……:( 有些题的程序我找不到了,555……:( SRbGa的题虽然都很经典……但是由于其中的大部分都是我看了oibh上的解题报告后做 的,所以就不写了…… 题目排列顺序没有规律……:( 按照个人感觉,最短路有的算做了DP,有的算做了图论. 有些比较

LeetCode OJ - Sum Root to Leaf Numbers

这道题也很简单,只要把二叉树按照宽度优先的策略遍历一遍,就可以解决问题,采用递归方法越是简单. 下面是AC代码: 1 /** 2 * Sum Root to Leaf Numbers 3 * 采用递归的方法,宽度遍历 4 */ 5 int result=0; 6 public int sumNumbers(TreeNode root){ 7 8 bFSearch(root,0); 9 return result; 10 } 11 private void bFSearch(TreeNode ro

129. Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

421. Maximum XOR of Two Numbers in an Array

Given a non-empty array of numbers, a0, a1, a2, - , an-1, where 0 ≤ ai < 231. Find the maximum result of ai XOR aj, where 0 ≤ i, j < n. Could you do this in O(n) runtime? Example: Input: [3, 10, 5, 25, 2, 8] Output: 28 Explanation: The maximum resul

Humble Numbers(丑数) 超详解!

给定一个素数集合 S = { p[1],p[2],...,p[k] },大于 1 且素因子都属于 S 的数我们成为丑数(Humble Numbers or Ugly Numbers),记第 n 大的丑数为 h[n]. 算法 1: 一种最容易想到的方法当然就是从 2 开始一个一个的判断一个数是否为丑数.这种方法的复杂度约为 O( k * h[n]),铁定超时(如果你这样做而没有超时,请跟 tenshi 联系) 算法 2: 看来只有一个一个地主动生成丑数了 : 我最早做这题的时候,用的是一种比较烂的

【Scala】Scala之Numbers

一.前言 前面已经学习了Scala中的String,接着学习Scala的Numbers. 二.Numbers 在Scala中,所有的数字类型,如Byte,Char,Double,Float,Int,Long,Short都是对象,这七种数字类型继承AnyVal特质,这七种数字类型与其在Java中有相同的范围,而Unit和Boolean则被认为是非数字值类型,Boolean有false和true两个值,你可以获取到各个数字类型的最值. 复杂的数字和日期 如果需要更强大的数类,可以使用spire,sc

2、Add Two Numbers

1.Add Two Numbers--这是leedcode的第二题: You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input:

[LeetCode In C++] 2. Add Two Numbers

题目: You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 -&