摘要 :本文为一道基础编程题的题解,题目要求根据输入的正整数 N(1≤N≤9),输出一个下三角格式的 N×N 部分九九口诀表。文章详细说明了输入/输出格式,并给出了完整的 C 语言实现代码,通过双重循环控制行与列,使用
printf格式化输出左对齐、占 4 位宽度的乘积结果。
题目描述
下面是一个完整的下三角九九口诀表:
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
本题要求对任意给定的一位正整数 N,输出从 11 到 NN 的部分口诀表。
输入格式:
输入在一行中给出一个正整数 N(1≤N≤9)。
输出格式:
输出下三角 N*N 部分口诀表,其中等号右边数字占 4 位、左对齐。
输入样例:
4
输出样例:
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
代码部分实现
下面是该程序的执行流程图:
#mermaid-svg-w1QFVw8uQMeCNrnB{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-w1QFVw8uQMeCNrnB .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-w1QFVw8uQMeCNrnB .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-w1QFVw8uQMeCNrnB .error-icon{fill:#552222;}#mermaid-svg-w1QFVw8uQMeCNrnB .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-w1QFVw8uQMeCNrnB .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-w1QFVw8uQMeCNrnB .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-w1QFVw8uQMeCNrnB .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-w1QFVw8uQMeCNrnB .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-w1QFVw8uQMeCNrnB .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-w1QFVw8uQMeCNrnB .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-w1QFVw8uQMeCNrnB .marker{fill:#333333;stroke:#333333;}#mermaid-svg-w1QFVw8uQMeCNrnB .marker.cross{stroke:#333333;}#mermaid-svg-w1QFVw8uQMeCNrnB svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-w1QFVw8uQMeCNrnB p{margin:0;}#mermaid-svg-w1QFVw8uQMeCNrnB .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-w1QFVw8uQMeCNrnB .cluster-label text{fill:#333;}#mermaid-svg-w1QFVw8uQMeCNrnB .cluster-label span{color:#333;}#mermaid-svg-w1QFVw8uQMeCNrnB .cluster-label span p{background-color:transparent;}#mermaid-svg-w1QFVw8uQMeCNrnB .label text,#mermaid-svg-w1QFVw8uQMeCNrnB span{fill:#333;color:#333;}#mermaid-svg-w1QFVw8uQMeCNrnB .node rect,#mermaid-svg-w1QFVw8uQMeCNrnB .node circle,#mermaid-svg-w1QFVw8uQMeCNrnB .node ellipse,#mermaid-svg-w1QFVw8uQMeCNrnB .node polygon,#mermaid-svg-w1QFVw8uQMeCNrnB .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-w1QFVw8uQMeCNrnB .rough-node .label text,#mermaid-svg-w1QFVw8uQMeCNrnB .node .label text,#mermaid-svg-w1QFVw8uQMeCNrnB .image-shape .label,#mermaid-svg-w1QFVw8uQMeCNrnB .icon-shape .label{text-anchor:middle;}#mermaid-svg-w1QFVw8uQMeCNrnB .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-w1QFVw8uQMeCNrnB .rough-node .label,#mermaid-svg-w1QFVw8uQMeCNrnB .node .label,#mermaid-svg-w1QFVw8uQMeCNrnB .image-shape .label,#mermaid-svg-w1QFVw8uQMeCNrnB .icon-shape .label{text-align:center;}#mermaid-svg-w1QFVw8uQMeCNrnB .node.clickable{cursor:pointer;}#mermaid-svg-w1QFVw8uQMeCNrnB .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-w1QFVw8uQMeCNrnB .arrowheadPath{fill:#333333;}#mermaid-svg-w1QFVw8uQMeCNrnB .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-w1QFVw8uQMeCNrnB .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-w1QFVw8uQMeCNrnB .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-w1QFVw8uQMeCNrnB .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-w1QFVw8uQMeCNrnB .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-w1QFVw8uQMeCNrnB .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-w1QFVw8uQMeCNrnB .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-w1QFVw8uQMeCNrnB .cluster text{fill:#333;}#mermaid-svg-w1QFVw8uQMeCNrnB .cluster span{color:#333;}#mermaid-svg-w1QFVw8uQMeCNrnB div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-w1QFVw8uQMeCNrnB .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-w1QFVw8uQMeCNrnB rect.text{fill:none;stroke-width:0;}#mermaid-svg-w1QFVw8uQMeCNrnB .icon-shape,#mermaid-svg-w1QFVw8uQMeCNrnB .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-w1QFVw8uQMeCNrnB .icon-shape p,#mermaid-svg-w1QFVw8uQMeCNrnB .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-w1QFVw8uQMeCNrnB .icon-shape .label rect,#mermaid-svg-w1QFVw8uQMeCNrnB .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-w1QFVw8uQMeCNrnB .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-w1QFVw8uQMeCNrnB .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-w1QFVw8uQMeCNrnB :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 是
是
否
否
开始
读取整数 N
i = 1(行控制)
i <= N ?
j = 1(列控制)
j <= i ?
输出 'j*i=乘积'(左对齐占4位)
j++
换行
i++
结束
c
#include <stdio.h>
int main(void)
{
int N, i, j; // 定义变量:N为输入的正整数,i和j为循环控制变量
scanf("%d", &N); // 读取用户输入的N值
// 外层循环:控制行数,从第1行到第N行
for (i = 1; i <= N; i++) {
// 内层循环:控制每行输出的列数,从第1列到第i列(下三角结构)
for (j = 1; j <= i; j++) {
// 格式化输出:j*i = 乘积,%-4d表示左对齐且占4位宽度
printf("%d*%d=%-4d", j, i, j * i);
}
printf("\n"); // 每行输出结束后换行
}
return 0; // 程序正常结束
}