<코드>

#include <stdio.h>
#include <string.h>

int main()
{
	char str[10][100];
	char tmp[100];
	char c;
	
	for(int i=0; i<10; i++)
		fscanf(stdin, " %s", str[i]);
	fscanf(stdin, " %c", &c);
	
	for(int i=9; i>=0; i--)
	{
		for(int j=0; j<i; j++)
		{
			if(strcmp(str[i], str[j])<0)
			{
				strcpy(tmp, str[i]);
				strcpy(str[i], str[j]);
				strcpy(str[j], tmp);
			}
		}
	}
	
	for(int i=0; i<10; i++)
	{
		if(strchr(str[i], c))
			fprintf(stdout, "%s\n", str[i]);
	}
	
	return 0;
}

<코드>

#include <stdio.h>
#include <string.h>

typedef struct msg{
	char s[100];
	int len;
}msg;

int main()
{
	int i=0, max;
	char str[100];
	char *ptr;
	msg m[100];
	
	fgets(str, 100, stdin);
	fprintf(stdout, "%d\n", strlen(str));
	
	ptr = strtok(str, " ");
	
	while(ptr)
	{
		strcpy(m[i].s, ptr);
		m[i].len = strlen(m[i].s);
		i++;
		ptr = strtok(NULL, " ");
	}
	
	max = m[0].len;
	for(int j=0; j<i; j++)
	{
		if(max < m[j].len)
			max = m[j].len;
	}
	
	for(int j=0; j<i; j++)
	{
		if(m[j].len == max)
			fprintf(stdout, "%s ", m[j].s);
	}
	
	return 0;
}

 

<코드>

#include <stdio.h>
#include <stdlib.h>

typedef struct score{
	int x;
	int y;
	int z;
	float avg;
}score;

int main()
{
	int n;
	score *students;
	score tmp;
	
	fscanf(stdin, "%d", &n);
	
	students = (score *)malloc(sizeof(score) * n);
	
	for(int i=0; i<n; i++)
	{
		fscanf(stdin, "%d %d %d", &students[i].x, &students[i].y, &students[i].z);
		students[i].avg = (students[i].x+students[i].y+students[i].z)/(float)3;
	}
	
	for(int i=n-1; i>=0; i--)
	{
		for(int j=0; j<i; j++)
		{
			if(students[i].avg > students[j].avg)
			{
				tmp = students[i];
				students[i] = students[j];
				students[j] = tmp;
			}
		}
	}
	
	for(int i=0; i<n; i++)
		fprintf(stdout, "%.1f\n", students[i].avg);
	
	
	return 0;
}

<코드>

#include <stdio.h>

int main()
{
	int h, t;
	int count = 0;
	
	fscanf(stdin, "%d %d", &h, &t);
	
	for(int i=h; i<=t; i++)
	{
		if((!(i%4) && (i%100)) || !(i%400))
			count++;
	}
	
	fprintf(stdout, "%d", count);
	
	return 0;
}

<코드>

#include <stdio.h>

int main()
{
	int n;
	
	fscanf(stdin, "%d", &n);
	
	for(int i=1; i<=n; i++)
	{
		for(int j=1; j<=n-i; j++)
			fprintf(stdout, "  ");
		
		for(int j=1; j<=(i*2-1); j++)
			fprintf(stdout, "* ");
		
		fprintf(stdout, "\n");
	}
	
	for(int i=n-1; i>0; i--)
	{
		for(int j=1; j<=n-i; j++)
			fprintf(stdout, "  ");
		
		for(int j=1; j<=(i*2-1); j++)
			fprintf(stdout, "* ");
		
		fprintf(stdout, "\n");
	}
	
	return 0;
}

<코드>

#include <stdio.h>

int main()
{
	int arr[1000];
	int tmp, i=0;
	
	while(1)
	{
		fscanf(stdin, "%d", &tmp);
		
		if(!tmp)
			break;
		
		if(!(tmp%5)&&!(tmp%3)){
			arr[i++] = tmp;
		}
	}
	
	if(!i)
		printf("0");
	else
	{
		for(int j=0; j<i; j++)
			fprintf(stdout, "%d ", arr[j]);
		fprintf(stdout, "\n%d", i);
	}
	
	return 0;
}

<코드1>

#include <stdio.h>

int factorial(int x)
{
	if(x >= 1)
		return 1;
	
	return x*factorial(x-1);
}

int main()
{
	int x;
	
	fscanf(stdin, " %d", &x);
	
	fprintf(stdout, " %d", factorial(x));
	
	return 0;
}

 

<코드2>

#include <stdio.h>

int main()
{
	int x, result = 1;
	
	fscanf(stdin, "%d", &x);
	
	for(int i=1; i<=x; i++)
		result*=i;
	
	fprintf(stdout, "%d", result);
	
	return 0;
}

 

※ <코드1>처럼 함수로 짜면 채점이 안돼서 <코드2>와 같이 반복문으로 작성해서 제출해야 한다.

<코드>

#include <stdio.h>

int main()
{
	int x, y, result;
	char op;
	
	fscanf(stdin, "%d %d %c", &x, &y, &op);
	
	switch(op)
	{
		case '+':
			result = x+y;
			break;
		case '-':
			result = x-y;
			break;
		case '/':
			result = x/y;
			break;
		case '%':
			result = x%y;
			break;
		case '*':
			result = x*y;
			break;
	}
	
	fprintf(stdout, "%d %c %d = %d", x, op, y, result);
	
	return 0;
}

<코드>

#include <stdio.h>

int main()
{
	int x, y, z;
	
	fscanf(stdin, "%d %d %d", &x, &y, &z);
	
	fprintf(stdout, "%d %d...%d", x+y+z, (x+y+z)/3, (x+y+z)%3);
	
	return 0;
}

<코드>

#include <stdio.h>

int main()
{
	double x, y;
	
	fscanf(stdin, "%lf %lf", &x, &y);
	
	fprintf(stdout, "%.2f %.2f %.2f", x, y, x+y);
	
	return 0;
}

+ Recent posts