• PAT Have Fun with Numbers 大整数运算


    题目:https://pintia.cn/problem-sets/17/problems/263

    题解:https://www.liuchuo.net/archives/2151

    题解代码:

    #include <stdio.h>
    #include <string.h>
    int book[10]; //萝卜坑,记录0-9数字出现的个数
    int main()
    {
    	char num[20];
    	scanf("%s", num);
    	int len = strlen(num);
    	int carry = 0; //进位
    	//乘以2
    	for (int i = len - 1; i >= 0; i--)
    	{
    		int temp = num[i] - '0';
    		book[temp]++;
    		temp = temp * 2 + carry;
    		carry = 0;
    		if (temp >= 10)//大于等于!
    		{
    			carry = temp / 10;
    			temp = temp % 10;
    		}
    		num[i] = temp + '0';
    		book[temp]--;
    	}
    	//打印结果
    	bool isOriginal = true;
    	for (int i = 0; i < 10; i++)
    	{
    		if (book[i] != 0)
    		{
    			isOriginal = false;
    			break;
    		}
    	}
    	printf("%s
    ", isOriginal && carry == 0 ? "Yes" : "No"); //原位数字相同、且无进位
    	if (carry > 0)
    	{
    		printf("%d", carry);
    	}
    	printf("%s
    ", num);
    	return 0;
    }
    

     

    问题:无法自主设计完备的测试用例。

    自解代码(未通过全部用例):

    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    //GetPermutation();
    int main(){
    
    	unsigned long long int x;
    	cin>>x;
    	unsigned long long int dx = x*2; 
    	unsigned long long int dis = dx;
    
    	vector<int> a(20);
    	int c=0;
    	while(x>0){
    		int t = x%10;
    		x= x/10;
    		a.insert(a.begin()+c,t);
    //		cout<<t<<endl;
    	}
    
    	c=0;
    	vector<int> b(20);
    	while(dx>0){
    		int t = dx%10;
    		dx= dx/10;
    		b.insert(b.begin()+c, t);
    //		cout<<t<<endl;
    	}
    	sort(a.begin(),a.end());
    	sort(b.begin(),b.end());
    	if(a.size()==b.size()) {
    		bool f = true;
    		for(unsigned int i=0;i<a.size();i++){
    			if(a[i]!=b[i]){
    				cout<<"No"<<endl;
    				f=false;
    				break;
    			}
    		}
    		if(f){
    			cout<<"Yes"<<endl;
    			
    		}
    	}else{
    		cout<<"No"<<endl;
    	}
    	cout<<dis<<endl;
    
    	return 0;
    }
    

    自解结果:

    参考:

    C++ 基础类型 https://en.cppreference.com/w/cpp/language/types

    C++ vector insert() https://www.geeksforgeeks.org/vector-insert-function-in-c-stl/

  • 相关阅读:
    (转)NandFlash详述
    (转)Redhat Linux 硬盘挂载方法!!!
    为Linux虚拟机挂载SD卡!
    DECLARE_GLOBAL_DATA_PTR 作用
    NAND FLASH ECC校验原理与实现
    Ehcache学习笔记(三) 与Spring集成
    ExtJs ComponentQuery 组件选择器
    好记性不如烂博客之 Quartz HowTo: Update an existing job
    使用WeakReference 与 ReferenceQueue 简单实现弱引用缓存
    Ehcache学习笔记(四) Web Caching 页面级别缓存
  • 原文地址:https://www.cnblogs.com/simon-chou/p/13366558.html
Copyright © 2020-2023  润新知