C++两数之和

给出一组数组,给出一个target值,要求输出i,j使Arry[i]+Arry[j]=target
例如:
Arry[5]=[1,2,3,4,5]
target=6
output[0,4]、[1,3]

代码如下

# include <iostream>
using namespace std;
class solution
{
public:
	int arry[10];
	int target;
	void Set()
	{
		int i;
		cout<<"Enter 10 numbers for a arry:"<<endl;
		for(i=0;i<10;i++)
		{
			cin>>arry[i];
		}
	}
	void Show()
	{
		int i;
		cout<<"Arry is:"<<endl;
		for(i=0;i<10;i++)
		{
			cout<<arry[i]<<" ";
		}
	}
	void Find()
	{
		int target;
		int i,j;
		cout<<endl;
		cout<<"target=";
		cin>>target;
		for(i=0;i<10;i++)
		{
			for(j=i+1;j<10;j++)
			{
					if(arry[i]+arry[j]==target)
					{
						cout<<"["<<i<<","<<j<<"]"<<endl;
					}
			}
		}
	}
};
int main(void)
{
	solution A;
	A.Set();
	A.Show();
	A.Find();
	system("pause");
	return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值