1 条题解

  • 0
    @ 2024-12-24 9:54:30

    C++ :

    
    #include <iostream>
    #include <string>
    #include <algorithm>
    #include<time.h>
    using namespace std;
    int main()
    {
    	int m, n;
    	char ch;
    
    	string first, second, ans, next;
    
    	int sum, bit, flag;
    	while (cin >> first >> n)
    	{
    			////clock_t start = clock();
    
    		flag = 0;
    		m = n;
    		while (n--)
    		{
    			second = first;
    			reverse(second.begin(), second.end());
    			sum = 0;
    			ans = "";
    			//ans.reserve(first.length() + 1);
    			ans.resize(first.length());
    			size_t i = 0;
    			for (i = 0; i < first.length(); i++)
    			{
    				bit = first[i] - '0' + second[i] - '0' + sum;
    				sum = bit / 10;
    				bit = bit % 10;
    				ch = bit + '0';
    				ans[i] = ch;
    			}
    			if (sum >0)
    				ans += char(sum + '0');
    			reverse(ans.begin(), ans.end());
    
    			first = ans;
    			reverse(ans.begin(), ans.end());
    			if (first == ans)
    			{
    				flag = 1;
    				break;
    			}
    		}
    
    		////clock_t end = clock();
    		
    		if (flag == 1)
    		{
    			//cout << "yes" << ":" << first << ":" << m - n << "__sec:" << double(end - start) / CLOCKS_PER_SEC << endl;
    			cout << "yes" << endl;
    		}
    		else
    		{
    			//cout << "no" << ":" << ans.length() << ":" << m << "__sec:" << double(end - start) / CLOCKS_PER_SEC << endl;
    			cout << "no" << endl;
    		}
    	}
    	return 0;
    }
    

    Java :

    
    
    import java.math.BigInteger;
    import java.util.Scanner;
    
    public class Main {
    	public static void main(String[] args) {
    		Scanner in = new Scanner(System.in);
    		while(in.hasNextBigInteger()){
    			BigInteger big = in.nextBigInteger();
    			int m =in.nextInt();
    			re(big,m);
    		}
    	}
    
    	private static void re(BigInteger big, int m) {
    		// TODO Auto-generated method stub
    		if(m==0){
    			System.out.println("no");
    			return;
    		}			
    		else{
    			StringBuffer sb = new StringBuffer(big.toString());
    			sb=sb.reverse();
    			BigInteger gib = new BigInteger(sb.toString());
    			big = big.add(gib);
    			if(isre(big)){
    				System.out.println("yes");
    				return;
    			}else
    				re(big,m-1);
    		}
    	}
    
    	private static boolean isre(BigInteger big) {
    		// TODO Auto-generated method stub
    		StringBuffer sb = new StringBuffer(big.toString());
    		if(sb.toString().equals(sb.reverse().toString()))
    			return true;
    		return false;
    	}
    	
    }
    
    
    • 1

    信息

    ID
    1156
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者