思考题目,仔细检查,外加一个ceil函数

题目:

A palindrome is a word, number, or phrase that reads the same forwards as backwards. For example, the name “anna” is a palindrome. Numbers can also be palindromes (e.g. 151 or 753357). Additionally numbers can of course be ordered in size. The first few palindrome numbers are: 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, ... The number 10 is not a palindrome (even though you could write it as 010) but a zero as leading digit is not allowed.

Input

The input consists of a series of lines with each line containing one integer value i (1 ≤ i ≤ 2 ∗ 109 ). This integer value i indicates the index of the palindrome number that is to be written to the output, where index 1 stands for the first palindrome number (1), index 2 stands for the second palindrome number (2) and so on. The input is terminated by a line containing ‘0’.

Output

For each line of input (except the last one) exactly one line of output containing a single (decimal) integer value is to be produced. For each input value i the i-th palindrome number is to be written to the output.

Sample Input

1

12

24

0

Sample Output

1

33

151

这道题目就是要求你找出第n个回文序列数字,容易看出一位数的回文9种,二位数的回文9种,三位数的回文90种,四位数的回文90种,所以我们设一位数a[1]=9,a[2]=9后面就用一个函数得出来,a[i]=a[i-2]*10;规律容易,但是要想清楚怎么把一个个回文数字得出来,这题改bug改了两个来小时,最后别人提醒我可能数组开小了,然后才过了,值得后面再做几遍锻炼逻辑能力

#include <bits/stdc++.h>
using namespace std;
long long a[20];
int b[20];
void value(){
    a[1]=9,a[2]=9;
    for(int i=3;i<=19;){
        a[i]=a[i-2]*10;
        a[i+1]=a[i-1]*10;
        i+=2;
    }
}
int main()
{
    long long i,j,n,x,y,flag1,flag;
    value();
    while(cin>>n){
        if(n==0){
            break;
        }
    i=1,y=1;
    x=n;
    while(n>a[i]){
        n-=a[i];
        i++;
    }
    flag1=i;
    if((flag1)%2){
        flag=(flag1)/2+1;
    }
    else{
        flag=(flag1)/2;
    }
    b[1]=n*9/a[flag1];
    if((n*9)%a[flag1]){
        b[1]++;
        n-=(b[1]-1)*a[flag1]/9+1;
    }
    else{
        n-=(b[1]-1)*a[flag1]/9+1;
    }
    for(i=2;i<=flag;i++)
    {
        y=1;
        for(j=2;j<=i;j++){
            y*=10;
        }
        b[i]=n/(a[flag1]/9/y)-1;
        if((n-(a[flag1]/9/y)*b[i])!=0){
            b[i]+=1;
        }
        n-=b[i]*(a[flag1]/9/y);
    }
    for(i=1;i<=flag;i++){
        cout<<b[i];
    }
    if(flag1%2){
        for(i=1;i<=flag-1;i++){
            cout<<b[flag-i];
        }
    }
    else{
        for(i=1;i<=flag;i++){
            cout<<b[flag-i+1];
        }
    }
    cout<<endl;
}
    return 0;
}

最后AC的感觉真爽

然后是一个ceil函数,头文件是#include<math.h>,这个函数感觉挺有用的,就是求一个数,大于等于这个数的最小整数值。

附上一个水题目加深印象。

A square number is an integer number whose square root is also an integer. For example 1, 4, 81 are some square numbers. Given two numbers a and b you will have to find out how many square numbers are there between a and b (inclusive).

Input

The input file contains at most 201 lines of inputs. Each line contains two integers a and b (0 < a ≤ b ≤ 100000). Input is terminated by a line containing two zeroes. This line should not be processed.

Output

For each line of input produce one line of output. This line contains an integer which denotes how many square numbers are there between a and b (inclusive).

Sample Input

1 4

1 10

0 0

Sample Output

2

3

这题就是给你两个数,让你求出两个数之间有可以开出整数平方根的数目。

#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    long long n,x,m,y;
    while(cin>>n>>m)
    {
        if(n==0&&m==0){
            break;
        }
    x=ceil(sqrt(n));
    y=sqrt(m);
    cout<<y-x+1<<endl;
}
return 0;
}

 

猜你喜欢

转载自www.cnblogs.com/wangzhelin/p/9366290.html