在一行中输入未知数量的整数... [英] Input unknown number of integers in a line...

查看:90
本文介绍了在一行中输入未知数量的整数...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我需要将所有整数相加(它们用空格分隔)

我该怎么做?


例如

输入:1 4 5 2 3

总和:15

输入:2 3

总和:5

If I need to add up all integers in a line (they''re separated by space)
How can I do that??

e.g.
input : 1 4 5 2 3
sum : 15
input : 2 3
sum: 5

推荐答案

none<"" root \" @(none)">写道:
none <""root\"@(none)"> writes:
如果我需要将一行中的所有整数相加(它们用空格分隔)
我该怎么办?
If I need to add up all integers in a line (they''re separated by space)
How can I do that??




您可以编写一个C程序来完成它。你试过吗?

-

char a [] =" \ n .CJacehknorstu" ;; int putchar(int); int main(void){unsigned long b []

= {0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x1f6},* p =

b,x,i = 24; for(;; p + =!* p; * p / = 4)switch(x = * p& 3)case 0:{return 0; for(p - ; i - ; i - )case

2:{i ++; if(1)break; else default:continue; if(0)case 1:putchar(a [i& 15]); break;}}}



You could write a C program to do it. Have you tried?
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x1f6},*p=
b,x,i=24;for(;p+=!*p;*p/=4)switch(x=*p&3)case 0:{return 0;for(p--;i--;i--)case
2:{i++;if(1)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}


我搜索了函数..

但仍然无法找到任何线索..


我的第一个想法就是scanf整条线,

并使用itoa函数逐个添加整数,

但是如何隔离子串并使用itoa函数将其转换为

整数??


就好,

10 23 234

我可以检测到2个空格,

所以我需要剪切2个字符并将其放到另一个字符数组中?


Ben Pfaff提到:
I searched for functions..
but still can''t find any clues..

My first thought is scanf the whole line,
and use itoa function to add the integers one by one,
but how can I isolate the substring and use itoa function to turn it
into integer??

just like,
10 23 234
I can detect the 2 spaces,
so I need to cut the 2 characters and place it to another character array??

Ben Pfaff mentioned:
无<"" root \" @(无)">写道:

none <""root\"@(none)"> writes:

如果我需要将一行中的所有整数相加(它们用空格分隔)
我该怎么办?
If I need to add up all integers in a line (they''re separated by space)
How can I do that??



您可以编写一个C程序来完成它。你试过吗?


You could write a C program to do it. Have you tried?



none<"" root \" @(none)">写道:
none <""root\"@(none)"> writes:
我的第一个想法是scanf整行,
并使用itoa函数逐个添加整数,
但是如何隔离子串并使用itoa函数将它变成整数?
My first thought is scanf the whole line,
and use itoa function to add the integers one by one,
but how can I isolate the substring and use itoa function to turn it
into integer??




我认为你正在努力工作。没有理由

一次读完整行。一次读取并累积一个数字



-

int main(void){char p [] =" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz 。$ \\

\ n",* q =" kl BIcNBFr.NKEzjwCIxNJC" ;; int i = sizeof p / 2; char * strchr(); int putchar(\

); while(* q){i + = strchr(p,* q ++) - p; if(i> =(int)sizeof p)i- = sizeof p-1; putchar(p [i] \\ \\

);}返回0;}



I think you are trying to work too hard. There is no reason to
read the entire line all at once. Read and accumulate one number
at a time.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}


这篇关于在一行中输入未知数量的整数...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆