【C刷题记录】日常超时我佛了

题目:
程序改错-3(4分)
题目内容:

从键盘任意输入两个符号各异的整数,直到输入的两个整数满足要求为止,然后打印这两个数。请通过测试找出下面这个程序存在的问题(不止一个问题哦),并改正。同时用下面给出的运行结果示例检查修改后的程序。

#include <stdio.h>
  int main()
  {
    int x1, x2;
    do{
      printf("Input x1, x2:");
      scanf("%d,%d", &x1, &x2);
    }while (x1 * x2 > 0);
    printf("x1=%d,x2=%d\n", x1, x2);
    return 0;
  }

程序正确的运行结果示例:
Input x1, x2:
a,s↙
Input x1, x2:
a,1↙
Input x1, x2:
2,s↙
Input x1, x2:
1,2↙
Input x1, x2:
-1,-2↙
Input x1, x2:
0,3↙
Input x1, x2:
1.2,3.4↙
Input x1, x2:
1.2,5↙
Input x1, x2:
-1,3↙
x1=-1,x2=3
输入格式: “%d,%d”
输出格式:
输入提示信息:“Input x1, x2:\n”
输出: “x1=%d,x2=%d\n”
为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。
时间限制:500ms内存限制:32000kb

程序:
超时一号

#include <stdio.h>
  int main()
  {
    int a,b,c;
    printf("Input x1, x2:\n");
    c=scanf("%d,%d",&a,&b);
    while (a*b>=0||c!=2)
    {
        fflush(stdin);
        printf("Input x1, x2:\n");
        c=scanf("%d,%d",&a,&b);
    }
    printf( "x1=%d,x2=%d\n",a,b);
    return 0;
  }

超时二号

#include <stdio.h>
  int main()
  {
    int a,b,c;
    do
    {
        fflush(stdin);
        printf("Input x1, x2:\n");
        c=scanf("%d,%d",&a,&b);
    }while (a*b>=0||c!=2);
    printf( "x1=%d,x2=%d\n",a,b);
    return 0;
  }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值