ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

UVA-509 RAID技术 题解答案代码 算法竞赛入门经典第二版

2021-12-18 18:02:28  阅读:174  来源: 互联网

标签:arr return int 题解 sign ++ 509 RAID false


GitHub - jzplp/aoapc-UVA-Answer: 算法竞赛入门经典 例题和习题答案 刘汝佳 第二版

AC代码

#include<stdio.h>
#include<string.h>
int d, s, b;
char arr[6][64000];
char sign[10];
char res[64000];
int resi;
char con[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; 

bool judge(int x) {
	int k, c = 0, xloc;
	int xflag = false;
	for(k = 0; k < d; ++k) {
		if(arr[k][x] == 'x') {
			if(xflag == true) return false;
			if(xflag == false) {
				xflag = true;
				xloc = k;
			}
			continue;
		}
		c += arr[k][x] - '0';
	}
	c = c % 2;
	if(xflag == true) {
		arr[xloc][x] = (sign[0] == 'E' ? c : !c) + '0';
	} else {
		if((sign[0] == 'E' && c == 1) || (sign[0] == 'O' && c == 0)) {
			return false; 
		}
	}
	return true; 
}

bool isVaild() {
	int i, j, k, t, x;
	for(i = 0; i < b; ++i) {
		t = i % d;
		for(j = 0; j < s; ++j) {
			x = i * s + j;
			if(judge(x) == false) {
				return false;
			}
		}
		for(k = 0; k < d; ++k) {
			if(k == t) continue;
			strncpy(res+resi, &arr[k][i*s], s);
			resi += s; 
		}
	}
	return true;
}

void convert() {
	int i, j = 0;
	for(i = 0; i < resi; ++i) { 
		j = j * 2 + res[i] - '0';
		if(i % 4 == 3) {
			printf("%c", con[j]);
			j = 0;
		}
	}
	if(i % 4 == 0) return;
	while(i % 4 != 0) {
		j = j * 2;
		++i;
	}
	printf("%c", con[j]);
}

int main() {
	int i, count = 0;
	int t, x, c;
	while(scanf("%d%d%d", &d, &s, &b) == 3 && d != 0) {
		scanf("%s", sign);
		for(i = 0; i < d; ++i) {
			scanf("%s", arr[i]);
		}
		printf("Disk set %d is ", ++count);
		resi = 0;
		if(isVaild()) {
			printf("valid, contents are: ");
			convert();
			putchar('\n'); 
		} else {
			puts("invalid.");
		}
	}
	return 0;
}

标签:arr,return,int,题解,sign,++,509,RAID,false
来源: https://blog.csdn.net/qq278672818/article/details/122015183

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有