1。 一人岁数的3次方是四位数,四次方是六位数,并知道此人岁数的3次方和4次方用遍了0~9十个数字。编写一程序求此人的岁数。
2。对1,2,3, 4, 5 这五个数任意取出两个数,列出他们的所有组合。
2。对1,2,3, 4, 5 这五个数任意取出两个数,列出他们的所有组合。
public static int getAge() {
int age;
int third;
int fourth;
for (int i = 11; true; i++) {
if (i < 200) {
third = (int) Math.pow(i, 3);
fourth = (int) Math.pow(i, 4);
if (getLength(third, fourth) == 10) {
age = i;
break;
}
}
}
return age;
}
public static int getLength(int args1, int args2) {
String str1 = String.valueOf(args1);
String str2 = String.valueOf(args2);
String str = str1 + str2;
if (str.length() != 10) {
return -1;
}
int[] intarray = new int[10];
for (int i = 0; i < str.length(); i++) {
intarray[i] = Integer.parseInt(str.substring(i,i+1));
}
Arrays.sort(intarray);
if(intarray[0]!=0 && intarray[9]!=9)
return -1;
return 10;
}
int age;
int third;
int fourth;
for (int i = 11; true; i++) {
if (i < 200) {
third = (int) Math.pow(i, 3);
fourth = (int) Math.pow(i, 4);
if (getLength(third, fourth) == 10) {
age = i;
break;
}
}
}
return age;
}
public static int getLength(int args1, int args2) {
String str1 = String.valueOf(args1);
String str2 = String.valueOf(args2);
String str = str1 + str2;
if (str.length() != 10) {
return -1;
}
int[] intarray = new int[10];
for (int i = 0; i < str.length(); i++) {
intarray[i] = Integer.parseInt(str.substring(i,i+1));
}
Arrays.sort(intarray);
if(intarray[0]!=0 && intarray[9]!=9)
return -1;
return 10;
}
第二题还更简单了
for(int i=1; i<6; i++){
for(int j=1; j<6; j++){
if(i==j){
System.out.println(j+""+j);
}else{
System.out.println(i+""+j);
System.out.println(j+""+i);
}
}
}
for(int i=1; i<6; i++){
for(int j=1; j<6; j++){
if(i==j){
System.out.println(j+""+j);
}else{
System.out.println(i+""+j);
System.out.println(j+""+i);
}
}
}
public class A {
// http://community.csdn.net/Expert/topic/4667/4667929.xml?temp=.57922
public static void main(String[] args) {
String t;
String[] s = new String[5];
int j = s.length;
for(int i=0; i<j; i++) {
s[i] = new Integer(i+1).toString();
}
for(int i=0; i<j; i++) {
t = s[i];
for(int a=0; a<j; a++) {
t += s[i];
System.out.println(t);
}
System.out.println();
}
}
}
// http://community.csdn.net/Expert/topic/4667/4667929.xml?temp=.57922
public static void main(String[] args) {
String t;
String[] s = new String[5];
int j = s.length;
for(int i=0; i<j; i++) {
s[i] = new Integer(i+1).toString();
}
for(int i=0; i<j; i++) {
t = s[i];
for(int a=0; a<j; a++) {
t += s[i];
System.out.println(t);
}
System.out.println();
}
}
}
第二题还更简单了
for(int i=1; i<6; i++){
for(int j=1; j<6; j++){
if(i==j){
System.out.println(j+""+j);
}else{
System.out.println(i+""+j);
System.out.println(j+""+i);
}
}
}
============================================================
楼上的没看清题目,它是让你对1,2,3, 4, 5 这五个数任意取出两个数,列出他们的所有组合,所以重复的数字不应该算在里面。
第二题应该改为:
for(int i=1; i<6; i++){
for(int j=1; j<6; j++){
if(i==j){
break;
}else{
System.out.println(i+""+j);
System.out.println(j+""+i);
}
}
}
for(int i=1; i<6; i++){
for(int j=1; j<6; j++){
if(i==j){
System.out.println(j+""+j);
}else{
System.out.println(i+""+j);
System.out.println(j+""+i);
}
}
}
============================================================
楼上的没看清题目,它是让你对1,2,3, 4, 5 这五个数任意取出两个数,列出他们的所有组合,所以重复的数字不应该算在里面。
第二题应该改为:
for(int i=1; i<6; i++){
for(int j=1; j<6; j++){
if(i==j){
break;
}else{
System.out.println(i+""+j);
System.out.println(j+""+i);
}
}
}
public class B {
public static void main(String[] args) {
for (int i = 1; i < 6; i++) {
int t = i;
for(int a = 0; a<5; a++) {
int c = a+1;
if(c == t) {
continue;
}else {
System.out.println(t*10+c);
}
}
System.out.println();
}
}
}
public static void main(String[] args) {
for (int i = 1; i < 6; i++) {
int t = i;
for(int a = 0; a<5; a++) {
int c = a+1;
if(c == t) {
continue;
}else {
System.out.println(t*10+c);
}
}
System.out.println();
}
}
}
第二题
public class Test { public static void main(String[] args) { int[][] a=new int[5][]; for(int i=0;i<a.length;i++) { a[i]=new int[i+1]; } for(int i=1;i<=a.length;i++) { for(int j=i+1;j<=a.length;j++) { System.out.print(i); System.out.print(j+" "); } System.out.print(" "); } for(int i=a.length;i>0;i--) { for(int j=i-1;j>0;j--) { System.out.print(i); System.out.print(j+" "); } System.out.print(" "); } } } |
public class Test { public static int getDigits(String str) { int[] intarr = new int[10]; for (int i = 0; i < 10; i++) intarr[i] = 0; for (int i = 0; i < str.length(); i++) { int j = Integer.parseInt(str.substring(i, i + 1)); intarr[j] = 1; } int num = 0; for (int i = 0; i < 10; i++) num = num + intarr[i]; return num; } private static int getAge() { int age; int third; int fourth; for (age = 1; age < 100; age++) { third = (int) Math.pow(age, 3); fourth = (int) Math.pow(age, 4); if (third < 1000 || third >= 10000) continue; if (fourth < 100000 || fourth >= 1000000) continue; String str = String.valueOf(third) + String.valueOf(fourth); if (getDigits(str) == 10) return age; } return 0; } } |
第二道题
class Combine
{
public static void main(String[] args)
{
for(int i=1; i<5; i++)
{
for(int j=i+1; j<6; j++)
{
System.out.println(i+""+j);
System.out.println(j+""+i);
}
}
}
class Combine
{
public static void main(String[] args)
{
for(int i=1; i<5; i++)
{
for(int j=i+1; j<6; j++)
{
System.out.println(i+""+j);
System.out.println(j+""+i);
}
}
}
public class Age
{
public static void main(String [] args)
{
String str1 = null;
String str2 = null;
String str3 = null;
String str4 = "0123456789";
for(int i=10;i<50;i++)
{
str1 = Integer.toString(i*i*i);
str2 = Integer.toString(i*i*i*i);
str3 = str1+str2;
if((str1.length() == 4) && (str2.length() ==6))
{
boolean flag = true;
for(int j=0;j<10;j++)
if(str3.indexOf(str4.charAt(j))==-1)
flag = false;
if(flag){
System.out.println(">>>"+i);
System.out.println(str3);
}
}
}
}
}
{
public static void main(String [] args)
{
String str1 = null;
String str2 = null;
String str3 = null;
String str4 = "0123456789";
for(int i=10;i<50;i++)
{
str1 = Integer.toString(i*i*i);
str2 = Integer.toString(i*i*i*i);
str3 = str1+str2;
if((str1.length() == 4) && (str2.length() ==6))
{
boolean flag = true;
for(int j=0;j<10;j++)
if(str3.indexOf(str4.charAt(j))==-1)
flag = false;
if(flag){
System.out.println(">>>"+i);
System.out.println(str3);
}
}
}
}
}
比赛贴 ~ 微软又一道笔试题
silver6 | 04
四月
, 2006 09:48
怎样只用
4
行代码编写出一个从字符串到长整形的转换函数?
我的方法,不过好象比
4
行多
*_#!~
long atol(char *str)
{
char c = *str;
if( !isdigit(c) ) str++;
for(long value = 0; *str != ''; value = value * 10 + (*str -'0'),str++);
return c == '-' ? -value : value ;
}
long atol(char *str)
{
char c = *str;
if( !isdigit(c) ) str++;
for(long value = 0; *str != ''; value = value * 10 + (*str -'0'),str++);
return c == '-' ? -value : value ;
}
void stol(const char * des, long& num)
{
for (int base = 1, i = 0; des[i] != ''; base = 10, ++i)
{
num *= base;
num += (int)(des[i] - '0');
}
}
num 要初始化为 0
{
for (int base = 1, i = 0; des[i] != ''; base = 10, ++i)
{
num *= base;
num += (int)(des[i] - '0');
}
}
num 要初始化为 0
void stol(const char * des, long& num) { for (int i=num=0; des[i] != ''; i++) { num *= 10; num += (int)(des[i] - '0'); } } |
void stol(char *str, long &num)
{
while(*str != '')
{
num = num * 10 + (*str - '0');
str++;
}
}
{
while(*str != '')
{
num = num * 10 + (*str - '0');
str++;
}
}
void stol(const char * des, long& num)
{
char p = des[0];
for (int b = 1, pos = 1, base = 1; des[pos] != ''; b = 10, ++pos, base *= 10)
{
(num *= b) += (int)(des[pos] - '0');
}
p == '-' ? (num *= -1) : (num = (int)(des[0] - '0') * base + num);
}
改了一下
{
char p = des[0];
for (int b = 1, pos = 1, base = 1; des[pos] != ''; b = 10, ++pos, base *= 10)
{
(num *= b) += (int)(des[pos] - '0');
}
p == '-' ? (num *= -1) : (num = (int)(des[0] - '0') * base + num);
}
改了一下
真的是微软的笔试题么?
我只用了一行。
#include <iostream>
using namespace std;
long str2long(char* p,long xxx=0L)
{
return *p==''?xxx:str2long(p,xxx*10+(*p+++0-'0'));
}
int main()
{
char *str="123456789",*p=str;
cout<<str2long(p);
getchar();
return 0;
}
我只用了一行。
#include <iostream>
using namespace std;
long str2long(char* p,long xxx=0L)
{
return *p==''?xxx:str2long(p,xxx*10+(*p+++0-'0'));
}
int main()
{
char *str="123456789",*p=str;
cout<<str2long(p);
getchar();
return 0;
}
用
STL
,四行
#include <sstream>
#include <iostream>
#include <string>
using namespace std;
long ToLong(string& s)
{
long l;
istringstream iss(s);
iss>>l;
return l;
}
int main(int argc, _TCHAR* argv[])
{
string s = "-12356";
cout<<ToLong(s);
return 0;
}
#include <sstream>
#include <iostream>
#include <string>
using namespace std;
long ToLong(string& s)
{
long l;
istringstream iss(s);
iss>>l;
return l;
}
int main(int argc, _TCHAR* argv[])
{
string s = "-12356";
cout<<ToLong(s);
return 0;
}
谢谢刚才上面的帖子提醒负数的问题,我更正了,还是只用一行:
#include <iostream>
using namespace std;
long str2long(char* p,long xxx=0L,bool IsPositive=true)
{
return *p==''?(IsPositive?xxx:xxx*(-1)):(*p=='-'?str2long(++p,0L,false):str2long(p,xxx*10+*p+++0-'0',IsPositive));
}
int main()
{
char *str="-123456789",*p=str;
cout<<str2long(p);
getchar();
return 0;
}
#include <iostream>
using namespace std;
long str2long(char* p,long xxx=0L,bool IsPositive=true)
{
return *p==''?(IsPositive?xxx:xxx*(-1)):(*p=='-'?str2long(++p,0L,false):str2long(p,xxx*10+*p+++0-'0',IsPositive));
}
int main()
{
char *str="-123456789",*p=str;
cout<<str2long(p);
getchar();
return 0;
}