我想把从键盘输入一串数字int改为string类似 int a[]={7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1};的形式

math mode - Sum of (example: 0+1+2+3 = 6 , 0+1+2+3+4+5+6+7 = 28 and so on) - TeX - LaTeX Stack Exchange
to customize your list.
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. J it only takes a minute:
Here's how it works:
Anybody can ask a question
Anybody can answer
The best answers are voted up and rise to the top
Can anyone help please? I need to know how to make the sum of value S(n) = 0+1+2+3+4...+(n-1)+n (example: 0+1+2+3 = 6 , 0+1+2+3+4+5+6+7 = 28 and so on) on Latex
111k9193391
The difficult task is generating the terms of the sequence, not computing the sum, I present a macro that prints all the terms or just the sum. You can define a different starting point and another difference (defaults 0 and 1).
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\arithmeticsequence}{sO{}m}
\group_begin:
\keys_set:nn {rudstep/arseq} { #2 }
\IfBooleanTF{#1}
{ \rudstep_arseq_sum:n { #3 } }
{ \rudstep_arseq_full:n { #3 } }
\group_end:
\keys_define:nn { rudstep/arseq }
.int_set:N = \l_rudstep_diff_int,
start .int_set:N = \l_rudstep_start_int,
.initial:n = 1,
start .initial:n = 0,
\seq_new:N \l_rudstep_terms_seq
\cs_new_protected:Npn \rudstep_arseq_full:n #1
\seq_clear:N \l_rudstep_terms_seq
\int_step_inline:nnnn
{ \l_rudstep_start_int } % start
{ \l_rudstep_diff_int }
{ \l_rudstep_start_int + #1*\l_rudstep_diff_int }
{ \seq_put_right:Nn \l_rudstep_terms_seq { ##1 } }
$\seq_use:Nn \l_rudstep_terms_seq { + } = \rudstep_arseq_sum:n { #1 }$
\cs_new:Npn \rudstep_arseq_sum:n #1
\int_eval:n { (#1+1)*(2*\l_rudstep_start_int+#1*\l_rudstep_diff_int)/2 }
\ExplSyntaxOff
\begin{document}
Just the sum of five terms after 0: \arithmeticsequence*{5}
The whole sequence: \arithmeticsequence{3}
Or: \arithmeticsequence{4}
Start from 1: \arithmeticsequence[start=1]{3}
A big example:
\arithmeticsequence[start=81297,diff=198]{180}
\end{document}
Of course, if you know how to do it in Lisp, then here's the way:
\documentclass{article}
\usepackage{lisp-on-tex}
\begin{document}
\lispinterp{
(\define \intsum
(\lambda (\n)
(\lispif (\= \n :0 ) :0
(\+ (\intsum (\- \n :1)) \n))))}
\lispinterp{(\texprint(\intsum:100))}
\end{document}
that prints, as is well known,
Note: shamelessly adapted from the documentation of lisp-on-tex.
The e-TeX version of the same idea, using recursion, can be
\documentclass{article}
\newcommand{\Sn}[1]{%
\number\numexpr #1
\ifnum\numexpr#1-1&0
+\expandafter\Sn\expandafter{\number\numexpr#1-1}
\begin{document}
\end{document}
Note that this is fully expandable. The macro \Sn starts from the argument and if it's bigger than 1 asks to expand \Sn with the argument decreased by 1. One might start also from 0, but I'll leave it as an exercise to the interested reader.
496k5613312351
\documentclass{article}
\newcommand\foo[1]{\the\numexpr((#1)*(#1+1))/2\relax}
% or if you want to print the terms
\newcommand\foob[1]{$\fooc{#1}{0}=\foo{#1}$}
\newcommand\fooc[2]{\the\numexpr#2\relax\ifnum#1=#2\relax\else+\fooc{#1}{\numexpr#2+1\relax}\fi}
\begin{document}
\foo{8} and \foo{5}
\foob{8} and \foob{5}
\end{document}
334k268001408
Using Lua is probably a huge overkill in this situation, but it shows off how one can easily integrate Lua in LaTeX.
The code might also be easier to grasp for programmers who are beginners in LaTeX ;)
% !TEX TS-program = lualatex
% !TEX encoding = UTF-8 Unicode
\documentclass{article}
\usepackage[utf8]{luainputenc}
\usepackage{luacode}
% The code won't work as-is if you move it into \directlua{}.
% If you do so, be sure to replace "~" (in i ~= startInt) by \string~
\begin{luacode}
function calcSum(startInt, endInt)
local sum = 0
for i=startInt, endInt do
sum = sum + i
return sum
function writeSum(startInt, endInt)
local sum = 0
local tex = ""
for i=startInt, endInt do
if i ~= startInt then
tex = tex .. " + "
sum = sum + i
tex = tex .. i
tex = tex .. " = " .. sum
return tex
\end{luacode}
\def\calcSum#1#2{\directlua{
tex.print(calcSum(tonumber(#1), tonumber(#2)))
\def\writeSum#1#2{\directlua{
tex.print(writeSum(tonumber(#1), tonumber(#2)))
\begin{document}
The sum of the numbers \(0, 1, 2, \ldots, 28\) is \(\calcSum{0}{28}\).\\
Another sum: \(\writeSum{100}{103}\).
\end{document}
Here is a short expl3 approach using \int_step_inline:nnnn. This carries out the explicit sum and doesn't rely on Gauss' reduction. So this is for illustration purposes only.
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\cs_generate_variant:Nn \int_to_arabic:n { V }
\cs_new:Npn \rudstep_sum:n #1
\int_zero:N \l_tmpa_int
\int_step_inline:nnnn { 1 } { 1 } { #1 }
\int_add:Nn \l_tmpa_int { ##1 }
\int_to_arabic:V \l_tmpa_int
\NewDocumentCommand \calcsum {m}
\rudstep_sum:n { #1 }
\ExplSyntaxOff
\begin{document}
\calcsum{8}
\end{document}
27.8k367151
Using my package calculator, you can perform arithmetic calculations comfortably. This code solves your problem:
\documentclass{article}
\usepackage{ifthen}
\usepackage{calculator}
\newcounter{n}
\newcommand{\sumZeroToN}[2]{%
\COPY{0}{#2}
\whiledo{\not{\value{n}&#1}}{%
\ADD{#2}{\value{n}}{#2}\stepcounter{n}}}
\begin{document}
\sumZeroToN{100}{\sol}
\sum_{n=0}^{100} n = \sol
\end{document}
An expandable solution using the package
for computations and \xintiloop for expandably looping with an index.
\documentclass{article}
\usepackage{bnumexpr}% minimally extends \numexpr to big integers (new on CTAN
% for \xintiloop: (for fun)
\usepackage{xinttools}% automatically loaded by xint, which is loaded by
% bnumexpr, but in the future bnumexpr will only load a
% smaller part of xint, and xint itself will not load
% xinttools anymore.
\newcommand{\ArithmeticSequenceAndItsSum}[3]{%
% #1= initial term, A
% #2= common difference, d
% #3= number of terms, N
% sum is A+(A+d)+...+(A+(N-1)d)=N*A+d*N(N-1)/2
% (cf. Gauss Werke, Kindergarten Abteilung)
#1% assume N at least one and that #1 does not need to be evaluated
\xintiloop [1+1]
\unless\ifnum#3=\xintiloopindex
+\thebnumexpr#1+\xintiloopindex*#2\relax
=\thebnumexpr #3*#1+#2*#3*(#3-1)/2\relax
\begin{document}
$\ArithmeticSequenceAndItsSum {0}{1}{10}$
$\ArithmeticSequenceAndItsSum {1}{2}{10}$
\noindent $\ArithmeticSequenceAndItsSum {}{}{100}$
And now go see the log for some bigger integers!!
\message{\ArithmeticSequenceAndItsSum {}{}{1000}}
\end{document}
29.4k46101
protected by ♦
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10
on this site (the ).
Would you like to answer one of these
Not the answer you're looking for?
Browse other questions tagged
The week's top questions and answers
Important community announcements
Questions that need answers
By subscribing, you agree to the
TeX - LaTeX Stack Exchange works best with JavaScript enabled二维数组int a[3][4]={{1,2,3,4},{5,6,7,8},{9,0,1,2}};里面的数值怎么排列的?c语言
按行排列的,先排列第二维的,然后在排列第一维.二维数组实际的存储还是转换为一维数组来实现的.每个a[i][j]实际对应a[i*M+j],M是第二维的长度.
那么int a [3][4] ={{1},{5},{9}};是怎么回事
没有填的地方默认会用用0补齐,a[3][4]实际就是这样{{1,0,0,0},{5,0,0,0},{9,0,0,0}}。所以对于二维数组的定义,第二维长度必须是确定的,比如int a[][4],而int a[][]这样是不允许的。
为您推荐:
其他类似问题
扫描下载二维码int k,a[3][3]={1,2,3,4,5,6,7,8,9}; for(k=0;k&3;k+_百度知道
int k,a[3][3]={1,2,3,4,5,6,7,8,9}; for(k=0;k&3;k+
k++) printf(&_ಥ%d&quot,拜托了,因为,3; for(k=0,6,并且是详解哦,9};3,4;,╭(╯3╰)╮作为一名大学生党不容易啊(ಥ在c语言中的输出结果,5;k&lt,亲们,7,我不懂,8,a[k][2-k]),2,a[3][3]={1int k
提问者采纳
a[2][0], 1,也就是斜对角线上的347a相当于一个矩阵, a[1][1], 2所以打印的是a[0][2]:1 2 33 4 57 8 9然后k的取值是0
可不可以再解释下为什么a[0][2]是3,a[1][1]是4呀= ̄ω ̄=
不对,应该是357
c语言的数组喜欢从0开始记下标。a[0][0]是第0行第0列的值,也就是左上角的1;a[0][1]是第0行第1列的值,也就是2;a[0][2]是第0行第2列的值,也就是3;依此类推就好啦~
谢谢谢谢o(≧v≦)o
提问者评价
太给力了,你的回答完美地解决了我的问题,非常感谢!
其他类似问题
为您推荐:
其他1条回答
结果3 5 7 a是二维数组,%d是十进制
可不可以再解释下为什么a[0][2]是3,a[1][1]是4呀= ̄ω ̄=
可不可以再解释下为什么a[0][2]是3,a[1][1]是5呀= ̄ω ̄=
上面那个我问错了,不好意思
可以看作是3*3=9个长度的数组
以此为a[0][0],a[0][1]依次到最后a[2][2]
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁

我要回帖

更多关于 从键盘输入一串字符串 的文章

 

随机推荐