From 2a6031a4b06668e874df0003fa5375343e49fe36 Mon Sep 17 00:00:00 2001 From: zh <2088376133@qq.com> Date: Tue, 19 Sep 2023 09:13:38 +0000 Subject: [PATCH] =?UTF-8?q?add=20chapter1.1=5F=E6=B1=82=E4=B8=A4=E6=95=B0?= =?UTF-8?q?=E4=B9=8B=E5=92=8Ccpp.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zh <2088376133@qq.com> --- ...44\346\225\260\344\271\213\345\222\214cpp" | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 "chapter1.1_\346\261\202\344\270\244\346\225\260\344\271\213\345\222\214cpp" diff --git "a/chapter1.1_\346\261\202\344\270\244\346\225\260\344\271\213\345\222\214cpp" "b/chapter1.1_\346\261\202\344\270\244\346\225\260\344\271\213\345\222\214cpp" new file mode 100644 index 00000000..cdace292 --- /dev/null +++ "b/chapter1.1_\346\261\202\344\270\244\346\225\260\344\271\213\345\222\214cpp" @@ -0,0 +1,19 @@ +//求1~n之间所有整数之和,输出第一次大于或等于100的值和n的值,可以用两个变量,加一个标志位来实现,当第一次总和大于等于100的时候,分别记录总和和n的值,并将标志位设置为禁止即可。 +#include +int main() +{ + int n=0; + int i=1; + int sum=0; + int flag=0; + int x=0,nx=0; + scanf("%d",&n); + while(i<=n) + { + sum+=i; + if(sum>=100 && flag==0) x=sum,nx=i,flag=1; + i++; + } + printf("总和:%d\n\r",sum); + printf("第一次和大于等于100时的和为%d,此时n为%d\n\r",x,nx); +} \ No newline at end of file -- Gitee