HDOJ 1320 Inversion (正反模拟)

xiaoxiao2021-02-28  75

思路:模拟题读懂题意翻译成代码就行

代码如下:

#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include <algorithm> #include <vector> using namespace std; int main() { int n; char flag; int a[50]; int b[50]; int i,j; int t; int z; int temp; int k; while (cin >> n && n) { t = 0; z = 0; memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b)); getchar(); cin >> flag; for (i = 0; i < n; i++) cin >> a[i]; if (flag == 'P') { for (i = 1; i <= n; i++) { t = 0; for (j = 0; j < n; j++) { if (i < a[j]) { t++; } if (i == a[j]) break; } b[z++] = t; } printf("%d", b[0]); for (i = 1; i < z; i++) { printf(" %d", b[i]); } printf("\n"); } else { for (i = 1; i <= n; i++) { temp = a[i-1]; for (j = 0; j < a[i - 1]; j++) { if (b[j]) { temp++; a[i - 1]++; } } while (b[temp]) temp++; b[temp] = i; } printf("%d", b[0]); for (i = 1; i < n; i++) printf(" %d", b[i]); printf("\n"); } } system("pause"); return 0; }
转载请注明原文地址: https://www.6miu.com/read-1950014.html

最新回复(0)