poj 3077Rounders(模拟)

转载请注明出处:

viewmode=contents">http://blog.csdn.net/u012860063?

viewmode=contents

题目链接:http://poj.org/problem?

id=3077

Description

For a given number, if greater than ten, round it to the nearest ten, then (if that result is greater than 100) take the result and round it to the nearest hundred, then (if that result is greater than 1000) take that number and round it to the nearest thousand,
and so on ...

Input

Input to this problem will begin with a line containing a single integer n indicating the number of integers to round. The next n lines each contain a single integer x (0 <= x <= 99999999).

Output

For each integer in the input, display the rounded integer on its own line.

Note: Round up on fives.

Sample Input

9
15
14
4
5
99
12345678
44444445
1445
446

Sample Output

20
10
4
5
100
10000000
50000000
2000
500

代码一、例如以下:

#include <iostream>
#include <cstring>
using namespace std;
int main()
{
	int t, n, k, count;
	char s[17];
	int i, j;
	while(cin >>t)
	{
		while(t--)
		{
			count = 0;
			int p = 0, l = 0;;
			memset(s,0,sizeof(s));
			cin>>s;
			int len = strlen(s);
			if(len == 1)
			{
				cout<<s[0]<<endl;
				continue;
			}
			for(i = len-1; i > 0; i--)
			{
				if(s[i]-'0'+p > 4)
				{
					p = 1;
					count++;
				}
				else
				{
					p = 0;
					count++;
				}
			}
			if(s[0]-'0' + p > 9)
			{
				cout<<10;
			}
			else
			{
				cout<<s[0]-'0'+p;
			}
			for(i = 0; i < count; i++)
			{
				cout<<'0';
			}
			cout<<endl;
		}
	}
	return 0;
}

代码二、例如以下:

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
using namespace std;

int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        int n, count = 0;
        scanf("%d", &n);
        double x = n;
        while (x >= 10)
        {
            x /= 10;
            x = (int)(x + 0.5);
            count++;
        }
        n = (int)x;
        for (int i = 0; i < count; i++)
            n *= 10;
        printf("%d\n", n);
    }
    return 0;
}
时间: 2024-10-20 19:53:36

poj 3077Rounders(模拟)的相关文章

POJ 2420 模拟退火法

A Star not a Tree? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3272   Accepted: 1664 Description Luke wants to upgrade his home computer network from 10mbs to 100mbs. His existing network uses 10base2 (coaxial) cables that allow you

Shuffle&#39;m Up (poj 3087 模拟)

Language: Default Shuffle'm Up Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5968   Accepted: 2802 Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting

G - Shuffle&#39;m Up POJ 3087 模拟洗牌的过程,算作暴力搜索也不为过

G - Shuffle'm Up Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3087 Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by start

POJ 1016 模拟字符串

Numbers That Count Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20396   Accepted: 6817 Description "Kronecker's Knumbers" is a little company that manufactures plastic digits for use in signs (theater marquees, gas station price

POJ 1379 模拟退火法

Run Away Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5631   Accepted: 1728 Description One of the traps we will encounter in the Pyramid is located in the Large Room. A lot of small holes are drilled into the floor. They look complet

poj 3087 模拟

背景:bfs专题的题,可是直接模拟就好了啊. 思路:管件在于记录第一个s12串,当再次出现第一个s12串时说明进入了循环之中,不能呢达到目标状态. 学习:1.strcmp时要注意,该字符串的有效部分是不是以'\0'结尾的. #include<map> #include<set> #include<stack> #include<queue> #include<vector> #include<cstdio> #include<c

[poj]poj2632(模拟)

题意:模拟 模拟就行 #include<iostream> #include<cstdio> #include<stack> using namespace std; const int N=1e2+7; struct robot{ int id,x,y,to; }; const int dx[4]={0,1,0,-1}; const int dy[4]={1,0,-1,0}; int T,n,m,A,B,flag; int map[N][N]; robot rbt[N

POJ 1068-Parencodings(模拟)

Parencodings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20444   Accepted: 12303 Description Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in two different ways: q By an integer sequence P = p1 p2...pn

POJ 3077-Rounders(水题乱搞)

Rounders Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7697   Accepted: 4984 Description For a given number, if greater than ten, round it to the nearest ten, then (if that result is greater than 100) take the result and round it to th