Global Scholar Interview Question
Developer Program EngineersHOW ABOUT THIS BELOW:
public static boolean numberCheck(int num){
String numStr = ""+num; //先把这个数字包装到字符串中,以方便用每个字符查找
if(numStr.length()>10){
return false; //显然如果这个数是十一位数或者更大,根本不能满足
}
//char[] numChars = numStr.toCharArray();
for(int i =0;i<numStr.length();i++){
int times = Integer.parseInt(numStr.substring(i,i+1)); //i在num出现的次数
int count =0;
for(int j = 0;j<numStr.length();j++){
if((String.valueOf(i).equals(String.valueOf(numStr.charAt(j))))){
++count;
}
}
if(times!=count){
return false;
}
}
return true;
}
int indexproperty_check(int data)
{
int data1=data;
int a[9]={0};
int digit_size=0;
while(data1)
{
a[data1%10]++;
data1=data1/10;
digit_size++;
}
for(int i=0;i<digit_size;i++)
{
data1 = a[i]+data1*10;
}
if(data1==data)
{
cout<<"Index property is satisfied";
return 1;
}
else
{
cout<<"Index property is not satisfied";
return 0;
}
}
public static boolean isStructure(int input){
int divider=1;
int digits=0;
int max=Integer.MIN_VALUE;
while(input/divider>0){
if((input/divider)%10>max)
max=(input/divider)%10;
divider*=10;
digits++;
}
if(max>=digits)
return false;
int a[]= new int[digits];
int inputcopy=input;
while(inputcopy>0){
a[inputcopy%10]++;
inputcopy/=10;
}
int result=0;
int i=0;
while(--digits>=0){
result+=a[i++]*Math.pow(10,digits);
}
return result==input;
}
public static void main(String args[]) {
for(int i=0; i<10000;i++){
if(Numbers.isStructure(i))
System.out.println(i);
}
}
#include <iostream>
#include <conio.h>
using namespace std;
#include <math.h>
#include <vector>
int _tmain(int argc, _TCHAR* argv[])
{
int n;
cin >> n;
/* Find Number of Digits in the Input Number */
int NumOfDig = ceil(log10(n));
/* Vector to maintain the Value of the Input Digit in the Index */
vector<int> arr(NumOfDig);
/* Vector to maintain Number of Occurences of a value (0-9) in the Input Number */
vector<int> NumOfOccurences(10);
int counter = NumOfDig - 1;
while(counter >= 0)
{
int rem = n % 10;
arr[counter] = rem;
NumOfOccurences[rem] += 1;
n /= 10;
counter--;
}
vector<int>::iterator it;
int index;
for(index = 0, it = arr.begin(); it != arr.end(); it++, index++)
{
if((*it) != NumOfOccurences[index])
{
cout << endl << "NO";
_getch();
return 0;
}
}
cout << endl << "YES";
_getch();
return 0;
}
Hope this code works.
- Vanathi August 07, 2011int main()
{
int n,a[4]={0}, i=3, b[4]={0}, flag=1;
scanf("%d",&n);
while(n>0)
{
a[i--]=n%10;
n=n/10;
}
for(i=0;i<4;i++)
b[a[i]]++;
for(i=0;i<4;i++)
if(a[i]!=b[i])
{
flag=0;
break;
}
if(flag)
printf("given number has same property");
else printf("given number doesn't possess the property");
}
}}}