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

放在一个里面:

守型数: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;
}

计算两个矩阵的乘积:http://codeup.cn/problem.php?cid=100000588&pid=5
很烂。。。
代码:

#include <stdio.h>
#include <string.h>

int main(){
	int a1[10];
	int a2[10];
	int b1[10];
	int b2[10];
	int b3[10];
	while(scanf("%d %d %d",&a1[0],&a1[1],&a1[2]) != EOF){
		getchar();
		scanf("%d %d %d",&a2[0],&a2[1],&a2[2]);
		getchar();
		scanf("%d %d",&b1[0],&b1[1]);
		getchar();
		scanf("%d %d",&b2[0],&b2[1]);
		getchar();
		scanf("%d %d",&b3[0],&b3[1]);
		int t = a1[0] * b1[0] + a1[1] * b2[0]+a1[2]*b3[0];
		int y = a1[0] * b1[1] + a1[1] * b2[1]+a1[2]*b3[1];
		int u = a2[0] * b1[0] + a2[1] * b2[0]+a2[2]*b3[0];
		int i = a2[0] * b1[1] + a2[1] * b2[1]+a2[2]*b3[1];
		
		printf("%d %d\n%d %d\n",t,y,u,i);
		t = 0;
		memset(a1,0,sizeof(a1));
		memset(a2,0,sizeof(a2));
		memset(b1,0,sizeof(b1));
		memset(b2,0,sizeof(b2));
		memset(b3,0,sizeof(b3));
	}
}

整数和:http://codeup.cn/problem.php?cid=100000588&pid=7
这题没啥好说的
代码:

#include <stdio.h>
#include <string.h>
int main(){
	int m;
	int record[200];
	int sumrecord[200];
	while(scanf("%d",&m) != EOF){
		int sum = 0;
		for(int i = 0;i <m;i++){
			scanf("%d",&record[i]);
		}
		for(int j = 0;j <m;j++){
			if(record[j] >=0){
				for(int s = record[j];s <=record[j]*2;s++){
					sum = sum + s;
				}
				sumrecord[j] = sum;
				sum = 0;
			}
			else{
				for(int s = record[j]*2;s <=record[j];s++){
					sum = sum + s;
				}
				sumrecord[j] = sum;
				sum = 0;
			}
		}
		for(int i = 0;i <m;i++){
			printf("%d\n",sumrecord[i]);
		}
		memset(record,0,sizeof(record));
		memset(sumrecord,0,sizeof(sumrecord));
	}
}

多项式的值:http://codeup.cn/problem.php?cid=100000588&pid=9
emmm一次过~
代码:

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

int main(){
	int m;
	int maxmi;
	int record[20];
	int x;
	while(scanf("%d",&m) != EOF){
		for(int i = 0;i <m;i++){
			scanf("%d",&maxmi);
			getchar();
			int signal = 0;
			while(signal<=maxmi){
				scanf("%d",&record[signal]);
				signal++;
			}
			getchar();
			scanf("%d",&x);
			int ans = 0;
			for(int s = 0;s<=maxmi;s++){
				ans = ans + record[s]*(int)pow(1.0*x,s);
			}
			printf("%d\n",ans);
			memset(record,0,sizeof(record)); 
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值