📜  Q查询的所有回文数之和在[L,R]范围内

📅  最后修改于: 2021-04-26 19:32:58             🧑  作者: Mango

在2D阵列的形式提供的q的查询ARR [] [其每行包括两个数LR,其表示所述范围[L,R]的]中,任务是找到所有回文号码躺在范围的总和[L ,R]

方法:
这个想法是使用前缀和数组。到该特定索引为止的所有回文数的总和已预先计算并存储在数组pref []中,以便可以在O(1)时间内回答每个查询。

  1. 初始化前缀数组pref []
  2. 从1到N进行迭代,并检查该数字是否是回文数:
    • 如果数字是回文数,则pref []的当前索引将存储该数字与该数字在pref []的先前索引处的总和。
    • 别的PREF的当前索引[]是相同的PREF先前索引处的值[]。
  3. 对于Q查询可以找到范围为[L,R]的所有回文数之和:
sum = pref[R] - pref[L - 1]

下面是上述方法的实现

C++
// C++ program to find the sum
// of all palindrome numbers
// in the given range
#include 
using namespace std;
 
// pref[] array to precompute
// the sum of all palindromic
// number
long long pref[100001];
 
// Function that return number
// num if num is palindromic
// else return 0
int checkPalindrome(int num)
{
 
    // Convert num to string
    string str = to_string(num);
 
    int l = 0, r = str.length() - 1;
 
    while (l < r) {
        if (str[l] != str[r]) {
            return 0;
        }
        l++;
        r--;
    }
    return num;
}
 
// Function to precompute the
// sum of all palindrome numbers
// upto 100000
void preCompute()
{
    for (int i = 1; i <= 100000; ++i) {
 
        // checkPalindrome()
        // return the number i
        // if i is palindromic
        // else return 0
        pref[i] = pref[i - 1]
                  + checkPalindrome(i);
    }
}
 
// Function to print the sum
// for each query
void printSum(int L, int R)
{
    cout << pref[R] - pref[L - 1]
         << endl;
}
 
// Function to print sum of all
// palindromic numbers between
// [L, R]
void printSumPalindromic(int arr[][2],
                         int Q)
{
 
    // Function that pre computes
    // the sum of all palindromic
    // numbers
    preCompute();
 
    // Iterate over all Queries
    // to print the sum
    for (int i = 0; i < Q; i++) {
        printSum(arr[i][0], arr[i][1]);
    }
}
 
// Driver code
int main()
{
    // Queries
    int Q = 2;
    int arr[][2] = { { 10, 13 },
                     { 12, 21 } };
 
    // Function that print the
    // the sum of all palindromic
    // number in Range [L, R]
    printSumPalindromic(arr, Q);
    return 0;
}


Java
// Java program to find the sum
// of all palindrome numbers
// in the given range
import java.util.*;
 
class GFG{
  
// pref[] array to precompute
// the sum of all palindromic
// number
static int []pref = new int[100001];
  
// Function that return number
// num if num is palindromic
// else return 0
static int checkPalindrome(int num)
{
  
    // Convert num to String
    String str = String.valueOf(num);
  
    int l = 0, r = str.length() - 1;
  
    while (l < r) {
        if (str.charAt(l) != str.charAt(r)) {
            return 0;
        }
        l++;
        r--;
    }
    return num;
}
  
// Function to precompute the
// sum of all palindrome numbers
// upto 100000
static void preCompute()
{
    for (int i = 1; i <= 100000; ++i) {
  
        // checkPalindrome()
        // return the number i
        // if i is palindromic
        // else return 0
        pref[i] = pref[i - 1]
                  + checkPalindrome(i);
    }
}
  
// Function to print the sum
// for each query
static void printSum(int L, int R)
{
    System.out.print(pref[R] - pref[L - 1]
         +"\n");
}
  
// Function to print sum of all
// palindromic numbers between
// [L, R]
static void printSumPalindromic(int arr[][],
                         int Q)
{
  
    // Function that pre computes
    // the sum of all palindromic
    // numbers
    preCompute();
  
    // Iterate over all Queries
    // to print the sum
    for (int i = 0; i < Q; i++) {
        printSum(arr[i][0], arr[i][1]);
    }
}
  
// Driver code
public static void main(String[] args)
{
    // Queries
    int Q = 2;
    int arr[][] = { { 10, 13 },
                     { 12, 21 } };
  
    // Function that print the
    // the sum of all palindromic
    // number in Range [L, R]
    printSumPalindromic(arr, Q);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program to find the sum
# of all palindrome numbers
# in the given range
 
# pref[] array to precompute
# the sum of all palindromic
# number
pref=[0]*100001
 
# Function that return number
# num if num is palindromic
# else return 0
def checkPalindrome(num):
     
    # Convert num to string
    strr = str(num)
    l = 0
    r = len(strr)- 1
    while (l < r):
        if (strr[l] != strr[r]):
            return 0
             
        l+=1
        r-=1
     
    return num
 
 
# Function to precompute the
# sum of all palindrome numbers
# upto 100000
def preCompute():
    for i in range(1,100001):
        # checkPalindrome()
        # return the number i
        # if i is palindromic
        # else return 0
        pref[i] = pref[i - 1]+ checkPalindrome(i)
     
 
 
# Function to print the sum
# for each query
def printSum(L, R):
    print(pref[R] - pref[L - 1])
 
 
# Function to prsum of all
# palindromic numbers between
# [L, R]
def printSumPalindromic(arr,Q):
     
    # Function that pre computes
    # the sum of all palindromic
    # numbers
    preCompute()
     
    # Iterate over all Queries
    # to print the sum
    for i in range(Q):
        printSum(arr[i][0], arr[i][1])
     
 
 
# Driver code
 
# Queries
Q = 2
arr= [[10, 13 ],[ 12, 21 ]]
 
# Function that print the
# the sum of all palindromic
# number in Range [L, R]
printSumPalindromic(arr, Q)
 
# This code is contributed by shivanisinghss2110


C#
// C# program to find the sum
// of all palindrome numbers
// in the given range
using System;
 
class GFG{
   
// pref[] array to precompute
// the sum of all palindromic
// number
static int []pref = new int[100001];
   
// Function that return number
// num if num is palindromic
// else return 0
static int checkPalindrome(int num)
{
   
    // Convert num to String
    String str = String.Join("",num);
   
    int l = 0, r = str.Length - 1;
   
    while (l < r) {
        if (str[l] != str[r]) {
            return 0;
        }
        l++;
        r--;
    }
    return num;
}
   
// Function to precompute the
// sum of all palindrome numbers
// upto 100000
static void preCompute()
{
    for (int i = 1; i <= 100000; ++i) {
   
        // checkPalindrome()
        // return the number i
        // if i is palindromic
        // else return 0
        pref[i] = pref[i - 1]
                  + checkPalindrome(i);
    }
}
   
// Function to print the sum
// for each query
static void printSum(int L, int R)
{
    Console.Write(pref[R] - pref[L - 1]
         +"\n");
}
   
// Function to print sum of all
// palindromic numbers between
// [L, R]
static void printSumPalindromic(int [,]arr,
                         int Q)
{
   
    // Function that pre computes
    // the sum of all palindromic
    // numbers
    preCompute();
   
    // Iterate over all Queries
    // to print the sum
    for (int i = 0; i < Q; i++) {
        printSum(arr[i,0], arr[i,1]);
    }
}
   
// Driver code
public static void Main(String[] args)
{
    // Queries
    int Q = 2;
    int [,]arr = { { 10, 13 },
                     { 12, 21 } };
   
    // Function that print the
    // the sum of all palindromic
    // number in Range [L, R]
    printSumPalindromic(arr, Q);
}
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:
11
0

时间复杂度: O(N)