我尝试使用以下代码检查数字是否为回文:
unsigned short digitsof (unsigned int x)
{
unsigned short n = 0;
while (x)
{
x /= 10;
n++;
}
return n;
}
bool ispalindrome (unsigned int x)
{
unsigned short digits = digitsof (x);
for (unsigned short i = 1; i <= digits / 2; i++)
{
if (x % (unsigned int)pow (10, i) != x % (unsigned int)pow (10, digits - 1 + i))
{
return false;
}
}
return true;
}
但是,以下代码无法检查回文 - 即使数字是回文,也总是返回false。
有人可以指出错误吗?
(请注意:我没有兴趣将它变成一个字符串并反转它以查看问题所在:相反,我很想知道上面代码中的错误位置。)
答案 0 :(得分:5)
我个人只是从数字中构建一个字符串,然后将其视为正常的回文检查(检查上半部分中的每个字符是否与length()-index
处的字符匹配)。
答案 1 :(得分:1)
问题在于:
x % (unsigned int)pow (10, i)
让我们试试:
x =504405
i =3
SO I want 4.
x % 10^3 => 504405 %1000 => 405 NOT 4
怎么样
x / (unsigned int)pow (10, i -1) % 10
答案 2 :(得分:1)
x % (unsigned int)pow (10, i)
不是第i位。
答案 3 :(得分:0)
仅供参考!以下两个功能对我有用:
double digitsof (double x)
{
double n = 0;
while (x > 1)
{
x /= 10;
n++;
}
return n;
}
bool ispalindrome (double x)
{
double digits = digitsof (x);
double temp = x;
for(double i = 1; i <= digits/2; i++)
{
float y = (int)temp % 10;
cout<<y<<endl;
temp = temp/10;
float z = (int)x / (int)pow(10 , digits - i);
cout<<(int)z<<endl;
x = (int)x % (int)pow(10 , digits - i);
if(y != z)
return false;
}
return true;
}
答案 4 :(得分:0)
代码,以检查给定编号是否是回文,是否在JAVA中
I have updated your code, see below..
public class WeekFragment extends Fragment {
//Create a global View instance like below
View previousView;
public WeekFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_week, container, false);
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
TableLayout timeTable = getActivity().findViewById(R.id.timeTable);
int count = timeTable.getChildCount();
for (int i = 0; i < count; i++) {
View v = timeTable.getChildAt(i);
if (v instanceof TableRow) {
TableRow row = (TableRow) v;
int rowCount = row.getChildCount();
for (int r = 0; r < rowCount; r++) {
final View v2 = row.getChildAt(r);
if (v2 instanceof ImageView) {
final ImageView b = (ImageView) v2;
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
b.setBackground(getResources().getDrawable(R.drawable.cell_highlight));
//Check for the first time for null value
if(previousView!=null&&previousView.equals(b)){
/*You can make cell normal here by adding b.setBackgroundResource(R.drawable.normal);*/
switchActivities();
}
previousView=null;
//Assign new view
previousView=b;
}
});
}
}
}
}
}
}
答案 5 :(得分:0)
我已经用自己的解决方案完成了此操作,这些解决方案受这些条件的限制
var inputNumber = 10801
var firstDigit = 0
var lastDigit = 0
var quotient = inputNumber
while inputNumber > 0 {
lastDigit = inputNumber % 10
var tempNum = inputNumber
var count = 0
while tempNum > 0 {
tempNum = tempNum / 10
count = count + 1
}
var n = 1
for _ in 1 ..< count {
n = n * 10
}
firstDigit = quotient / n
if firstDigit != lastDigit {
print("Not a palindrome :( ")
break
}
quotient = quotient % n
inputNumber = inputNumber / 10
}
if firstDigit == lastDigit {
print("It's a palindrome :D :D ")
}