YT15-HDU-字串的模擬
Problem Description
As you know, when you want to hack someone's program, you must submit your test data. However sometimes you will submit invalid data, so we need a data checker to check your data. Now small W has prepared a problem for BC, but he is
too busy to write the data checker. Please help him to write a data check which judges whether the input is an integer ranged from a to b (inclusive).
Note: a string represents a valid integer when it follows below rules.
1. When it represents a non-negative integer, it contains only digits without leading zeros.
2. When it represents a negative integer, it contains exact one negative sign ('-') followed by digits without leading zeros and there are no characters before '-'.
3. Otherwise it is not a valid integer.
Note: a string represents a valid integer when it follows below rules.
1. When it represents a non-negative integer, it contains only digits without leading zeros.
2. When it represents a negative integer, it contains exact one negative sign ('-') followed by digits without leading zeros and there are no characters before '-'.
3. Otherwise it is not a valid integer.
Input
Multi test cases (about 100), every case occupies two lines, the first line contain a string which represents the input string, then second line contains a and b separated by space. Process to the end of file.
Length of string is no more than 100.
The string may contain any characters other than '\n','\r'.
-1000000000$\leq a \leq b \leq 1000000000$
Length of string is no more than 100.
The string may contain any characters other than '\n','\r'.
-1000000000$\leq a \leq b \leq 1000000000$
Output
For each case output "YES" (without quote) when the string is an integer ranged from a to b, otherwise output "NO" (without quote).
Sample Input
10 -100 100 1a0 -100 100
Sample Output
YES NO
程式碼如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
int main()
{
char str[200],cp[200];
int a,b;
while(gets(str))
{
cin>>a>>b;
getchar();
int x=atoi(str);//把字串轉換成長整型數的一個函式,標頭檔案是#include <cstdlib>
sprintf(cp,"%d",x);//字串格式化命令,主要功能是把格式化的資料寫入某個字串中,標頭檔案是#include <cstdlib>
if(strcmp(str,cp)==0 && x>=a && x<=b)//比較函式,標頭檔案是#include <cstring>
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}
執行結果:
程式碼不是我寫的,問了一個做了的同學才知道大神都是用函式庫做題的,繼續努力吧!!!
相關文章
- 垂直柱狀圖(模擬+字串)字串
- 模擬實現字串函式strlen , strcpy ,strcmp字串函式
- PAT-B 1024 科學計數法【模擬+字串】字串
- hduoj1002 A + B Problem II (大數相加 字串模擬)字串
- c語言與字串相關的庫函式的模擬實現C語言字串函式
- 模擬實現不受限制的字串函式--C語言版字串函式C語言
- YC322A [ 20240724 CQYC NOIP 模擬賽 T3 ] 小 M 的字串(string)字串
- 模擬Promise的功能Promise
- 模擬
- 10.6 模擬賽(NOIP 模擬賽 #9)
- 2024.11.20 NOIP模擬 - 模擬賽記錄
- modelsim 獨立模擬vivado的IP核及模擬指令碼指令碼
- 有限元模擬 有限體積模擬
- CMRR的模擬(原理版)
- promise的模擬實現Promise
- 模擬supervisor的程式管理
- git 模擬Git
- ACP模擬
- 模擬題
- 模擬賽
- Altair SimSolid 工程模擬軟體 衡祖模擬AISolid
- Gpssworld模擬(二):並排排隊系統模擬
- 從“模擬”的角度看,《微軟模擬飛行》還需要什麼?微軟
- 小車側方位停車過程的動態模擬matlab模擬Matlab
- 簡單的模擬(洛谷)
- javascript模擬new的實現JavaScript
- JavaScript 模擬new的實現JavaScript
- 「模擬賽」暑期集訓CSP提高模擬15(8.7)
- 「模擬賽」暑期集訓CSP提高模擬3(7.20)
- 「模擬賽」暑期集訓CSP提高模擬5(7.22)
- 「模擬賽」暑期集訓CSP提高模擬6(7.23)
- 「模擬賽」暑期集訓CSP提高模擬10(7.28)
- mumu模擬器 MuMuManager.exe是MuMu模擬器12新加入的工具
- 5.4 模擬賽
- 2024.2.18 模擬賽
- 2024.1.26 模擬賽
- 2024.1.23 模擬賽
- 2024.3.30 模擬賽
- 2024.3.17 模擬賽