特殊回文数

特殊回文数

特殊回文数
问题描述
123321 是一个非常特殊的数,它从左边读和从右边读是一样的。
输入一个正整数 n, 编程求所有这样的五位和六位十进制数,满足各位数字之和等于
n 。
输入格式
输入一行,包含一个正整数 n。
输出格式
按从小到大的顺序输出满足条件的整数,每个整数占一行。
数据规模和约定
1<=n<=54。

想法

惭愧的是,我今天又差点看不懂题目,脑子有点晕乎乎的,就一直搁到了晚上才写
事实证明的是,想不出的题目就放放,也许放着放着你就会了
后面我看懂了,是输入一个数n;然后找到五位或六位数个十百千等等相加起来等于n的数,且那五位或六位数必须还得是回文数
那这我会啊,回文数很简单,要相加起来等于n回文数不就加个条件就完事了?
于是就有了以下代码

C++
代码1

#include <iostream>
#include <string>
using namespace std;
void main (){
	int a=0;
	int b=0;
	int c=0;
	int d=0;
	int e=0;
	int f=0;
	int n;
	int i;
	int sum;
	cin>>n;
	if(n>1&&n<52){
		for(i=10000;i<1000000;i++){
			a=i/100000%10;
			b=i/10000%10;
			c=i/1000%10;
			d=i/100%10;
			e=i/10%10;
			f=i%10;
			sum=a+b+c+d+e+f;
			if(sum==n){
				if(a==f&&b==e&&c==d){
					cout<<a<<b<<c<<d<<e<<f<<endl;
				}
				else if(a==e&&b==d){
					cout<<a<<b<<c<<d<<e<<endl;

				}
			}
		}
	}
}

代码二

#include <iostream>
#include <string>
using namespace std;
void main (){
	int a=0;
	int b=0;
	int c=0;
	int d=0;
	int e=0;
	int f=0;
	int sum;
	int n;
	cin>>n;
	for(a=1;a<10;a++){
		for(b=0;b<10;b++){
			for(c=0;c<10;c++){
				for(d=0;d<10;d++){
					for(e=0;e<10;e++){
						for(f=0;f<10;f++){
							sum=a+b+c+d+e+f;
							if(sum==n){
								if(a==f&&b==e&&c==d){
									cout<<a<<b<<c<<d<<e<<f<<endl;
								}
								else if(a==e&&b==d){
									cout<<a<<b<<c<<d<<e<<endl;
								}
							}
						}
					}
				}
			}
		}
	}
}

java
代码一

import java.util.Scanner;
public class SpaecialPalindrome {
    public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            int a=0;
            int b=0;
            int c=0;
            int d=0;
            int e=0;
            int f=0;
            int sum;
            int n=in.nextInt();
            for(a=1;a<10;a++){
                for(b=0;b<10;b++){
                    for(c=0;c<10;c++){
                        for(d=0;d<10;d++){
                            for(e=0;e<10;e++){
                                for(f=0;f<10;f++){
                                    sum=a+b+c+d+e+f;
                                    if(sum==n){
                                        if(a==f&&b==e&&c==d){
                                            System.out.println(" "+a+b+c+d+e+f);
                                        }
                                        else if(a==e&&b==d){
                                            System.out.println(" "+a+b+c+d+e);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
    }
}

代码二

import java.util.Scanner;
public class SpaecialPalindrome {
    public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
        int a=0;
        int b=0;
        int c=0;
        int d=0;
        int e=0;
        int f=0;
        int sum;
        int i;
        int n=in.nextInt();
            for(i=10000;i<1000000;i++){
                a=i/100000%10;
                b=i/10000%10;
                c=i/1000%10;
                d=i/100%10;
                e=i/10%10;
                f=i%10;
                sum=a+b+c+d+e+f;
                if(sum==n){
                    if(a==f&&b==e&&c==d){
                        System.out.println(" "+a+b+c+d+e+f);
                    }
                    else if(a==e&&b==d){
                        System.out.println(" "+a+b+c+d+e);
                    }
                }
            }
    }
}
posted @ 2021-07-20 22:19  柏阳光菜菜  阅读(198)  评论(0)    收藏  举报