九度1026 1118 1138 1194

题目描述:

输入两个不超过整型定义的非负10进制整数A和B(<=231-1),输出A+B的m (1 < m <10)进制数。

输入:
输入格式:测试输入包含若干测试用例。每个测试用例占一行,给出m和A,B的值。
当m为0时输入结束。
输出:
输出格式:每个测试用例的输出占一行,输出A+B的m进制数。
样例输入:
8 1300 48
2 1 7
0
样例输出:
2504
1000
#include <iostream>
#include<stdio.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */


int main(int argc, char** argv) {
	int m;
	long long a,b;
	while(scanf("%d",&m)!=EOF)
	{
		if(m==0)break;
		scanf("%lld%lld",&a,&b);
		int arr[10000]={0},size=0;
		a=a+b;
		do
		{
			arr[size++]=a%m;
			a/=m;
		}while(a);
		for(int i=size-1;i>=0;i--)
		{
			printf("%d",arr[i]);
		}
		printf("\n");
	}
	return 0;
}
抄王道机试上的,其中lld我真的第一次接触。
 
  

九度OJ 1118 数制转换

题目描述:

    求任意两个不同进制非负整数的转换(2进制~16进制),所给整数在long所能表达的范围之内。    不同进制的表示符号为(0,1,...,9,a,b,...,f)或者(0,1,...,9,A,B,...,F)。

输入:

    输入只有一行,包含三个整数a,n,b。a表示其后的n 是a进制整数,b表示欲将a进制整数n转换成b进制整数。a,b是十进制整数,2 =< a,b <= 16。

    数据可能存在包含前导零的情况。

输出:

    可能有多组测试数据,对于每组数据,输出包含一行,该行有一个整数为转换后的b进制数。输出时字母符号全部用大写表示,即(0,1,...,9,A,B,...,F)。

样例输入:
15 Aab3 7
样例输出:
210306
提示:

可以用字符串表示不同进制的整数。


#include <iostream>
#include<stdio.h>
#include<string.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */


int main(int argc, char** argv) {
int a,b;
char str[40];
while(scanf("%d%s%d",&a,str,&b)!=EOF)
{
int tmp=0,c=1;
for(int i=strlen(str)-1;i>=0;i--)
{
int x;
if(str[i]>='0'&&str[i]<='9')
{
x=str[i]-'0';
}
else if(str[i]>='a'&&str[i]<='z')
{
x=str[i]-'a'+10;
}
else if(str[i]>='A'&&str[i]<='Z')
{
x=str[i]-'A'+10;
}
x*=c;
tmp+=x;
c*=a;
}
int size=0;
char ans[40];
do
{
ans[size++]=tmp%b<10?tmp%b+'0':tmp%b-10+'A';
tmp/=b;
}while(tmp);
for(int i=size-1;i>=0;i--)
printf("%c",ans[i]);
printf("\n");

return 0;

}

1138

题目描述:

将一个长度最多为30位数字的十进制非负整数转换为二进制数输出。

输入:

多组数据,每行为一个长度不超过30位的十进制非负整数。
(注意是10进制数字的个数可能有30个,而非30bits的整数)

输出:

每行输出对应的二进制数。

样例输入:
0
1
3
8
样例输出:
0
1
11
1000


#include <iostream>
#include<stdio.h>
#include<string.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */


int main(int argc, char** argv) {
int n;
while(scanf("%d",&n)!=EOF)
{
char ans[200];
int size=0;
do
{
int x=n%2;
ans[size++]=x+'0';
n/=2;
}while(n);
for(int i=size-1;i>=0;i--)
{
printf("%c",ans[i]);
}
printf("\n");
}
return 0;
}

九度OnlineJudge题目1194:八进制

题目描述:

输入一个整数,将其转换成八进制数输出。

输入:

输入包括一个整数N(0<=N<=100000)。

输出:

可能有多组测试数据,对于每组数据,
输出N的八进制表示数。

样例输入:
7
8
9
样例输出:
7
10
11
#include <iostream>
#include<stdio.h>
#include<string.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */


int main(int argc, char** argv) {
int n;
while(scanf("%d",&n)!=EOF)
{
char ans[50];
int size=0;
do
{
int x=n%8;
n/=8;
ans[size++]=x+'0';
}while(n);
for(int i=size-1;i>=0;i--)
{
printf("%c",ans[i]);

printf("\n");
}
return 0;
}

同样手法,后几题都比较简单。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值