## 2020新星赛b题(注意读题还有就是小数据试了去试大数据)
/*
B. Shooting
The girl Kaguya is standing in a bar and starring at Miyuki. She wants to shoot Miyuki with her heart arrow but she needs to know the distance between them to make her shot accurate.
The problem is that she wrote it in scientific notation. The scientific notation of some real number x is the notation of form AeB, where A is a real number and B is an integer and x=A∗10
B
is true. In our case A is between 0 and 9 and B is non-negative.
So she asked you to tell her the distance value in usual decimal representation with minimal number of digits after the decimal point (and no decimal point if it is an integer). See the output format for better understanding.
输入格式:
There are multiple test cases. The first line of input contains an integer T indicating the number of test cases (1≤T≤10). Each test case contains only one line of a single string of form a.deb where a, d and b are integers and e is usual character 'e' (0≤a≤9, 0≤d≤10
100
, 0≤b≤100) — the scientific notation of the desired distance value.
a and b contain no leading zeros and d contains no trailing zeros (but may be equal to 0). Also, b can not be non-zero if a is zero.
输出格式:
For each test case print the real number x (the desired distance value) in a single line in its decimal notation.
Thus if x is an integer, print it's integer value without decimal part and decimal point and without leading zeroes.
Otherwise print x in a form of p.q such that p is an integer that have no leading zeroes (but may be equal to zero), and q is an integer that have no trailing zeroes (and may not be equal to zero).
输入样例:
3
8.549e2
8.549e3
0.33e0
输出样例:
854.9
8549
0.33
*/
#include<stdio.h>
#include<string.h>
char a[120];
char dianqian[120],ehou[120],zhongjian[120];
int main()
{
int n;
scanf("%d",&n);
for(int j=1;j<=n;j++)
{
getchar();
/*
memset(ehou,'\0',sizeof(ehou));*/
int dian,e;
scanf("%s",a);
int shu=strlen(a)-1;
for(int i=0;i<=shu;i++)
{
if(a[i]=='.') dian=i;
if(a[i]=='e') e=i;
}
for(int i=0;i<=dian-1;i++) dianqian[i]=a[i];
dianqian[dian]='\0';
for(int i=dian+1;i<=e-1;i++) zhongjian[i-dian-1]=a[i];
zhongjian[e-dian-1]='\0';
for(int i=e+1;i<=shu;i++) ehou[i-e-1]=a[i];
ehou[shu+1-e-1]='\0';
int num1=0;
int num2=e-dian-1;
int num3;
sscanf(ehou,"%d",&num3);
if(dianqian[0]=='0')
{
if(num2==1&&zhongjian[0]=='0'&&num3==0) putchar(dianqian[0]);
else
{
putchar('0');
putchar('.');
for(int i=0;i<=num2-1;i++) putchar(zhongjian[i]);
}
}
else
{
if(num3>=num2)
{
putchar(dianqian[0]);
int cha=num3-num2;
for(int i=0;i<=num2-1;i++) putchar(zhongjian[i]);
for(int i=1;i<=cha;i++) putchar('0');
}
else
{
if(num2==1&&zhongjian[0]=='0'&&num3==0) putchar(dianqian[0]);
else
{
if(num3==0)
{
putchar(dianqian[0]);
putchar('.');
for(int i=0;i<=num2-1;i++) putchar(zhongjian[i]);
}
else
{
putchar(dianqian[0]);
for(int i=0;i<=num2-1;i++)
{
putchar(zhongjian[i]);
if(i==num3-1) putchar('.');
}
}
}
}
}
putchar('\n');
}
return 0;
}
相当经典的科学计数法(应该说是比较恶心的题目)
我们需要前缀零后缀零小数整数是否为零的判断
/*
1060 Are They Equal (25分)
If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered equal since they are both saved as 0.123×10
5
with simple chopping. Now given the number of significant digits on a machine and two float numbers, you are supposed to tell if they are treated equal in that machine.
Input Specification:
Each input file contains one test case which gives three numbers N, A and B, where N (<100) is the number of significant digits, and A and B are the two float numbers to be compared. Each float number is non-negative, no greater than 10
100
, and that its total digit number is less than 100.
Output Specification:
For each test case, print in a line YES if the two numbers are treated equal, and then the number in the standard form 0.d[1]...d[N]*10^k (d[1]>0 unless the number is 0); or NO if they are not treated equal, and then the two numbers in their standard form. All the terms must be separated by a space, with no extra space at the end of a line.
Note: Simple chopping is assumed without rounding.
Sample Input 1:
3 12300 12358.9
Sample Output 1:
YES 0.123*10^5
Sample Input 2:
3 120 128
Sample Output 2:
NO 0.120*10^3 0.128*10^3
此乃错误做法
*/
/*#include<iostream>
#include<stdio.h>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
int n;
string b, c;
cin >> n >> b >> c;
getchar();
int kb, kc;
int flag1 = 0, flag2 = 0;
if (b.find(".") != string::npos)
{
int dianb = b.find(".");
if (dianb == 1 && b[0] == '0')
{
b.erase(0, 2);
int tempb = b.size();
int jishub=0;
int flagb = 0;
for (int i = 0; i <= b.size() - 1; i++)
{
if (b[i] == '0') jishub++;
else
{
flagb = 1;
break;
}
}
b.erase(0, jishub);
if(flagb==1) kb = -jishub;
else kb = 0;
}
else
{
kb = dianb;
b.erase(dianb, 1);
}
}
else
{
if (b == "0")
{
kb = 0;
flag1 = 1;
}
else kb = b.size();
}
if (c.find(".") != string::npos)
{
int dianc = c.find(".");
if (dianc == 1 && c[0] == '0')
{
c.erase(0, 2);
int tempc = c.size();
int jishuc = 0;
int flagc = 0;
for (int i = 0; i <= c.size() - 1; i++)
{
if (c[i] == '0') jishuc++;
else
{
flagc = 1;
break;
}
}
c.erase(0, jishuc);
if(flagc==1) kc = -jishuc;
else kc = 0;
}
else
{
kc = dianc;
c.erase(dianc, 1);
}
}
else
{
if (c == "0")
{
kc = 0;
flag2 = 1;
}
else kc = c.size();
}
int bshu, cshu;
if (flag1 == 1) bshu = b.size() - 1;
else bshu = b.size();
if (flag2 == 1) cshu = c.size() - 1;
else cshu = c.size();
for (int i = 1; i <= n - bshu; i++) b += "0";
for (int i = 1; i <= n - cshu; i++) c += "0";
string bans = "0." + b.substr(0, n) + "*10^" + to_string(kb);
string cans = "0." + c.substr(0, n) + "*10^" + to_string(kc);
if (cans == bans) cout << "YES" << " " << bans;
else cout << "NO" << " " << bans << " " << cans;
return 0;
}
参考liouchuo大佬代码
*/
#include <iostream>
#include <cstring>
#include<stdio.h>
using namespace std;
int main() {
int n, p = 0, q = 0;
char a[10000], b[10000], A[10000], B[10000];
scanf("%d%s%s", &n, a, b);
int cnta = strlen(a), cntb = strlen(b);
for (int i = 0; i < strlen(a); i++) {
if (a[i] == '.') {
cnta = i;
break;
}
}
for (int i = 0; i < strlen(b); i++) {
if (b[i] == '.') {
cntb = i;
break;
}
}
int indexa = 0, indexb = 0;
while (a[p] == '0' || a[p] == '.') p++;
while (b[q] == '0' || b[q] == '.') q++;
if (cnta >= p)
cnta = cnta - p;
else
cnta = cnta - p + 1;
if (cntb >= q)
cntb = cntb - q;
else
cntb = cntb - q + 1;
if (p == strlen(a))
cnta = 0;
if (q == strlen(b))
cntb = 0;
while (indexa < n) {
if (a[p] != '.' && p < strlen(a))
A[indexa++] = a[p];
else if (p >= strlen(a))
A[indexa++] = '0';
p++;
}
while (indexb < n) {
if (b[q] != '.' && q < strlen(b))
B[indexb++] = b[q];
else if (q >= strlen(b))
B[indexb++] = '0';
q++;
}
if (strcmp(A, B) == 0 && cnta == cntb)
printf("YES 0.%s*10^%d", A, cnta);
else
printf("NO 0.%s*10^%d 0.%s*10^%d", A, cnta, B, cntb);
return 0;
}