【编程题】求和

题目描述

输入两个整数 n 和 m,从数列1,2,3.......n 中随意取几个数,使其和等于 m ,要求将其中所有的可能组合列出来

输入描述:

每个测试输入包含2个整数,n和m

输出描述:

按每个组合的字典序排列输出,每行输出一种组合
示例1

输入

5 5

输出

1 4
2 3
5
//参考http://www.cnblogs.com/BLoodMaster/archive/2010/03/23/1692641.html
//参考https://www.nowcoder.com/questionTerminal/11cc498832db489786f8a03c3b67d02c?toCommentId=428428
//重要的递归算法的一般形式
/*
void   func( mode){
	if(endCondition){
		constExpression       //基本项
	}
	else
	{
	accumrateExpreesion /归纳项

	mode=expression //步进表达式

	func(mode) / /调用本身,递归
	}
}
*/
void function(int n, int m, vector<int>& v, int bag) {
	if (m == 0) {//3
		for (int i = 0; i<v.size(); i++) {
			i == 0 ? cout << v[i] : cout << " " << v[i];
		}
		cout << endl;
	}
	for (int i = bag; i <= n&&i <= m; i++) {
		v.push_back(i);//1
		function(n, m - i, v, i + 1);//2
		v.pop_back();//4
		//1.m为所求的和,当找到一个符合的值的时候把该i值传入容器,
		//2.同时和也要减去这个值,然后传入的第四个参数要i+1,因为i已经放到容器了,就从下一个数据取,
		//3.直到和为零的时候,则容器里保存的数据就是所要的结果
		//4.当遍历完的结果m不为零则执行pop.back操作把这些没用的数据释放掉
	}
}
int main() {
	int n, m;
	//n表示为有1-n这些数,m表示从1-n中取几个数相加正好为m
	while (cin >> n >> m) {
		//保存结果的容器
		vector<int>v;
		//第四个参数表示第一个数就是为1存进容器
		function(n, m, v, 1);
	}
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Hadoop wordcount编程是一个经典的MapReduce编程,主要是用来统计给定文本文件中各个单词出现的次数。下面是Hadoop wordcount编程的代码示例和写答案的步骤: Step 1:编写Mapper类 Mapper类是MapReduce程序中的一个组件,主要负责将输入数据转换成中间数据,并输出键值对。在Hadoop wordcount编程中,我们可以编写一个Mapper类,将文本文件中的每个单词作为键,出现次数作为值,输出键值对。 示例代码如下: ``` public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> { private final static IntWritable one = new IntWritable(1); private Text word = new Text(); public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); StringTokenizer tokenizer = new StringTokenizer(line); while (tokenizer.hasMoreTokens()) { word.set(tokenizer.nextToken()); context.write(word, one); } } } ``` Step 2:编写Reducer类 Reducer类是MapReduce程序中的另一个组件,主要负责将中间数据进行合并和计算,并输出最终结果。在Hadoop wordcount编程中,我们可以编写一个Reducer类,对每个单词出现的次数进行求和,并输出最终结果。 示例代码如下: ``` public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> { public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int sum = 0; for (IntWritable val : values) { sum += val.get(); } context.write(key, new IntWritable(sum)); } } ``` Step 3:编写Driver类 Driver类是MapReduce程序的入口,主要负责调用Mapper和Reducer类,并设置相关的参数和配置。在Hadoop wordcount编程中,我们可以编写一个Driver类,设置输入输出路径和相关配置参数。 示例代码如下: ``` public class WordCountDriver { public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "word count"); job.setJarByClass(WordCountDriver.class); job.setMapperClass(WordCountMapper.class); job.setCombinerClass(WordCountReducer.class); job.setReducerClass(WordCountReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } } ``` Step 4:提交任务并查看结果 将上述代码保存为Java文件并编译打包,然后将打包后的Jar文件上传到Hadoop集群上,并执行以下命令提交任务: ``` hadoop jar wordcount.jar input_path output_path ``` 其中,input_path和output_path分别是输入和输出路径。 最后,在Hadoop集群上执行以下命令查看结果: ``` hadoop fs -cat output_path/part-r-00000 ``` 以上就是Hadoop wordcount编程的代码示例和写答案的步骤。当然,具体的实现细节和操作步骤可能会有所不同,需要根据实际情况进行调整和修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值