目录
求余(%)运算符 《王道机试指南》
取最后n位数字 --> 该数字除以10^n的余数
还是A+B
题目描述
读入两个小于10000的正整数A和B,计算A+B。需要注意的是:如果A和B的末尾K(不超过8)位数字相同,请直接输出-1。
输入描述:
测试输入包含若干测试用例,每个测试用例占一行,格式为"A B K",相邻两数字有一个空格间隔。当A和B同时为0时输入结束,相应的结果不要输出。
输出描述:
对每个测试用例输出1行,即A+B的值或者是-1。
示例1
输入
1 2 1 11 21 1 108 8 2 36 64 3 0 0 1
输出
3 -1 -1 100
AC代码:
#include<iostream>
#include <cmath>
using namespace std;
int a,b,k,c;
int main(){
while(cin>>a>>b>>k){
if(a==0 && b==0) break;
int t=1;
while(k--) t=t*10; //除以10的k次方的余数
if( a%t==b%t ) {
cout<<-1<<endl;
continue;
}
c=a+b;
cout<<c<<endl;
}
return 0;
}
守形数
题目描述
守形数是这样一种整数,它的平方的低位部分等于它本身。 比如25的平方是625,低位部分是25,因此25是一个守形数。 编一个程序,判断N是否为守形数。
输入描述:
输入包括1个整数N,2<=N<100。
输出描述:
可能有多组测试数据,对于每组数据, 输出"Yes!”表示N是守形数。 输出"No!”表示N不是守形数。
示例1
输入
复制
25 4
输出
复制
Yes! No!
计算位数和求余取后几位数字
AC代码:
#include<iostream>
using namespace std;
//平方的低位部分等于它本身 25^2=625
int main(){
int n;
int cnt,t,r;
while(cin>>n){
//不用判断0 因为 2<=N<100
//计算n的位数
cnt=0;
t=n;
do{
cnt++;
t=t/10;
}while(t!=0);
r=n*n; //计算n的平方
t=1;
while(cnt--) t=t*10;
int re=r%t;
if(re==n) cout<<"Yes!"<<endl;
else cout<<"No!"<<endl;
}
return 0;
}
数位拆解 《王道机试指南》
特殊乘法
题目描述
写个算法,对2个小于1000000000的输入,求结果。 特殊乘法举例:123 * 45 = 1*4 +1*5 +2*4 +2*5 +3*4+3*5
输入描述:
两个小于1000000000的数
输出描述:
输入可能有多组数据,对于每一组数据,输出Input中的两个数按照题目要求的方法进行运算后得到的结果。
示例1
输入
123 45
输出
54
位数按照从小到大依次存入buf[20]数组
AC代码:
#include <iostream>
#include <string.h>
using namespace std;
int a,b,res;
int buf1[20],buf2[20];
int size1,size2;
int main(){
while(cin>>a>>b){
memset(buf1,0,sizeof(buf1));
memset(buf2,0,sizeof(buf2));
size1=size2=0;
while(a!=0){
buf1[size1++]=a%10;
a=a/10;
}
while(b!=0){
buf2[size2++]=b%10;
b=b/10;
}
int ans=0;
for(int i=0;i<size1;i++){
for(int j=0;j<size2;j++){
ans+=buf1[i]*buf2[j];
}
}
cout<<ans<<endl;
}
return 0;
}
反序数
题目描述
设N是一个四位数,它的9倍恰好是其反序数(例如:1234的反序数是4321)
求N的值
输入描述:
程序无任何输入数据。
输出描述:
输出题目要求的四位数,如果结果有多组,则每组结果之间以回车隔开。
AC代码:
#include<iostream>
using namespace std;
int main(){
//遍历所有的四位数
int buf[10],size;
for(int i=1000;i<=9999;i++){
//数位拆解;
size=0;
int t=i;
while(t!=0){
buf[size++]=t%10;
t=t/10;
}
//计算得到的反序数
int res=0;
for(int j=0;j<size;j++) res = res*10+buf[j];
if(res==i*9){
cout<<i<<endl;
}
}
}
对称平方数
题目描述
打印所有不超过n(n<256)的,其平方具有对称性质的数。如11*11=121。
输入描述:
无
输出描述:
每行一个数,表示对称平方数。
AC代码:不懂0为啥不算
#include<iostream>
using namespace std;
int main(){
for(int i=1;i<=3;i++) cout<<i<<endl; //一位
for(int i=10;i<256;i++){
//数位拆解
int buf[10],size=0;
int t=i*i;
while(t!=0){
buf[size++]=t%10;
t=t/10;
}
bool flag=true;
for(int j=0;j<size/2;j++){
if(buf[j]!=buf[size-j-1]) {
flag= false;
break;
}
}
if(flag== true)
cout<<i<<endl;
}
return 0;
}
Digital Root
题目描述
The digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root. If the resulting value contains two or more digits, those digits are summed and the process is repeated. This is continued as long as necessary to obtain a single digit. For example, consider the positive integer 24. Adding the 2 and the 4 yields a value of 6. Since 6 is a single digit, 6 is the digital root of 24. Now consider the positive integer 39. Adding the 3 and the 9 yields 12. Since 12 is not a single digit, the process must be repeated. Adding the 1 and the 2 yeilds 3, a single digit and also the digital root of 39.
输入描述:
The input file will contain a list of positive integers, one per line. The integer may consist of a large number of digits.
输出描述:
For each integer in the input, output its digital root on a separate line of the output.
示例1
输入
24 39
输出
6 3
还是利用数位拆解的题目,注意拆解的时候初始化size
AC代码:
#include<iostream>
using namespace std;
int main(){
int n;
while(cin>>n){
int t=n;
int a;
int buf[200],size=0;
while((t/10)!=0)
{
//数位拆解
int m=t;
size=0;
while(m!=0){
buf[size++]=m%10;
m=m/10;
}
t=0;
for(int i=0;i<size;i++){
t+=buf[i];
}
}
cout<<t<<endl;
}
return 0;
}