大佬救救,为什么和样例输出一样,输入样例的值都不对
查看原帖
大佬救救,为什么和样例输出一样,输入样例的值都不对
1345402
Amount789楼主2024/11/20 14:12
#include <bits/stdc++.h>
#include <string>
using namespace std;
bool judge_sushu(int num)
{
  if (num == 1)
    return false;
  for (int i = 2; i <= sqrt(num); i++)
  {
    if (num % i == 0)
    {
      return false;
    }
  }
  return true;
}
bool judge_huiwen(string str)
{
  int length = str.length();
  if (length == 1)
  {
    return true;
  }
  else
  {
    int head = 0;
    int tail = length - 1;
    int count = 0;
    if (length % 2 == 0)
    {
      if (str[head] == str[tail])
      {
        while (head != (length / 2))
        {
          head++;
          tail--;
          if (str[head] == str[tail])
            count++;
        }
        if (count == length / 2)
          return true;
          else return false;
      }
    }
    if (length % 2 != 0)
    {
      if (str[head] == str[tail])
      {
        while(head< (length / 2)-1)
        {
          head++;
          tail--;
          if(str[head]==str[tail])
            count++;
        }
        if(count==(length/2)-1)
        {
          return true;
        }
        else
        return false;
        
      }
    }
  }
}
void all_number(int num1, int num2)
{
  for(int i=num1;i<=num2;i++)
  {
    string str = to_string(i);
        if(judge_huiwen(str))
          {  
            if(judge_sushu(i))
              cout <<i<< endl;
          }
  }
}
int main()
{
  int a, b;
  cin>>a>>b;
  all_number(a,b);
}
2024/11/20 14:12
加载中...