YTU OJ 2460: Sum of specific prime numbers within a certain range

Question description

Find the sum of specific prime numbers [m, n] within a certain range (specific prime numbers refer to prime numbers containing the number 3, such as 3, 13, 31, etc.)

enter

In the first line, enter a number t to represent the number of test data groups. Each of the subsequent t lines contains two numbers m, and n represents a certain range (0<m, n<=100, m is not equal to n)

output

The output has t lines, each line contains a number, and the sum of the corresponding specific prime numbers is output.

Sample input

2 14

Sample output

3
16

c++ code

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MaxSize 1000
bool judge(int n)
{
    
    
 int t=n;
 while (t != 0)
 {
    
    
  if (t % 10 == 3)
   return true;
  t = t / 10;
 }
 return false;
}
bool is_prime(int n)
{
    
    
 int i;
 if (n == 1 || n == 2)
  return true;
 for (i = 2; i < n; i++)
 {
    
    
  if (n%i == 0)
   return false;
 }
 return true;
}
int add(int m, int n)
{
    
    
 int i,sum=0;
 for (i = m; i <= n; i++)
 {
    
    
  if (is_prime(i) == true && judge(i) == true)
   sum += i;
 }
 return sum;
}
int main()
{
    
    
 int t, m, n,sum=0;
 scanf("%d", &t);
 while (t--)
 {
    
    
  scanf("%d %d", &m, &n);
  sum=add(m, n);
  printf("%d\n", sum);
 }
 return 0;
}

Guess you like

Origin blog.csdn.net/u014295602/article/details/103318517