hdoj2031
我的思路:(注意数值转字符或字符串)
//hdoj 2031
#include <string>
#include <stdio.h>
#include <iostream>
#include <sstream>//字符串流对象
using namespace std;
int main(){
int n,r;
bool minus;
string s;
while(scanf("%d %d",&n,&r)!=EOF){
s="";
minus=false;
if(n<0){
minus=true;
n=-n;//转为相反数->正数
}
while(n){
int a=n%r;
if(a>=10) a='A'+a-10;
if(a>=0&&a<=9){
ostringstream os;
os<<a;
s=os.str()+s;
}
else{
s=char(a)+s;//char
}
n/=r;
}
if(minus)
s='-'+s;
cout<<s<<endl;
}
return 0;
}
别人的思路:
先用int数组存储余数,再将余数倒着printf,用%d和%c决定输出的是数字(<=9)还是字符(>9,通过ASCⅡ码转换)
hdoj 2092
//hdoj 2092
#include <iostream>
#include <cmath>
using namespace std;
int main(){
int n,m;
while(cin>>n>>m,n||m){
int squre_sum=pow(n,2)-2*m;
int sub=sqrt(squre_sum-2*m);
if(sub!=sqrt(squre_sum-2*m)){//利用完全平方公式的转换
cout<<"No"<<endl;
continue;
}
cout<<"Yes"<<endl;
}
return 0;
}
hdoj 2097
用人工方法求出转换进制后的每一位数字,好像没有直接调用函数的妙法?
补充知识:(没用到)
#include <stdlib.h>///itoa 作进制转换第一个参数是要转换的数字 第二个参数是要写入转换结果的目标字符串 第三个参数是基数
#include <stdio.h>
sscanf(s,"%d",&n);//从字符串s中读入整数n
sprintf(s,"%d",n);//将n转换为字符串s
sprintf(str, "%x", 100); //将100转为16进制表示的字符串
//hdoj 2097
#include <iostream>
using namespace std;
int get_sum(int n,int r){
int sum=0;
while(n){
sum+=n%r;
n/=r;
}
return sum;
}
int main(){
int n;
while(cin>>n,n){
if(get_sum(n,10)==get_sum(n,16)&&get_sum(n,16)==get_sum(n,12))
cout<<n<<" is a Sky Number."<<endl;
else cout<<n<<" is not a Sky Number."<<endl;
}
return 0;
}
hdoj 2098
//hdoj 2098
#include <iostream>
#include <vector>
using namespace std;
const int maxn=10010;
vector<bool> prime(maxn,true);
void get_prime(){
prime[0]=false;
prime[1]=false;
for(int i=4;i<maxn;i+=2) prime[i]=false;
for(int i=3;i<maxn;i+=2){
if(prime[i]==true){
for(int j=i*i;j<maxn;j+=i){
prime[j]=false;
}
}
}
}
int main(){
get_prime();
int n;
while(cin>>n,n){
int count=0;
for(int i=3;i<n/2;i+=2){
if(prime[i]&&prime[n-i]){
++count;
}
}
cout<<count<<endl;
}
return 0;
}
hdoj 2136
//hdoj 2136
#include <stdio.h>
#include <vector>//
using namespace std;//
const int maxn=1000000;
vector<bool> isprime(maxn,true);
vector<int> lpf(maxn,0);
int count_prime=0;
void set_prime(){
isprime[0]=false;
isprime[1]=false;
for(int i=2;i<maxn;++i){
if(isprime[i]){
++count_prime;
lpf[i]=count_prime;
for(int j=i;j<maxn;j+=i){//需要对已标记为false的数进行操作,如6=2*3,在i=3时,需要对6更新lpf值
isprime[j]=false;
lpf[j]=count_prime;
}
}
}
}
int main(){
set_prime();
int n;
while(scanf("%d",&n)!=EOF){
printf("%d\n",lpf[n]);
}
return 0;
}
hdoj 1108
最小公倍数,利用最大公约数乘上各自的非公因子
//hdoj 1108
#include <stdio.h>
int main(){
int a,b;
while(scanf("%d %d",&a,&b)!=EOF){
int x=a,y=b;
//辗转相除法求最大公约数
while(y!=0){
int r=x%y;
x=y;
y=r;
}
//x为最大公约数
printf("%d\n",a/x*b);
}
return 0;
}
hdoj 1713
通分后,对分子求最小公倍数。约分时,对分子分母求最大公约数。
//hdoj 1713
#include <stdio.h>
long long gcd(long long x,long long y){//辗转相除法求最大公约数
while(y!=0){
long long r=x%y;
x=y;
y=r;
}
return x;
}
int main(){
long long a,b,c,d;
int T;
scanf("%d",&T);
for(int i=0;i<T;++i){
scanf("%lld/%lld %lld/%lld",&a,&b,&c,&d);
//通分
long long m=b*d;//分母
long long x=a*d,y=c*b;//分子
x=gcd(x,y);//x为最大公约数
long long n=a*d/x*b*c;//分子的最小公倍数————>结果的分子
long long res=n/m;
if(res*m<n){
long long t=gcd(n,m);
printf("%lld/%lld\n",n/t,m/t);
}
else printf("%lld\n",res);
}
return 0;
}
hdoj 1722
一次生日Party可能有p人或者q人参加,现准备有一个大蛋糕.问最少要将蛋糕切成多少块(每块大小不一定相等),才能使p人或者q人出席的任何一种情况,都能平均将蛋糕分食.
神奇的解法。。p+q-gcd(p,q)
//hdoj 1722
#include <stdio.h>
int gcd(int x,int y){//辗转相除法求最大公约数
while(y!=0){
int r=x%y;
x=y;
y=r;
}
return x;
}
int main(){
int p,q;
while(scanf("%d %d",&p,&q)!=EOF){
printf("%d\n",p+q-gcd(p,q));
}
return 0;
}
hdoj 2504
//hdoj 2504
#include <iostream>
using namespace std;
int gcd(int x,int y){
while(y!=0){
int r=x%y;
x=y;
y=r;
}
return x;
}
int main(){
int n;
cin>>n;
for(int i=0;i<n;++i){
int a,b;
cin>>a>>b;
int x=a,y=b;
x=gcd(x,y);
int m=a/x;
for(int j=2;;++j){
if(gcd(m,j)==1){
cout<<x*j<<endl;
break;
}
}
}
return 0;
}
hdoj 1717
小数化分数(循环or普通小数)
这篇博客写的很好,解释了为何要对n位循环部分除以n个9。开启任意门
//hdoj 1717
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int gcd(int x,int y){
while(y!=0){
int r=x%y;
x=y;
y=r;
}
return x;
}
int main(){
int n;
cin>>n;
for(int i=0;i<n;++i){
string s;
cin>>s;
int index=s.find("(");
if(index>=0){//循环小数
int lindex=s.find(")");
int dotindex=s.find(".");
int f_len=lindex-1-index;//循环小数部分长度
int int_len=index-1-dotindex;//整数部分长度 可以是0
string new_s=s.substr(index+1,f_len);//循环小数部分
int down=pow(10,f_len)-1;//分母
//先求循环小数部分的分子
int up;//分子
sscanf(&new_s[0],"%d",&up);//注意&new_s[0]的写法!!
if(int_len>0){
string int_part=s.substr(dotindex+1,int_len);//整数部分
//加上整数部分后的分子(假分数的分子)
up+=atof(&int_part[0])*down;//atoi不行?
//纯小数
down*=pow(10,int_len);
}
//约分
int c=gcd(up,down);
up/=c;
down/=c;
cout<<up<<"/"<<down<<endl;
}
else{//普通小数
int dotindex=s.find(".");
int len=s.size();
int ten=len-1-dotindex;
int down=pow(10,ten);
//float t;
//sscanf(&s[0],"%f",&t);
//int up=t*down;
int up=atof(&s[0])*down;//atof 与 sscanf both ok
int c=gcd(up,down);
up/=c;
down/=c;
cout<<up<<"/"<<down<<endl;
}
}
return 0;
}