pascal的三道题

来源:百度知道 编辑:UC知道 时间:2024/07/08 11:29:01
破蛋
LYJ需要你的帮忙做一道很简单的题
你的任务是找出在1到N之间所有整数的和。
Input
输入包括一个整数N,其绝对值不超过10000。
Output
输出一个整数,该整数是1到N之间所有整数之和。
Sample Input
-3
Sample Output
-5

Stone pile
Time Limit: 2.0 second
Memory Limit: 16 MB
You have a number of stones with known weights W1…Wn. Write a program that will rearrange the stones into two piles such that weight difference between the piles is minimal.
Input
Input contains the number of stones N (1 ≤ N ≤ 20) and weights of the stones W1…Wn (1 ≤ Wi ≤ 100000) delimited by white spaces.
Output
Your program should output a number representing the minimal possible weight difference between stone piles.
Sample
input output
558132714 3

A+B-C Problem
Time Limit: 1.0 second
Memory Limit: 16 MB
Calculate a + b - c
Input
a b and c (a,b,c<=10n ,n<=1000)
Output
a+b-c
Sample
input output
1 5 4 2
作其中一题也行

第一题:

program exp1;
var i,n,sum:integer;
begin
readln(n);
sum:=0;
if n>=1 then for i:=1 to n do sum:=sum+i
else for i:=n to 1 do sum:=sum+i;
writeln(sum);
end.

只写给你功能函数:
(1)你的任务是找出在1到N之间所有整数的和。
function GetSum(n:Integer):Integer;
var
i:Integer;
iSum:Integer;
begin
iSum:=0;
for i:=1 to n do begin
iSum:=iSum+i;
end;
Result:=iSum;
end;
(2)输出一个整数,该整数是1到N之间所有整数之和
function GetN(iSum:Integer):Integer;
var
iIndex:Integer;
n:Integer;
begin
n:=1;
iIndex:=1;
While n<iSum do begin
iIndex:=iIndex+1;
n:=n+iIndex;
end;
Result:=iIndex;
end;