1962. Hide That Number

这篇博客介绍了一个关于电话号码隐藏的算法,由Alex使用,他将电话号码左移一位并加上原始号码,然后截取结果以隐藏真实号码。文章提供了两个不同版本的解密程序,用于从隐藏的号码中恢复原始号码,并展示了如何处理可能的错误情况。程序的优化集中在减少计算时间和处理边界条件上。
摘要由CSDN通过智能技术生成

1962. Hide That Number

单点时限: 2.0 sec

内存限制: 256 MB

According to Wikipedia, Cryptography is “the practice and study of hiding information” and this is exactly what Alex is looking for. Ever since he was a kid, Alex was paranoid over someone laying their hands on his phone book. He decided that he must write the numbers in some secret way that only he can decipher. At first he tried quite complex algorithms, but that slowed him down when he needed to dial a number fast. He finally came up with the following algorithm: Rather than writing down the number itself, Alex would shift the number one place to the left (as if multiplying it by 10,) then adding the shifted number to the original. For example, if the phone number was 123, Alex would add 1230 to it, resulting in 1353. To make what he writes looks as a regular phone number, Alex truncates the result (from the left,) so that it has as many digits as the original phone number. In this example, Alex writes 353 instead of 123 in his phone book.

Alex needs a program to print the original phone number given what is written in his phone book. Alex, who by the way is a good friend of Johnny, isn’t that good in arithmetic. It is quite possible that the numbers are messed up. The program should print “IMPOSSIBLE” (without the quotes) if the original number cannot be computed.

输入格式

Your program will be tested on one or more test cases. Each case is specified on a separate line and is made of a single positive number having less than 1,000,000 digits.

The last line of the input file is made of a single zero.

输出格式

For each test case, output the result on a single line using the following format:

k. result

Where k is the test case number (starting at 1).

样例

input

353
9988
123456
0

output

1. 123
2. IMPOSSIBLE
3. 738496

第一版,没考虑到12是可以有92*10+92=1012截取后面两位得到,但是改成下面前面加上2位数字,且不能为11,即可AC

#include<iostream>
using namespace std;

int getReminder(string s,int x,string &ans){
	int reminder=0,flag=1;
	for(int i=0;i<s.size();i++){
		int tmp=reminder*10+s[i]-'0';
		if(flag==0||tmp/x!=0){
			ans+=to_string(tmp/x);
			flag=0;
		}
		reminder=tmp%x;
	}
	if(ans.size()==0)ans="0";
	return reminder;
}

int main(){
	string s;
	int cnt=1;
	while(1){
		cin>>s;
		if(s=="0")break;
		int ispossible=0;
		for(int i=1;i<20;i++){
		    if(i==11)continue;
			string tmp=to_string(i)+s,ans;
			int rem = getReminder(tmp,11,ans);
			if(rem==0){
				cout<<cnt<<". "<<ans<<endl;
				ispossible=1;
				break;
			}
		}
		if(ispossible==0)cout<<cnt<<". IMPOSSIBLE"<<endl;
		cnt++;
	}
}

第二版,选择从后往前处理,一开始由于ans=to_string(need)+ans每次会创建新对象,导致超时。由于目标值长度已确定,所以通过填写每一位的数字即可,感觉也可以ans+=to_string(need),然后通过reverse函数反转一下ans来解决。

#include<iostream>
using namespace std;

int main(){
	int cnt=0;
	string s;
	while(1){
		cin>>s;
		cnt++;
		if(s=="0")break;
		printf("%d. ",cnt);
		string ans;
		ans.resize(s.size());
		int need=0,carry=0;
		for(int i=s.size()-1;i>=0;i--){
			need=s[i]-'0'-need-carry;
			if(need<0){
				need+=10;
				carry=1;
			}else carry=0;
			ans[i]=need+'0';
		}
		if(need==0)cout<<"IMPOSSIBLE"<<endl;
		else cout<<ans<<endl;
	}
}

帮我根据以下要求:Then modify the View superclass to:  hide the frame when the user clicks on the “close” button;  add a “window closing” event handler (use an anonymous window adapter) that calls the controller’s shutdown method.修改下述代码:import javax.swing.JFrame; public abstract class View<T extends Controller> extends JFrame implements ModelListener { protected Model m; protected T c; public View(Model m, T c) { this.m = m; this.c = c; m.addListener(this); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } @Override public abstract void update(); },public class MyFrame extends View<ControllerClicks> { public MyFrame(Model m, ControllerClicks c) { super(m, c); this.setTitle("MyFrame Title"); this.setSize(400, 300); this.setLocationRelativeTo(null); this.setLayout(new BorderLayout()); MyPanel centerPanel = new MyPanel(m, c); this.add(centerPanel, BorderLayout.CENTER); JPanel topPanel = new JPanel(); this.add(topPanel, BorderLayout.PAGE_START); topPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); JButton resetButton = new JButton("Reset"); resetButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { c.resetClicked(); } }); topPanel.add(resetButton); JButton undoButton = new JButton("Undo"); undoButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { c.undoClicked(); } }); topPanel.add(undoButton); this.setVisible(true); } @Override public void update() { repaint(); // Makes Swing call MyPanel's paintComponent method. } } import javax.swing.JLabel; public class ViewNumber extends View<Controller> { private JLabel label; public ViewNumber(Model m, Controller c) { super(m, c); this.setTitle("View Number"); this.setSize(150, 150); label = new JLabel(); update(); // Initialize the label using the model. this.add(label); this.setVisible(true); } @Override public void update() { label.setText("Number of points is: " + m.numberOfPoints()); } }
05-25
深度学习是机器学习的一个子领域,它基于人工神经网络的研究,特别是利用多层次的神经网络来进行学习和模式识别。深度学习模型能够学习数据的高层次特征,这些特征对于图像和语音识别、自然语言处理、医学图像分析等应用至关重要。以下是深度学习的一些关键概念和组成部分: 1. **神经网络(Neural Networks)**:深度学习的基础是人工神经网络,它是由多个层组成的网络结构,包括输入层、隐藏层和输出层。每个层由多个神经元组成,神经元之间通过权重连接。 2. **前馈神经网络(Feedforward Neural Networks)**:这是最常见的神经网络类型,信息从输入层流向隐藏层,最终到达输出层。 3. **卷积神经网络(Convolutional Neural Networks, CNNs)**:这种网络特别适合处理具有网格结构的数据,如图像。它们使用卷积层来提取图像的特征。 4. **循环神经网络(Recurrent Neural Networks, RNNs)**:这种网络能够处理序列数据,如时间序列或自然语言,因为它们具有记忆功能,能够捕捉数据中的时间依赖性。 5. **长短期记忆网络(Long Short-Term Memory, LSTM)**:LSTM 是一种特殊的 RNN,它能够学习长期依赖关系,非常适合复杂的序列预测任务。 6. **生成对抗网络(Generative Adversarial Networks, GANs)**:由两个网络组成,一个生成器和一个判别器,它们相互竞争,生成器生成数据,判别器评估数据的真实性。 7. **深度学习框架**:如 TensorFlow、Keras、PyTorch 等,这些框架提供了构建、训练和部署深度学习模型的工具和库。 8. **激活函数(Activation Functions)**:如 ReLU、Sigmoid、Tanh 等,它们在神经网络中用于添加非线性,使得网络能够学习复杂的函数。 9. **损失函数(Loss Functions)**:用于评估模型的预测与真实值之间的差异,常见的损失函数包括均方误差(MSE)、交叉熵(Cross-Entropy)等。 10. **优化算法(Optimization Algorithms)**:如梯度下降(Gradient Descent)、随机梯度下降(SGD)、Adam 等,用于更新网络权重,以最小化损失函数。 11. **正则化(Regularization)**:技术如 Dropout、L1/L2 正则化等,用于防止模型过拟合。 12. **迁移学习(Transfer Learning)**:利用在一个任务上训练好的模型来提高另一个相关任务的性能。 深度学习在许多领域都取得了显著的成就,但它也面临着一些挑战,如对大量数据的依赖、模型的解释性差、计算资源消耗大等。研究人员正在不断探索新的方法来解决这些问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值