1512 - 反序数

通过次数

0

提交次数

0

时间限制 : 1 秒 内存限制 : 32 MB

设N是一个四位数,它的9倍恰好是其反序数(例如:1234的反序数是4321)

求N的值

题目输入

程序无任何输入数据。

题目输出

输出题目要求的四位数,如果结果有多组,则每组结果之间以回车隔开。

输入/输出样例

输入格式


                        

输出格式


                        

C语言解答

#include<stdio.h>
int main(){
  printf("1089\n");
  return 0;
}

C++解答

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int main()
{
	int i;
	char a[5],b[6];
	for(i=1000;i<=9999;i++)
	{
		sprintf(a,"%d",i);
		sprintf(b,"%d",i*9);
		reverse(b,b+strlen(b));
		if(!strcmp(a,b))
			printf("%d\n",i);
	}
	return 0;
}

Java解答



public class Main {
	public static void main(String[] args) {
		for (int i = 1000; i < 1111; i++) {
             if(f(i)==i*9){
            	 System.out.println(i);
             }
		}
		
	}

	private static int f(int n) {
		int a[] = new int[4];
		int i = 3;
		while (n > 0) {
			a[i] = n % 10;
			n = n / 10;
			i-- ;
		}
		
		
		int s = 0 ;
		
		for (int j = 0; j < a.length; j++) {
			s = (int) (s + a[j]*Math.pow(10, j)) ;
		}
		
		return s ;
	}
}