水题小技巧
2016-05-12 17:35
190 查看
Digital Roots
题一:题目链接:
http://acm.hdu.edu.cn/diy/contest_showproblem.php?pid=1009&cid=29312
题意:将一个多位数的各个位上的数字求和,直到这个和是一个个位数。
Problem Description
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 summedand 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.
Input
The input file will contain a list of positive integers, one per line. The end of the input will be indicated by an integer value of zero.Output
For each integer in the input, output its digital root on a separate line of the output.Sample Input
24 39 0
Sample Output
6 3
Source
代码:#include <iostream>
#include<cstdio>
#include<string.h>
using namespace std;
int main()
{
int n,sum;
while(scanf("%d",&n))
{
if(n==0)
break;
while(n>=10)
{
sum=0;
while(n!=0)
{
sum+=n%10;
n/=10;
}
n=sum;
}
cout<<sum<<endl;
}
return 0;
}
题二:
题目链接:
http://acm.hdu.edu.cn/diy/contest_showproblem.php?pid=1004&cid=29312
Rightmost Digit
题/*末位数 相乘后的末位数
1 1
2 4 8 6 2
3 9 7 1 3
4 6 4
5 5
6 6
7 9 3 1 7
8 4 2 6 8
9 1 9(由于x9是奇数,所以1不会出现)
0 0意:N^N的末位数字,规律题,最大循环是4
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 67 Accepted Submission(s) : 18
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
Given a positive integer N, you should output the most right digit of N^N.Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.Each test case contains a single positive integer N(1<=N<=1,000,000,000).
Output
For each test case, you should output the rightmost digit of N^N.Sample Input
2 3 4
Sample Output
7 6
Hint
In the first case, 3 * 3 * 3 = 27, so the rightmost digit is 7.In the second case, 4 * 4 * 4 * 4 = 256, so the rightmost digit is 6.
Author
代码:#include <iostream>
#include<cstdio>
#include<string.h>
using namespace std;
int main()
{
int t,n,b;
cin>>t;
while(t--)
{
cin>>n;
b=n%10;
int m=1;
for(int i=0;i<=(n-1)%4;i++)///末位数字最大循环是4
{
m*=b;
if(m>10)
m%=10;
}
cout<<m<<endl;
}
return 0;
}
题三:
改革春风吹满地
题意:求多边形的面积可以先根据逆时针给出的三个顶点求三角形的面积
三点为A(a,b),B(c,d),C(e,f)
面积公式s=(a*d+b*e+c*f-a*f-b*c-d*e);
一重循环即可
题目链接:
http://acm.hdu.edu.cn/diy/contest_showproblem.php?pid=1005&cid=29312
代码:
#include <iostream>
#include<cstdio>
#include<string.h>
using namespace std;
double area(int a,int b,int c,int d,int e,int f)
{
return (a*d+b*e+c*f-a*f-b*c-d*e)*0.5;
}
int main()
{
int t;
int x[100],y[100];
while(scanf("%d",&t))
{
if(t==0)
break;
double s=0;
for(int i=0;i<t;i++)
cin>>x[i]>>y[i];
for(int i=3;i<=t;i++)
s+=area(x[0],y[0],x[i-2],y[i-2],x[i-1],y[i-1]);
cout<<s<<endl;
}
return 0;
}
相关文章推荐
- mac下 ffmpeg 编译到android平台(支持 frei0r)
- React实战——基于百度IFE学院task50
- uibutton 文字闪动问题
- 22java中使用UDP TCP 协议做聊天工具
- 第7周 深入理解计算机系统(第二版) 知识总结 第一、二章
- iOS开发设置UISearchBar的输入背景框、提示文字等
- 一个前端练手作品——前端新人如何求职?
- 选择排序法——1到1000
- python中if __name__ == '__main__': 的解析
- Sublime 文本或者代码编辑软件的使用
- 排序
- 禁用ecshop缓存,关闭ecshop缓存功能
- HTML5 音频播放 <audio>
- nyoj 743 复杂度(排列组合)
- radiobutton+viewpager+Fragment切换过程取消预加载(延迟加载)
- sql server 2008学习(二)基本查询
- IOS地图定位坐标偏差解决方案(实战通过)
- 使用distcp并行拷贝大数据文件
- JavaScript实现加法运算例子(2种方法)
- mysql 带参数的游标