Pascal:范围内的数字总和
有多少小于1000的正整数的数字和等于6 不知道如何开始使用Pascal。在Python上,我的脚本类似于:Pascal:范围内的数字总和,pascal,lazarus,Pascal,Lazarus,有多少小于1000的正整数的数字和等于6 不知道如何开始使用Pascal。在Python上,我的脚本类似于: a = 1 b = 1000 for i in range(a,b): ........ 我不知道如何访问这些数字。如果有人能给我一个提示,我应该可以从这里取得一些进展 你的问题基本上就是“for循环是如何在Pascal中实现的”。。。只需查看文档,例如: 我还闻到了家庭作业的味道 你的问题基本上就是“for循环是如何在Pascal中实现的”。。。只需查看文档,例如: 我还闻到了家庭作
a = 1
b = 1000
for i in range(a,b):
........
我不知道如何访问这些数字。如果有人能给我一个提示,我应该可以从这里取得一些进展 你的问题基本上就是“for循环是如何在Pascal中实现的”。。。只需查看文档,例如:
我还闻到了家庭作业的味道 你的问题基本上就是“for循环是如何在Pascal中实现的”。。。只需查看文档,例如:
我还闻到了家庭作业的味道 你的问题基本上就是“for循环是如何在Pascal中实现的”。。。只需查看文档,例如:
我还闻到了家庭作业的味道 你的问题基本上就是“for循环是如何在Pascal中实现的”。。。只需查看文档,例如:
我还闻到了家庭作业的味道 忽略对Pascal的冷嘲热讽(它仍然是一种可行的语言,生活在Delphi的核心;它的语法已经被借用到了几种“现代”语言中),这个问题实际上比人们想象的要复杂得多。首先我会展示程序,然后我会解释
var
i, j, found, total: integer;
s: string;
begin
found:= 0; // how many numbers whose digits add up to six
for i:= 1 to 1000 do
begin
s:= inttostr (i);
total:= 0;
for j:= 1 to length (s) do
total:= total + ord (s[j]) - ord ('0');
if total = 6 then found:= found + 1;
end;
writeln ('I found ', found, ' numbers whose digits add up to six');
readln
end.
关键是将索引号(i)转换为字符串(即“inttostr(i)”行),然后对字符串的数字进行迭代并求和。忽略关于Pascal的snide注释(它仍然是一种可行的语言,并且是Delphi的核心;它的语法已被借用用于几种“现代”语言),这个问题实际上比人们想象的要复杂得多。首先我会展示程序,然后我会解释
var
i, j, found, total: integer;
s: string;
begin
found:= 0; // how many numbers whose digits add up to six
for i:= 1 to 1000 do
begin
s:= inttostr (i);
total:= 0;
for j:= 1 to length (s) do
total:= total + ord (s[j]) - ord ('0');
if total = 6 then found:= found + 1;
end;
writeln ('I found ', found, ' numbers whose digits add up to six');
readln
end.
关键是将索引号(i)转换为字符串(即“inttostr(i)”行),然后对字符串的数字进行迭代并求和。忽略关于Pascal的snide注释(它仍然是一种可行的语言,并且是Delphi的核心;它的语法已被借用用于几种“现代”语言),这个问题实际上比人们想象的要复杂得多。首先我会展示程序,然后我会解释
var
i, j, found, total: integer;
s: string;
begin
found:= 0; // how many numbers whose digits add up to six
for i:= 1 to 1000 do
begin
s:= inttostr (i);
total:= 0;
for j:= 1 to length (s) do
total:= total + ord (s[j]) - ord ('0');
if total = 6 then found:= found + 1;
end;
writeln ('I found ', found, ' numbers whose digits add up to six');
readln
end.
关键是将索引号(i)转换为字符串(即“inttostr(i)”行),然后对字符串的数字进行迭代并求和。忽略关于Pascal的snide注释(它仍然是一种可行的语言,并且是Delphi的核心;它的语法已被借用用于几种“现代”语言),这个问题实际上比人们想象的要复杂得多。首先我会展示程序,然后我会解释
var
i, j, found, total: integer;
s: string;
begin
found:= 0; // how many numbers whose digits add up to six
for i:= 1 to 1000 do
begin
s:= inttostr (i);
total:= 0;
for j:= 1 to length (s) do
total:= total + ord (s[j]) - ord ('0');
if total = 6 then found:= found + 1;
end;
writeln ('I found ', found, ' numbers whose digits add up to six');
readln
end.
关键是将索引号(i)转换为字符串(即“inttostr(i)”行),然后对字符串的数字进行迭代并求和。这是解决方案,不需要对字符串进行不必要的转换。它的工作原理是获取最右边的数字,将其值添加到累加器
Total
,然后通过执行整数除以10来删除最右边的数字,并重复此过程,直到没有剩余的数字
var
Value, Digit, Total, NumValues: Integer;
i: Integer;
begin
NumValues := 0;
for i := 1 to 1000 do
begin
Value := i;
Total := 0;
repeat
Digit := Value mod 10;
Total := Total + Digit;
Value := Value div 10;
until Value = 0;
if Total = 6 then
Inc(NumValues);
end;
WriteLn ('I found ', NumValues, ' numbers whose digits add up to six');
ReadLn;
end.
这里是解决方案,没有不必要的字符串转换。它的工作原理是获取最右边的数字,将其值添加到累加器
Total
,然后通过执行整数除以10来删除最右边的数字,并重复此过程,直到没有剩余的数字
var
Value, Digit, Total, NumValues: Integer;
i: Integer;
begin
NumValues := 0;
for i := 1 to 1000 do
begin
Value := i;
Total := 0;
repeat
Digit := Value mod 10;
Total := Total + Digit;
Value := Value div 10;
until Value = 0;
if Total = 6 then
Inc(NumValues);
end;
WriteLn ('I found ', NumValues, ' numbers whose digits add up to six');
ReadLn;
end.
这里是解决方案,没有不必要的字符串转换。它的工作原理是获取最右边的数字,将其值添加到累加器
Total
,然后通过执行整数除以10来删除最右边的数字,并重复此过程,直到没有剩余的数字
var
Value, Digit, Total, NumValues: Integer;
i: Integer;
begin
NumValues := 0;
for i := 1 to 1000 do
begin
Value := i;
Total := 0;
repeat
Digit := Value mod 10;
Total := Total + Digit;
Value := Value div 10;
until Value = 0;
if Total = 6 then
Inc(NumValues);
end;
WriteLn ('I found ', NumValues, ' numbers whose digits add up to six');
ReadLn;
end.
这里是解决方案,没有不必要的字符串转换。它的工作原理是获取最右边的数字,将其值添加到累加器
Total
,然后通过执行整数除以10来删除最右边的数字,并重复此过程,直到没有剩余的数字
var
Value, Digit, Total, NumValues: Integer;
i: Integer;
begin
NumValues := 0;
for i := 1 to 1000 do
begin
Value := i;
Total := 0;
repeat
Digit := Value mod 10;
Total := Total + Digit;
Value := Value div 10;
until Value = 0;
if Total = 6 then
Inc(NumValues);
end;
WriteLn ('I found ', NumValues, ' numbers whose digits add up to six');
ReadLn;
end.
我正试图学习在比赛中使用Pascal。我需要学会处理这样的情况,因为考试中会有类似的问题。哇。在过去的15年或20年中,我没有见过Pascal代码。你的问题是过去的爆炸!出于好奇,我想问一下,你今天是怎么用帕斯卡语言编程的?我看到了你对@MrPhi的回答。我上过一次帕斯卡大学,但那是1986年。希望这里有人记得他们的帕斯卡。我忘了。有谁能推荐一个Python代码作为初学者吗?@Cloud想出一种方法来获取任何整数的最后一位数字。然后,想一种通过“删除”最后一个数字来转换数字的方法。想一想这是如何通过算术实现的。例如,如何从
720
或725
获取72
。我正在尝试学习在比赛中使用Pascal。我需要学会处理这样的情况,因为考试中会有类似的问题。哇。在过去的15年或20年中,我没有见过Pascal代码。你的问题是过去的爆炸!出于好奇,我想问一下,你今天是怎么用帕斯卡语言编程的?我看到了你对@MrPhi的回答。我上过一次帕斯卡大学,但那是1986年。希望这里有人记得他们的帕斯卡。我忘了。有谁能推荐一个Python代码作为初学者吗?@Cloud想出一种方法来获取任何整数的最后一位数字。然后,想一种通过“删除”最后一个数字来转换数字的方法。想一想这是如何通过算术实现的。例如,如何从720
或725
获取72
。我正在尝试学习在比赛中使用Pascal。我需要学会处理这样的情况,因为考试中会有类似的问题。哇。在过去的15年或20年中,我没有见过Pascal代码。你的问题是过去的爆炸!出于好奇,我想问一下,你今天是怎么用帕斯卡语言编程的?我看到了你对@MrPhi的回答。我上过大学课程