bcman Posted April 19, 2015 Report Posted April 19, 2015 (edited) Salutare,Am urm?torul cod în C++#include <fstream>#include <iostream>#include <math.h>using namespace std;bool pal(unsigned n){ unsigned x=n, inv=0; while (x>0) { inv = inv*10+x%10; x/=10; } return inv==n;}int main(){ ifstream fin("test.in"); unsigned n; fin >> n; for (unsigned i=1;i<=n;i++) { unsigned a, b, rez=0; fin >> a >> b; if (pal(a)&&pal(int(sqrt(double(a))))) rez++; a=int(sqrt(double(a)))+1; b=int(sqrt(double()); while (a<= { if (pal(a) && pal(a*a)) rez++; a++; } cout << "Case #" << i << ": " << rez << endl; } return 0;}?tiu c? putea fi scris ceva mai optimizat, ideea e c? am nevoie de acest cod în Java. M? poate ajuta cineva??i cam ce am f?cut eu:package palindrome;import java.util.*;public class palindrome{ public static boolean pal(int n) { int x = n; int inv = 0; while (x > 0) { inv = inv * 10 + x % 10; x /= 10; } return inv == n; } public static int Main() { Scanner sc = new Scanner(new File("file.in")); int n = sc.nextInt(); for (int i = 1;i <= n;i++) { int rez = 0; int a = sc.nextInt(); int b = sc.nextInt(); if (palindrome.pal(a) && palindrome.pal((int)Math.sqrt((double)a))) { rez++; } a = (int)Math.sqrt((double)a) + 1; b = (int)Math.sqrt((double); while (a <= { if (palindrome.pal(a) && palindrome.pal(a * a)) { rez++; } a++; } System.out.print("Case #"); System.out.print(i); System.out.print(": "); System.out.print(rez); System.out.print("\n"); } return 0; }} Edited April 19, 2015 by bcman Quote
cobradera Posted April 30, 2015 Report Posted April 30, 2015 Citeste de la tastatura de cate ori sa ruleze programul si apoi primeste input, numarul posibil palindrom.E neclar care e rolul lui a si b in sintaxa, dar daca ai intrebari le putem lamuri.package generics.exercises;import java.util.Scanner;public class Palindrome { public static boolean isPalindrome(int numberPossiblePalindrome) { int number = numberPossiblePalindrome; int reverse = 0; while (number > 0) { reverse = reverse * 10 + number % 10; number /= 10; } return reverse == numberPossiblePalindrome; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Continue? [Y/N]"); int count = 0; while (sc.hasNext() && sc.nextLine().equalsIgnoreCase("y")) { System.out.println("Attempts: "); int attempts = sc.nextInt(); for (int i = 0; i < attempts; i++) { System.out.println("Palindrome candidate number: "); int palindromeCandidateNumber = sc.nextInt(); System.out.println("Limit number: "); int limitNumber = sc.nextInt(); double sqrtNumber = Math .sqrt((double) palindromeCandidateNumber); if (isPalindrome(palindromeCandidateNumber) && isPalindrome((int) sqrtNumber)) { count++; } palindromeCandidateNumber = (int) Math .sqrt((double) palindromeCandidateNumber) + 1; limitNumber = (int) Math.sqrt((double) limitNumber); while (palindromeCandidateNumber <= limitNumber) { if (isPalindrome(palindromeCandidateNumber) && isPalindrome(palindromeCandidateNumber * palindromeCandidateNumber)) { count++; } palindromeCandidateNumber++; } } } System.out.println("rez:" + count); }} Quote