13——1212,1312,1049

总结一下今天的题目:13——1212,1312,1049 - 蜗牛 - 追梦少年

http://acm.hdu.edu.cn/showproblem.php?pid=1212

Problem Description

As we know, Big Number is always troublesome. But it's really important in our ACM. And today, your task is to write a program to calculate A mod B.
To make the problem easier, I promise that B will be smaller than 100000.
Is it too hard? No, I work it out in 10 minutes, and my program contains less than 25 lines.

 

Input

The input contains several test cases. Each test case consists of two positive integers A and B. The length of A will not exceed 1000, and B will be smaller than 100000. Process to the end of file.

 

Output

For each test case, you have to ouput the result of A mod B.

 

Sample Input

2 3 12 7 152455856554521 3250

 

Sample Output

2 5 1521


此题为大数取模运算:x%n,x为一个一千位以内的大数,n为一个int整形数

数论中有个关于公式模运算的公式:


(a+b)%n=((a%n)+(b%n))%n

例 12%m=(1*10+2)%m=(1*10%m+2%m)%m;

123%m=(12*10+3)%m=(12*10%m+3%m)%m;

123456789%m=((((1*10%m+2%m)%m+.......

#include<iostream>
#include<string>
using namespace std;
int mod(string s,int n)
{
int num=0;
for(int i=0;i<s.length();i++)
num=(num*10%n+s[i]-'0')%n;
return num;
}
int main()
{
string s;int n;
while(cin>>s>>n)
cout<<mod(s,n)<<endl;
return 0;
}


http://acm.hdu.edu.cn/showproblem.php?pid=1312

//DFS搜索题1312

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define N 30
char matrix[N][N];
int vis[N][N];
int position[4][2]={{-1,0},{1,0},{0,1},{0,-1}};
int w,h,num;
void DFS(int x,int y)
{
if(x<0||x>=h||y<0||y>=w) //DFS结束的条件,判断是否出界
return;
int xx,yy;
for(int i=0;i<4;i++)
{
xx=x+position[i][0];
yy=y+position[i][1];
if(xx<0||yy<0||xx>=h||yy>=w)
continue;
if(matrix[xx][yy]=='#')
continue;
if(matrix[xx][yy]=='.')
{
//cout<<"sdagjlsd;"<<endl;
if(!vis[xx][yy])
{
vis[xx][yy]=1;/*标记访问过的节点为1,但是第一个节点没有标记,
也就是下面为什么把访问过的节点更新为’#‘;*/
num++;
matrix[xx][yy]='#';//把访问过的节点改为‘#’,否则会进入死循环
}
DFS(xx,yy);
}
}
}
int main()
{
int i,j;
while(cin>>w>>h,w&&h)
{
getchar();
num=1;
int x,y;
memset(vis,0,sizeof(vis));
for(i=0;i<h;i++)
for(j=0;j<w;j++)
{
cin>>matrix[i][j];
if(matrix[i][j]=='@')//输入的同时进行查找
{
x=i;y=j;
}
}
DFS(x,y);
cout<<num<<endl;
}
return 0;
}


http://acm.hdu.edu.cn/showproblem.php?pid=1049

计算题:深度n,每分钟爬u,每分钟下滑d

虫子每次用一分钟爬,一分钟休息,所以每次耗费的是两分钟,但由于最后一次虫子不需要休息,

所以用x=(n-u)%(u-d);如果x==0,time=(n-u)/(u-d)*2+1;否则time=((n-u)/(u-d)+1)*2+1,因为不能整除的话,时间仍按一分钟来计算

代码很简单:

#include<stdio.h>
int main()
{
int n,u,d;
while(scanf("%d%d%d",&n,&u,&d)&&n)
{
int t,x=n-u,y=u-d;
if(!(x%y))
t=x/y*2+1;
else
t=(x/y+1)*2+1;
printf("%d\n",t);
}
return 0;
}



评论

© 现实给了梦想多少时间 | Powered by LOFTER