プロジェクトオイラー問112

http://odz.sakura.ne.jp/projecteuler/index.php?Problem%20112
はずみ数の割合が丁度99%となる最小のnを求めよ。

全探索で間に合います。

#include<stdio.h>

bool u(int n){
	int old=n%10,a;
	n/=10;
	while(n>0){
		a=n%10;
		if(a<old){
			return false;
		}
		old=a;
		n/=10;
	}
	return true;
} 
bool d(int n){
	int old=n%10,a;
 	n/=10;
	while(n>0){
		a=n%10;
		if(a>old){
			return false;
		}
		old=a;
		n/=10;
	}
	return true;
}

int main(){
	int c=1,i;
	for(i=2;i<c*100;i++){
		c+=(u(i)||d(i));
	}
	printf("ans=%d\n",i);
}
最終更新:2015年12月18日 08:29