(算法练习)——数学问题->简单数学

放在一个里面:

守型数:http://codeup.cn/problem.php?cid=100000588&pid=0

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
int main(){
	int n,m;
	while(scanf("%d",&n) != EOF){
		m = n * n;
		//signal用来记录初始的n 
		int signal = n;
		int count = 1;
		while(n /10>0){
			count++;
			n = n/10;
		}
		//这里要用int转化 
		int t = m %(int)(pow(10,count));
		//printf("%d\n",t);
		if(t == signal){
			printf("Yes!\n");
		}
		else{
			printf("No!\n");
		}
	}
	return 0;
}

反序数:http://codeup.cn/problem.php?cid=100000588&pid=1
代码:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;

int F(int a){
	int sum = 0;
	int record[10];
	for(int i = 3;i >=0;i--){
		record[i] = (int)a/(pow(10,i));
		sum = sum + record[i]*pow(10,3-i);
		a = a %(int)(pow(10,i));
	}
	
	memset(record,0,sizeof(record));
	return sum;
	
}
int main(){
	int n;
	for(int i = 1000;i <10000;i++){
		if(i*9 == F(i)){
			printf("%d\n",i);
		}
	}
	return 0;
}

百鸡问题:http://codeup.cn/problem.php?cid=100000588&pid=2
提交时答案错误,郁闷

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;

int main(){
	double n;
	while(scanf("%lf",&n) != EOF){
		int x = 0;
		int y = 0;
		int z = 100;
		while(x*5 + y*3 + z*(1.0/3.0) <=n){
			printf("x=%d,y=%d,z=%d\n",x,y,z);
			y++;
			z--;
			//提前一步判断,如果大于,则x++,并把y置位0 
			//z有可能减少为0 
			
			if(x*5 + y*3 + z*(1.0/3.0) >n || z<0){
				x++;
				y = 0;
				z = 100-x;
			}
		}
	}
	return 0;
} 

abc:http://codeup.cn/problem.php?cid=100000588&pid=3
依然是。。。暴力= =

#include <stdio.h>
#include <math.h>

int main(){
	int a,b,c;
	
	for(a = 0;a<=9;a++){
		for(b = 0;b <=9;b++){
			for(c = 0;c <= 9;c++){
				int m = a*100+b*10+c;
				int n = b*100+c*10+c;
				if(m + n == 532){
					printf("%d %d %d\n",a,b,c);
				}
			}
		}
	}
}

众数:http://codeup.cn/problem.php?cid=100000588&pid=4
显示答案错误,郁闷。。。

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;

int hashrecord[40]={0};
int main(){
	int record[30];
	int i = 0;
	int m;
	while(scanf("%d",&record[i]) != EOF){
		m = record[i];
		i++;
		hashrecord[m]++;
		if(getchar() == '\n'){
			int MAX= 0;
			int signal;
			for(int j = 1;j <11;j++){
				if(MAX<hashrecord[j]){
					MAX = hashrecord[j];
					signal = j;
				}
				else if(MAX == hashrecord[j]){
					if(signal>j){
						signal = j;
					}
				}
				else{
					;
				}
				
			}
			printf("%d\n",signal);
			signal = 0;
			memset(hashrecord,0,sizeof(hashrecord));
			memset(record,0,sizeof(record));
			i = 0;
			m = 0;
		}
	}
	return 0;
}
发布了104 篇原创文章 · 获赞 3 · 访问量 1940

猜你喜欢

转载自blog.csdn.net/weixin_42377217/article/details/104041329
今日推荐