年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1940|回复: 1

For循环嵌套输出类似QQ好友列表

[复制链接]
  • TA的每日心情
    奋斗
    2022-12-13 21:26
  • 签到天数: 371 天

    [LV.9]以坛为家II

    发表于 2014-2-26 21:21:34 | 显示全部楼层 |阅读模式
    代码示例:
    1. #include <stdio.h>
    2. int main () {
    3.    
    4.     int temp_i = 0;
    5.     int temp_j = 0;
    6.    
    7.     while (temp_i <= 0) {
    8.         
    9.         printf("请输入好友列表数:");
    10.         scanf("%d", &temp_i);
    11.         
    12.         while (temp_j <= 0) {
    13.             
    14.             printf("请输入好友数:");
    15.             scanf("%d", &temp_j);
    16.         }
    17.     }
    18. //    printf("temp_i=%d,temp_j=%d\n", temp_i, temp_j);
    19.     for (int i = 0; i < temp_i; i++) {
    20.         printf("好友列表%d\n", i+1);
    21.         
    22.         for (int j = 0; j < temp_j; j++) {
    23.             printf("    好友%d\n",j+1);
    24.         }
    25.     }
    26.    
    27.     return 0;
    28. }
    复制代码
    运行结果:
    1. 请输入好友列表数:3
    2. 请输入好友数:5
    3. 好友列表1
    4.     好友1
    5.     好友2
    6.     好友3
    7.     好友4
    8.     好友5
    9. 好友列表2
    10.     好友1
    11.     好友2
    12.     好友3
    13.     好友4
    14.     好友5
    15. 好友列表3
    16.     好友1
    17.     好友2
    18.     好友3
    19.     好友4
    20.     好友5
    复制代码


  • TA的每日心情
    奋斗
    2022-12-13 21:26
  • 签到天数: 371 天

    [LV.9]以坛为家II

     楼主| 发表于 2014-2-26 21:37:39 | 显示全部楼层
    同样的原理可以输出九九乘法表:
    1. #include <stdio.h>
    2. int main() {
    3.     for (int i = 1; i < 10; i++) {
    4.         
    5.         for (int j = 1; j <= i; j++) {
    6.             printf("%d×%d=%d  ",i, j, i*j);
    7.         }
    8.         
    9.     printf("\n");
    10.         
    11.     }
    12.         
    13.     return 0;
    14. }
    复制代码
    输出结果:
    1. 1×1=1  
    2. 2×1=2  2×2=4  
    3. 3×1=3  3×2=6  3×3=9  
    4. 4×1=4  4×2=8  4×3=12  4×4=16  
    5. 5×1=5  5×2=10  5×3=15  5×4=20  5×5=25  
    6. 6×1=6  6×2=12  6×3=18  6×4=24  6×5=30  6×6=36  
    7. 7×1=7  7×2=14  7×3=21  7×4=28  7×5=35  7×6=42  7×7=49  
    8. 8×1=8  8×2=16  8×3=24  8×4=32  8×5=40  8×6=48  8×7=56  8×8=64  
    9. 9×1=9  9×2=18  9×3=27  9×4=36  9×5=45  9×6=54  9×7=63  9×8=72  9×9=81  
    复制代码

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-5-4 05:02 , Processed in 0.047733 second(s), 23 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表