CSharp · Console
// n people were split into m teams (there is at least one person in each team).
// Then all members within each team shook hands.
// Write a program that inputs n and m (0<m≤n) and
// prints minimum and maximum number of possible handshakes.
using System;
using System.Diagnostics;
public class Program
{
public static int InputInt(string prompt)
{
Console.Write("Enter " + prompt + ":");
return Convert.ToInt32(Console.ReadLine());
}
public static void Main(string[] args)
{
// input
int n = InputInt("number of people");
int m = InputInt("number of teams");
Trace.Assert(0 < m && m <= n);
// maximum: m-1 single person groups plus 1 big group of n - m + 1 persons
int maxHandShakes = (n-m+1)*(n-m)/2;
// minimum: split to "almost" equal groups
int smallTeamSize = n / m;
int bigTeamSize = smallTeamSize + 1;
int bigTeamCount = n - smallTeamSize * m; // same as = n % m
int smallTeamCount = m - bigTeamCount;
/* n == smallTeamCount*smallTeamSize + bigTeamCount*bigTeamSize */
int minHandShakes = smallTeamCount * smallTeamSize * (smallTeamSize - 1) / 2 +
bigTeamCount * bigTeamSize * (bigTeamSize - 1) / 2;
// output
Console.WriteLine("Max Handshakes = " + maxHandShakes);
Console.WriteLine("Min Handshakes = " + minHandShakes);
}
}