1.합계 SUM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Algorithm
{
public class sum
{
public static void Main()
{
// 1. Input : 5명의 국어 점수
int[] score = { 100, 75, 37, 50, 95, 90 };
int sum = 0;
// 2. Process : SUM
for (int i = 0; i < score.Length; i++)
{
if (score[i]>=80)
{
sum += score[i]; //sum
}
}
foreach문 사용하여 출력
foreach (var item in score)
{
if (item >= 80)
{
sum += item;
}
}
// 3. Output
Console.WriteLine("5명의 점수 중 80점 이상의 총점 : {0}", sum);
Console.WriteLine("5명의 점수 중 80점 이상의 총점 :
{1}",score.Length,sum);
Console.Write("{0}", sum);
}
}
}
2.카운터
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Algorithm
{
public class count
{
public static void Main()
{
//1.input
int[] data = { 10, 9, 4, 7, 6, 5 };
int count = 0; //카운터 저장
//2.process
for (int i = 0; i < data.Length; i++)
{
if (data[i] % 2 == 0)
{
count++;
}
}
//3.Output
Console.WriteLine("짝수의 건수 : {0}",count); //3
}
}
}
3.평균
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Algorithm
{
public class Average
{
public static void Main()
{
//1.Input
int[] date = { 50, 65, 78, 90, 95 };
int count = 0;
double avg = 0.0; //평균이 저장될 변수
int sum = 0;
//2.Process
for (int i = 0; i < date.Length; i++)
{
if (date[i] >= 80 && date[i] <= 95)
{
sum += date[i];
count++;
}
}
avg = sum / (double)count; //캐스팅(형식변환) 필요 : 3 -> 3.0
//3.Output
Console.WriteLine
("80점 이상 95점 이하인 자료의 평균 : {0}", avg); // 92.5
}
}
}
4.최대값
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Algorithm
{
public class Max
{
public static void Main()
{
//1. Init
//int max = 1; //해당 범위내에서 가장 작은 값으로 초기화
int max = Int32.MinValue;
//2. Input
int[] data = { 2, 5, 3, 7, 1 };
//3. Process : Max
for (int i = 0; i < data.Length ; i++)
{
if(data[i] > max)
{
max = data[i];
}
}
//4. Output
Console.WriteLine("최댓값 : {0}", max); //7
}
}
}
5.최소값
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Algorithm
{
public class Min
{
public static void Main()
{
int min = Int32.MaxValue;
//1. Input
int[] data = { 2, 5, 3, 7, 1 };
//2. Process : Min
for (int i = 0; i < data.Length; i++)
{
if (data[i] < min)
{
min = data[i];
}
}
//3. Output
Console.WriteLine("최소값 : {0}", min); //7
}
}
}
6. 가까운값
//가까운 값 : 차이값의 절대값의 최소값
using System;
public class 가까운값
{
public static void Main()
{
//1.input
int[] data = { 10, 20, 30, 27, 17 };
int target = 25; //target과 가까운 값
int near = 0;
int min = Int32.MaxValue; // 차이값의 절대값의 최소값
//2.process
for (int i = 0; i < data.Length; i++)
{
if (Abs(data[i] - target) < min)
{
//min=Math.Abs(data[i]-target); 다른방식
min = Abs(data[i]-target); //최소값
near = data[i];
}
}
//3.output
Console.WriteLine("{0}와 가장 가까운 값 : {1}",target, near);
}
private static int Abs(int p) 함수호출 후
{
return (p < 0) ? -p : p; 삼항연산자를 통하여 출력
}
}
7. 최빈값
//최빈값 : 가장 많이 나타난 값
// -> 데이터의 인덱스 (0점 ~100)의 카운터(Count)값의 최대점(MAX)
using System;
public class 최빈값
{
public static void Main()
{
//1.input
int[] score = { 1, 3, 4, 3, 5 }; //0부터 10까지 중에서
//1-1 0~5까지 배열 생성 -> 인덱스의 카운터 구하기 위해서
int[] index = new int[5+1];
int max = Int32.MinValue;
int mode = 0; //최빈값이 담길 그릇
//int count = 0;
//2.process
for (int i = 0; i < score.Length; i++)
{
index[score[i]]++; //count알고리즘
}
for (int i = 0; i < index.Length; i++)
{
max = index[i]; //MAx알고리즘
mode = i;
}
///////////////////////////////////////
for문을 이용하여 출력
///////////////////////////////////////
//for (int i = 0; i <= score.Length; i++)
//{
// for (int j = 0; j < score.Length; j++)
// {
// if (score[i] == score[j])
// {
// count++;
// if (count > mode)
// {
// mode = score[j];
// }
// }
// }
///////////////////////////////////////////
foreach 문 이용하여 출력
//////////////////////////////////////////
//foreach (int item in score)
//{
// count = 0;
// foreach (int item1 in score)
// {
// if (item==item1)
// {
// count++;
// }
// if (count > mode)
// {
// mode = item;
// }
// }
//}
////////////////////////////////////////////////
//3.output
Console.WriteLine("최빈값 :{0}", mode);
}
}
8. 순위
9. 정렬
10.병합
11.검색 Search
12.그룹 Grop
'.Net Project > .Net C#' 카테고리의 다른 글
15장 구조체(Struct), 열거형(Emumeration) (0) | 2009.08.06 |
---|---|
14장 함수(메서드) (0) | 2009.08.05 |
12장 Consoal Array (0) | 2009.08.05 |
11장 컬렉션 반복 (0) | 2009.08.05 |
10장 비트연산자 (0) | 2009.08.04 |