• vc++ 6.0 的%u 与 %ul


    刚刚在做实验,书本里面有代码如下:
    View Code
    1 #include "stdio.h"
    2
    3 unsigned long lcm(unsigned a,unsigned b);
    4 unsigned long lcm3(unsigned a,unsigned b,unsigned c);
    5
    6 unsigned long lcm(unsigned a,unsigned b)
    7 {
    8 unsigned long multiple,c=0;
    9
    10 multiple = a* b;
    11 while(a%b!=0)
    12 {
    13 c=a%b;
    14 a=b;
    15 b=c;
    16 }
    17
    18 return multiple/b;
    19 }
    20
    21 unsigned long lcm3(unsigned a,unsigned b,unsigned c)
    22 {
    23 return lcm(lcm(a,b),c);
    24 }
    25
    26
    27 int main()
    28 {
    29 unsigned long x,y,z;
    30 printf("please input three number:");
    31 scanf("%ul %ul %ul",&x,&y,&z);
    32 printf("%ul %ul %ul",x,y,z);
    33 printf("the result is %ul",lcm3(x,y,z));
    34 return 0;
    35 }
    基本思路是说想求三个数的公约数,但在vc++ 6.0 里结果却是错的...
    后来不断地排除,发现是在vc++ 6.0 里用不了 %ul,必须改成 %u...
    修改后的代码如下:
    View Code
    1 #include "stdio.h"
    2
    3 unsigned long lcm(unsigned a,unsigned b);
    4 unsigned long lcm3(unsigned a,unsigned b,unsigned c);
    5
    6 unsigned long lcm(unsigned a,unsigned b)
    7 {
    8 unsigned long multiple,c=0;
    9
    10 multiple = a* b;
    11 while(a%b!=0)
    12 {
    13 c=a%b;
    14 a=b;
    15 b=c;
    16 }
    17
    18 return multiple/b;
    19 }
    20
    21 unsigned long lcm3(unsigned a,unsigned b,unsigned c)
    22 {
    23 return lcm(lcm(a,b),c);
    24 }
    25
    26
    27  int main()
    28 {
    29 unsigned long x,y,z;
    30 printf("please input three number:");
    31 scanf("%u %u %u",&x,&y,&z);
    32 printf("%u %u %u",x,y,z);
    33 printf("the result is %u",lcm3(x,y,z));
    34 return 0;
    35 }
  • 相关阅读:
    5. Longest Palindromic Substring
    24. Swap Nodes in Pairs
    23. Merge k Sorted Lists
    22. Generate Parentheses
    21. Merge Two Sorted Lists
    20. Valid Parentheses
    19. Remove Nth Node From End of List
    18. 4Sum
    17. Letter Combinations of a Phone Number
    14. Longest Common Prefix
  • 原文地址:https://www.cnblogs.com/laizhd/p/2004492.html
Copyright © 2020-2023  润新知