如何检查数字是否在 c 中下降?
How can I check if a number is descending in c?
我如何检查数字是否递减,并有一个指示是或否的标志?
不是数组。
在最后一个循环中,last 或 x 取值 0,因此答案不正确。
while(n>1)
{
last=n%10;
x=n/10%10;
if(last>x){
flag=0;
}
if(last<x){
flag=1;
}
n=n/10;
}
if(flag==0) printf("0\n");
else printf("1\n");
#include <stdio.h>
#include <stdbool.h>
bool isDescending(int n)
{
bool descending=true;
while(n/10 && descending)
{
int d=n%10;
n/=10;
descending = d<n%10;
}
return descending;
}
int main(void) {
int test[] = { 123456, 975310 };
for(int i=0; i<2; ++i)
{
printf("Descending %d: %s\n", test[i], isDescending(test[i])? "Yes" : "No" );
}
return 0;
}
结果:
Success #stdin #stdout 0s 5432KB
Descending 123456: No
Descending 975310: Yes
我如何检查数字是否递减,并有一个指示是或否的标志? 不是数组。
在最后一个循环中,last 或 x 取值 0,因此答案不正确。
while(n>1)
{
last=n%10;
x=n/10%10;
if(last>x){
flag=0;
}
if(last<x){
flag=1;
}
n=n/10;
}
if(flag==0) printf("0\n");
else printf("1\n");
#include <stdio.h>
#include <stdbool.h>
bool isDescending(int n)
{
bool descending=true;
while(n/10 && descending)
{
int d=n%10;
n/=10;
descending = d<n%10;
}
return descending;
}
int main(void) {
int test[] = { 123456, 975310 };
for(int i=0; i<2; ++i)
{
printf("Descending %d: %s\n", test[i], isDescending(test[i])? "Yes" : "No" );
}
return 0;
}
结果:
Success #stdin #stdout 0s 5432KB
Descending 123456: No
Descending 975310: Yes