您的位置:首页 > 编程语言 > C语言/C++

【晨哥的C语言之旅】经典C语言程序100例(1--5)

2012-12-07 19:11 260 查看
题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?

1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去

      
掉不满足条件的排列。

//题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
#include <stdio.h>
void result();
main(){
result();
}

void result(){
int i, j, k;
int count,result;
count = 0;
for(i=1; i <= 4; i++){
for(j=1; j <= 4; j++){
for(k =1; k <=4; k++){
if(i != j && i != k && j != k){
result = i*100 + j*10 + k;
count++;
printf("%d\n", result);
}

}
}
}
printf("The number of the results is %d\n",count );

}


3. 题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
程序分析:在10万以内判断,先将该数加上100后再开方,再将该数加上268后再开方,如果开方后

      
的结果满足如下条件,即是结果。

/*一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,
请问该数是多少?
*/
#include <stdio.h>
#include <math.h>
main(){
int i ;
for(i = 0;i<100000; i++){
float a1,b1;
int a2,b2;
a1 = sqrt(i+100);
b1 = sqrt(i+268);
a2 = (int)a1;
b2 = (int)b1;
while((a1 - a2 == 0) && (b1 -b2 == 0)){
printf("The number is %d\n",i);
break;
}

}
}


4.题目:输入某年某月某日,判断这一天是这一年的第几天?

/*输入某年某月某日,判断这一天是这一年的第几天?

*/
#include <stdio.h>
main(){
int year = 2012;
int month = 12;
int  day = 7;
int month1[] = {31,28,31,30,31,30,31,31,30,31,30,31};
int month2[] = {31,29,31,30,31,30,31,31,30,31,30,31};
int result = 0;

if((year % 4 == 0 && year % 100 !=0) || (year % 400 == 0)){
for(int i = 0; i < month - 1; i++){
result += month1[i];
}
result += day;

}else {
for(int i = 0; i < month - 1; i++){
result += month2[i];
}
result += day;
}
printf("It is the %dth day of the year.", result);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: