5 条题解
题解与来源题共享。已有题解默认通过;新增或编辑后需由来源域管理员审核。待审核、驳回题解仅作者和审核管理员可见。来源域:system
-
2
#include <bits/stdc++.h> using namespace std; #define int long long #define endl '\n' const double EPS = 1e-8; const int N = 2e5 + 5; int a[N]; signed main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); int n; cin >> n; vector<int> vec; for (int i = 1; i <= n; i++) { vec.push_back(i); } do { for (int i = 0; i < n; i++) cout << vec[i] << " "; cout << endl; } while (next_permutation(vec.begin(), vec.end())); return 0; } -
1
这题一共两个写法:
1.循环#include<bits/stdc++.h> using namespace std; int main(){ //freopen(".in","r",stdin); //freopen(".out","w",stdout); int n; cin>>n; for(int i=1;i<=n;i++){ for(int j=i;j<=n;j++){ for(int k=j;k<=n;k++){ cout<<i<<" "<<j<<" "<<k<<"\n"; } } } return 0; }正片开始:
1.STL大法
用c++库里的一个very牛X的函数:next__permutation 给代码:
#include<bits/stdc++.h> using namespace std; int main(){ //freopen(".in","r",stdin); //freopen(".out","w",stdout); int n; cin>>n; vector<int> a(n); for(int i=0;i<n;i++){ a[i]=i+1; } do{ 【自己填吧,看懂了再写】 }while(next_permutation(a.begin(),a.end())); return 0; }重点登场:
c++代码:
#include<bits/stdc++.h> using namespace std; int a[13],n; int vis[15]; void dfs(int u){ if(u==n){ for(int i=0;i<n;i++){ cout<<a[i]<<" "; } cout<<"\n"; return; }//边界 for(int i=1;i<=n;i++){ if(vis[i]==false){ a[u]=i; vis[i]=true; dfs(u+1); vis[i]=false; a[u]=0; } } } void solve(){ cin>>n; dfs(0); } int main(){ //freopen(".in","r",stdin); //freopen(".out","w",stdout); solve(); return 0; } -
1
#include<bits/stdc++.h> using namespace std; typedef long long ll; int n; vector<int> v; bool st[10]; void dfs(int num){ if(num==n){ for(int i=0;i<n;i++){ if(i==0) cout<<v[i]; else cout<<" "<<v[i]; } cout<<"\n"; return; } for(int i=1;i<=n;i++){ if(!st[i]){ st[i]=true; v.push_back(i); dfs(num+1); st[i]=false; v.pop_back(); } } } int main(){ ios::sync_with_stdio(false); cin.tie(0); cin>>n; dfs(0); return 0; } -
0
JAVA ac代码:
import java.util.*; import java.util.StringTokenizer; import java.io.*; public class Main { static int n; static boolean flag[];//这个数是否被用过 public static void main(String[] args) { n=in.nextInt(); flag=new boolean[n+1]; int[] arr=new int[n];//当前数组 dfs(arr,0); out.close(); } private static void dfs(int[] arr, int i) { //终止条件:i==n,即arr数组中1到n-1位置都被填完了 if(i==n) { for(int j=0;j<n;j++) out.print(arr[j]+" "); out.println(); return; } for(int j=1;j<=n;j++) { //j还没被用过 if(!flag[j]) { flag[j]=true; arr[i]=j;//赋值 dfs(arr,i+1); //回溯 arr[i]=0; flag[j]=false; } } } static FastReader in=new FastReader(); static PrintWriter out=new PrintWriter(System.out); static class FastReader{ static BufferedReader br; static StringTokenizer st; FastReader(){ br=new BufferedReader(new InputStreamReader(System.in)); } String next() { String str=""; while(st==null||!st.hasMoreElements()) { try { str=br.readLine(); }catch(IOException e) { throw new RuntimeException(e); } st=new StringTokenizer(str); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(next()); } } }
- 1
信息
- ID
- 64
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 入门
- 标签
- 递交数
- 388
- 已通过
- 213
- 上传者