问题标题: 酷町堂:1210<-结贴啦

0
0
已解决
张帆
张帆
中级天翼
中级天翼
题目描述 Description
已知有一堆西瓜,请帮忙将这一堆西瓜分成两堆,已知每个西瓜的重量,现在要求分成两堆的西瓜的重量的差最小

输入描述 Input Description
第一行输入西瓜数量N (1 ≤ N ≤ 20)
第二行有N个数,W1, …, Wn (1 ≤ Wi ≤ 10000)分别代表每个西瓜的重量

输出描述 Output Description
输出分成两堆后的质量差

样例输入 Sample Input
5
5 8 13 27 14
样例输出 Sample Output
3

话说我没有强制类型转换咋就CE啦?

DEV-c++里面我编译过了呀!

#include<iostream>
#include<cmath>
using namespace std;

int n,w[10010],ans=0x7f7f7f7f;

void dfs(int t,int sum1,int sum2){
	if(t>n){
		ans=min(ans,(int)abs(sum1-sum2));
		return ;
	}
	dfs(t+1,sum1+w[t],sum2);
	dfs(t+1,sum1,sum2+w[t]);
	return ;
}

int main(){
	cin>>n;
	for(int i=1;i<=n;i++) cin>>w[i];
	dfs(1,0,0);
	cout<<ans;
	return 0;
}
/*
main.cpp: In function 'void dfs(int, int, int)':
main.cpp:9:29: error: no matching function for call to 'min(int&, __gnu_cxx::__enable_if<true, double>::__type)'
   ans=min(ans,abs(sum1-sum2));
                             ^
main.cpp:9:29: note: candidates are:
In file included from /usr/include/c++/4.8/bits/char_traits.h:39:0,
                 from /usr/include/c++/4.8/ios:40,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from main.cpp:1:
/usr/include/c++/4.8/bits/stl_algobase.h:193:5: note: template<class _Tp> const _Tp& std::min(const _Tp&, const _Tp&)
     min(const _Tp& __a, const _Tp& __b)
     ^
/usr/include/c++/4.8/bits/stl_algobase.h:193:5: note:   template argument deduction/substitution failed:
main.cpp:9:29: note:   deduced conflicting types for parameter 'const _Tp' ('int' and '__gnu_cxx::__enable_if<true, double>::__type {aka double}')
   ans=min(ans,abs(sum1-sum2));
                             ^
In file included from /usr/include/c++/4.8/bits/char_traits.h:39:0,
                 from /usr/include/c++/4.8/ios:40,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from main.cpp:1:
/usr/include/c++/4.8/bits/stl_algobase.h:239:5: note: template<class _Tp, class _Compare> const _Tp& std::min(const _Tp&, const _Tp&, _Compare)
     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
     ^
/usr/include/c++/4.8/bits/stl_algobase.h:239:5: note:   template argument deduction/substitution failed:
main.cpp:9:29: note:   deduced conflicting types for parameter 'const _Tp' ('int' and '__gnu_cxx::__enable_if<true, double>::__type {aka double}')
   ans=min(ans,abs(sum1-sum2));
                             ^
*/ 

 

张帆在2021-02-19 17:51:57追加了内容

没人我就结贴了惹、


0
0
张帆
张帆
中级天翼
中级天翼

就当是我的代码问题吧,结贴啦!

我要回答