Typically,theproblemstatementhasplentyofwords;yourgoal istoturnthemintoa program. Here isanexample:
Triangle
(Sampleproblem)
Atrianglehasthreesides,eachofwhichhasalength. Itturnsoutthatspecifyingthreenumbers forthelengthofthethreesidesofatriangleisenoughinformationtodrawatriangle.congruentto theoriginalone. However,noteverychoiceofthreenumberscanformatriangle. Forexample,if thenumbersare1.0,2.0,and4.0,thereisnowaytodrawatriangle,becausenoamountof stretchingwillpermitthe1.0and2.0sidestoconnectthetwoendpointsofthe4.0line. The
restrictiononlengthswhichcanformatriangleiscalledtheTiianglelnequaJjty:
a+b>c
wherea,b,andcarethelengthsofthethreesides,cbeing thelongest.
Foreachiterationoftheproblem,youaregiventhreenumbers,andyoumustprintoutalinewith thewordTRIANGLE iftheycanformthesidesofatriangle,andNOiftheycannot. Thereare
threeiterations,thatis, threesetsofthreenumbers. Youwillproducethree linesofoutput.
SampleInput:
1.02.04
454545.0
601580
SampleOutput: NO
TRIANGLE NO
Hereisasamplesolution,codedintheClanguage.Iusedscanfforinput. Theinputisprettyugly,but therestoftheprogramisn'ttoodifficult.
/*Triangleproblem-- C version*/
#include<stdio.h
intmain(){
floata,b,c,t;
inti;
/*repeat 3 times*/
for(i=O;i<3; i++){
if (scanf("%g",&a) !=1){printf("oops!badinput?\n");} if (scanf("%g",&b) != 1){printf("oops!badinput?\n");} if (scanf("%g",&c) != 1){printf("oops!badinput?\n");}
/*make sureclongest side */
if (a>c){
floatt= c;
c= a;
a =t;
}
if(b>c){
floattc;
c =b;
b=t;
}
/*triangleinequalitysatisfied? */
if(a+bc){
printf("TRIANGLE\n");
}
else{
printf("NO\n");
}
}
}
ThissamplesolutioniscodedinC++. MuchprettierVOthanC:
IITriangleproblem--C++version
#include<iostream>
main(){
IIdothethreetrialscalledforinthecontest for(inti=0; i<3;i++){
floata,b,c;
cina>b>Ci
IImakesureclongestside
if(a>c){
floatt= Cj
c= a;
a= t;
}
if(b>c){
floattc;
cb·I
b= t·I
}
IItriangleinequalitysatisfied?
if(a+b>c){
cout11 TRIANGLE\n11 ;
}
else{
cout"NO\n";
}
}IIendofrepeat3timesloop
}IIendofmain
IITriangleproblem--Javaversion importjava.util.Scanner;
publicclassSample{
publicstaticvoidmain(String[]unused){
IIsetupforinputfromconsole
Scannerins=newScanner(System.in);IINOTICEthescanneris
createdOUTSIDEtheloop!
IIdothethreetrialscalledforinthecontest
for(inti=0;i<3;i++){
floata1 b1c;
IIreadthethreenumbers ains.nextFloat();
b=ins.nextFloat();
c=ins.nextFloat();
II makesureclongestside if(a>c){
floatt=c;
ca·I
at·I
}
if(b>C){
floattc·I
cb·I
b=t;
}
IItriangleinequalitysatisfied?
if(a+bc){
System.out.println("TRIANGLE");
}
else{ System.out.println("NO");
}
}IIendofrepeat3timesloop
}IIendofmain
}II endofSampleclass
ThisexampleiscodedinVisualBasic.NoticetheuseofCDbl(String)toconverta Stringinputtoadouble;ingeneral,youneedtopaymoreattentiontoinputthanyou maybeusedto.
ModuleModule!
DimxAsString Dimy()AsString DimsAsDouble
Dima,b,cAsDouble
SubMain()
ForiAsInteger=1To3
'funnyinputwithmultipleitemsperline x=Console.Readline
y=x.Split
'IassumeIknowwhatitlookslike...a=CDbl(y(O))
b=CDbl(y(l))
c=CDbl(y(2))
'triangletestcode:
' firstputlargestvalueina
IfabThen
s =a a= b b=s
EndIf
IfacThen
s=a
a=c
c=s
EndIf
'commented outdebuggingcode
'Console.Writeline(a)
'Console.Writeline(b)
'Console.Writeline(c)
'maincomparisonofprogram
Ifa>=b+cThen
Console.Writeline(..NOTATRIANGLE..)
Else.
Console.Writeline(..TRIANGLE11
EndIf
Next
Iinsteadoffinalreadline,whichwouldconfusethejudges,
IIinsteadplaceaVSbreakpoint ontheEndSub
EndSub
EndModule
.#Thisisasampleprogramusingpython3.2,whichdiffersfrompython2.x
#insomesimpleways:
#1)printisafunction,andmusthaveparenteseslikeanyotherfuctioncall
#2)thepreferredwaytoacceptinputfromtheconsoleis"input()"
#whichalwaysreturnsastring.Youmayuse
#int(input())orfloat(input())ifyouwantaninteger
#orfloating-pointvalue.
#Thisprogramdoesthreeiterationsofthetriangleinequalityprogram
#foreachiteration,itacceptsthreenumbers,anddetermineswhetherthey
#canformatriangle
foriinrange(3):
#theinputforthe'sampleprogramiseasyinC++,orwithajavaScanner,
#butabitofapainforPython line=1 1 #lineinitiallyempty
items=list()#startitemsasemptylist while(len(items)3):
line=line+1 1 +input()#acceptawholelineofinput
items=line.split()#splitonwhitespaceintoanarrayofitems a=float(items[O])
bfloat(items[l])
c=float(items[2])
ifa<b+candbateandca+b print("triangle")
else:
print("no")