栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

zoj 1060 Sorting It All Out

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

zoj 1060 Sorting It All Out

#include <iostream>#include <stdlib.h>#include <string.h>#include <stdio.h>#include <algorithm>using namespace std;#define maxn 30int n, m;bool smaller[maxn][maxn];enum Result{    inconsistent, determined, undetermined};bool cmp(int a, int b){    return smaller[a][b];}int get_id(char a){    return a - 'A';}Result work(int a, int b){    if (smaller[b][a])        return inconsistent;    smaller[a][b] = true;    for (int i = 0; i < n; i++)        if (smaller[i][a]) smaller[i][b] = true;    for (int i = 0; i < n; i++) if (smaller[b][i])     smaller[a][i] = true;    for (int i = 0; i < n; i++)        for (int j = 0; j < n; j++) if (smaller[i][a] && smaller[b][j])     smaller[i][j] = true;    for (int i = 0; i < n; i++)        for (int j = i + 1; j < n; j++) if (!(smaller[i][j] || smaller[j][i]))     return undetermined;    return determined;}void input(int x){    char st[5];    for (int i = x + 1; i < m; i++)        gets(st);}void output(){    int f[maxn];    for (int i = 0; i < n; i++)        f[i] = i;    sort(f, f + n, cmp);    for (int i = 0; i < n; i++)        putchar(f[i] + 'A');}int main(){    while (scanf("%d%d", &n, &m), n | m)    {        getchar();        memset(smaller, 0, sizeof(smaller));        Result ans = undetermined;        for (int i = 0; i < m; i++)        { char a, b, opr; a = getchar(); opr = getchar(); b = getchar(); getchar(); if (opr == '<')     ans = work(get_id(a), get_id(b)); else     ans = work(get_id(b), get_id(a)); if (ans == inconsistent) {     printf("Inconsistency found after %d relations.n", i + 1);     input(i);     break; } if (ans == determined) {     printf("Sorted sequence determined after %d relations: ", i + 1);     input(i);     output();     printf(".n");     break; }        }        if (ans == undetermined) puts("Sorted sequence cannot be determined.");    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/378557.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号