#include <stdio.h>
int main()
{ int x1,x2;
do
{fflush(stdin);
printf("Input x1, x2:\n");
scanf("%d,%d",&x1,&x2);
}while (x1*x2>=0);
printf("x1=%d,x2=%d\n",x1,x2);
return 0;
}
手動除錯時明明沒問題,為什么在測驗網站上說運行時間超出限制呢?
uj5u.com熱心網友回復:
while (x1*x2>=0);改成while (x1*x2>0);試試
uj5u.com熱心網友回復:
看來是要優化下程式,感覺printf不需要每次都輸出吧,不刷緩沖區也可以
#include <stdio.h>
int main()
{
int x1,x2;
printf("Input x1, x2:\n");
do
{
scanf("%d,%d",&x1,&x2);
}while (x1*x2>=0);
printf("x1=%d,x2=%d\n",x1,x2);
return 0;
}
uj5u.com熱心網友回復:
忘了把題目發過去。。。從鍵盤任意輸入兩個符號各異的整數,直到輸入的兩個整數滿足要求為止,然后列印這兩個數。請通過測驗找出下面這個程式存在的問題(不止一個問題哦),并改正。同時用下面給出的運行結果示例檢查修改后的程式。
#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
uj5u.com熱心網友回復:
/**<題目內容:
從鍵盤任意輸入兩個符號各異的整數,直到輸入的兩個整數滿足要求為止,
然后列印這兩個數。請通過測驗找出下面這個程式存在的問題(不止一個問題哦),
并改正。同時用下面給出的運行結果示例檢查修改后的程式。
#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"
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int x1, x2, n, flag;
do
{
flag = 0;
printf("Input x1, x2:\n");
n = scanf("%d,%d", &x1, &x2);
switch (n)
{
case 0:
while (getchar() != '\n');
flag = 1;
break;
case 1:
while (getchar() != '\n');
flag = 1;
break;
default:
if (x1 * x2 >= 0)
flag = 1;
else
flag = 0;
break;
}
}
while (flag == 1);
printf("x1=%d,x2=%d\n", x1, x2);
return 0;
}
uj5u.com熱心網友回復:
#include<stdio.h>int main()
{
int x1,x2,n;
printf("Input x1, x2:\n");
n=scanf("%d,%d",&x1,&x2);
while(n!=2||x1*x2>=0)
{
fflush(stdin);
printf("Input x1, x2:\n");
n=scanf("%d,%d",&x1,&x2);
}
printf("x1=%d,x2=%d\n",x1,x2);
return 0;
}
uj5u.com熱心網友回復:
#include <stdio.h>
int main()
{
int x1, x2, check;
printf("Input x1, x2:\n");
check = scanf("%d,%d",&x1,&x2);
while((check != 2) || (x1 * x2 >= 0 ))
{
while (getchar() != '\n');
printf("Input x1, x2:\n");
check = scanf("%d,%d",&x1,&x2);
}
printf("x1=%d,x2=%d\n",x1,x2);
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/49144.html
標籤:基礎類
