넥슨 청소년 프로그래밍 챌린지 대회 문제 질문

ykw1101의 이미지

지금 제목에 나온 대회 작년 문제를 푸는 중인데요, 아직 초보라 참가에 의의를 두고 연습 중입니다. 그런데 1번 문제부터 막혔네요.. 문제는 https://www.nypc.co.kr/community/questionView.do?IDX=1 이 링크입니다.
코드는

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
int main(void) {
	int numc=0,numd=0;
	int s;
 
	scanf("%d",&s);
	char arr[s][s];
 
	for(int i=0;i<s;i++){
		for(int j=0;j<s;j++)
			scanf("%c",&arr[i][j]);
	}
 
	if(arr[s][s] == 'C')
		numc++;
	else if(arr[s][s] == 'D')
		numd++;
 
	int i = rand()%(s+1)+1;
	int j = rand()%(s+1)+1;
 
	while(2*numd<numc){
		if(arr[i][j] == '*')
			arr[i][j] == 'D';
	}
 
	for(int i=0;i<s;i++){
		for(int j=0;j<s;j++)
			printf("%c",arr[i][j]);
	}
 
	return 0;
}

제가 포인터를 모르고 배열도 미숙해서.. 더 공부해야하는데 일단 아는 지식만으로 짜본겁니다. 그런데 자꾸 실행 했을 때 오류가 납니다. *를 계속 입력하다 C를 한번 입력하면 실행이 바로 끝나버리네요. 배열 까지만 이용해서 이 코드를 짜기엔 부족한가요? 혹시 잘못된 점이 있다면 수정 부탁드리고 다른 방법이 있다면 알려주시면 감사하겠습니다.
(처음 입력시 부터 입력 충족 조건을 만족하지 못하는 것은 알지만 그것은 일단 넘겼읍니다. 혹시 배열만으로 입력도 조건을 충족 시킬 수 있다면 알려주시면 감사하겠습니다.)
shint의 이미지

웹에서 값을 확인해봤습니다.
http://codepad.org/ZayR6hkD

오류 확인 방법
1. 한줄씩 삭제해서 확인
2. 값을 출력해서 확인
3. 잘되는 책 예제.등을 참고해서 확인
4. 각 함수에 인자값. 리턴값. 오류값을 확인

scanf()
http://itguru.tistory.com/36
http://www.cplusplus.com/reference/cstdio/scanf/

다른 배열 생성 방법을 사용해보셔야 할거 같습니다.
- #define 으로 배열을 상수로 생성
- malloc / new 등으로 메모리 할당
- 파일을 배열 처럼 사용
- 구조체를 여러개 사용

----------------------------------------------------------------------------
젊음'은 모든것을 가능하게 만든다.

매일 1억명이 사용하는 프로그램을 함께 만들어보고 싶습니다.
정규 근로 시간을 지키는. 야근 없는 회사와 거래합니다.

각 분야별. 좋은 책'이나 사이트' 블로그' 링크 소개 받습니다. shintx@naver.com

peecky의 이미지

배열을 사용해서 풀 수 있는 문제입니다.

배열에 D, C, . 외에 다른 문자가 들어가지 않도록 처리를 해주세요. 지금은 배열에 줄바꿈 문자가 들어갈 것입니다.

rand()%(s+1)+1 의 값은 배열의 범위를 벗어날 수 있습니다. 만약 s가 5라면 저 식의 값은 1~6 사이의 값을 가지게 됩니다. 정상적인 경우라면 0~4 사이의 값을 가져야 합니다.

while 루프 안에서 numd의 값이 변하지 않습니다. 그래서 무한루프를 돌 가능성이 있습니다.

ykw1101의 이미지

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
 
int main(void) {
	int numc=0,numd=0;
	int s;
 
	scanf("%d",&s);
	char arr[s][s];
 
	if(s<=20&&s>=5){
		for(int i=0;i<s;i++){
			for(int j=0;j<s;j++)
				scanf("%c",&arr[i][j]);//arr input
		}
 
		if(arr[s][s] == 'C')//C number ㅍㅏㅇㅏㄱ
			numc++;
		else if(arr[s][s] == 'D')//D number ㅍㅏ아ㄱ
			numd++;
 
 		int i = rand()%s;
 		int j = rand()%s;
 
		while(2*numd<numc){
			if(arr[i][j] == '*'){
				arr[i][j] == 'D';
				numd++;
			}
		}
 
		for(int i=0;i<s;i++){
			for(int j=0;j<s;j++)
				printf("%c",arr[i][j]);
		}
  	}
	return 0;
}

할 수 있는 걸 바꾸긴 했는데... 첫번째로 제시해주신 것이 이해가 안됩니다.
peecky의 이미지

입력받는 코드

for(int i=0;i<s;i++){
	for(int j=0;j<s;j++)
		scanf("%c",&arr[i][j]);//arr input
}

직후에 임시코드로
for(int i=0;i<s;i++){
	for(int j=0;j<s;j++)
		printf("%d ",arr[i][j]);
}

를 넣어보세요. %d로 출력하는 이유는 아스키 코드 값을 보기 위함입니다. 정상적으로 'D'가 입력됐다면 68, 'C'는 67, '*'는 42, '.'은 46이 출력될 것입니다. 그 외에 줄바꿈 문자가 들어갔다면 10이나 13등의 값이 출력될 것입니다. (각 문자별 아스키코드 값은 http://www.asciitable.com 여기서 볼 수 있습니다)

줄바꿈 문자들이 입력되는 이유는, 뭐 당연하지만 입력에 줄바꿈이 있기 때문입니다. 줄바꿈 문자를 받아 줄 변수를 추가하고, scanf()로 적절히 받아내면 될 겁니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.