Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CREATE PROC sp_TongIn
- @n int,
- @count int out,
- @total int out
- AS
- DECLARE @last int
- DECLARE @first int
- SET @first = 2 -- so chan dau tien
- IF (@n % 2 = 0) SET @last = @n
- ELSE SET @last = @n - 1
- SET @count = @last / 2
- SET @total = (@count / 2) * (@last + @first) -- e.g.: 2 4 6 8 10 => (2 + 10) + (4 + 8) = 12 * 2
- IF (@count % 2 <> 0)
- BEGIN
- DECLARE @middle int
- SET @middle = @last / 2 + ((@last % 2 + 1) % 2) -- e.g.: 10 / 2 + ((10 % 2 + 1) % 2) = 5 + 1 = 6
- SET @total = @total + @middle
- END
- GO
- DECLARE @count int
- DECLARE @total int
- EXEC sp_TongIn 10, @count out, @total out
- PRINT 'So luong so chan: ' + CAST(@count as VARCHAR)
- PRINT 'Tong so chan:' + CAST(@total as VARCHAR)
Advertisement
Add Comment
Please, Sign In to add comment