原题: 3594. IP地址
sscanf
int sscanf ( const char * s, const char * format, ...);
http://www.cplusplus.com/reference/cstdio/sscanf/?kw=sscanf
#include <iostream>
using namespace std;
int main() {
string str;
while (cin >> str) {
int a, b, c, d;
sscanf(str.c_str(), "%d.%d.%d.%d", &a, &b, &c, &d);
if (a >= 0 && a <= 255 && b >= 0 && b <= 255 && c >= 0 && c <= 255 && d >= 0 && d <= 255) {
cout << "Yes!" << endl;
} else {
cout << "No!" << endl;
}
}
return 0;
}
#include <stdio.h>
int main () {
char sentence []="Rudolph is 12 years old";
char str [20];
int i;
sscanf (sentence,"%s %*s %d", str, &i);
printf ("%s -> %d\n", str, i);
return 0;
}
升级版 4088. 网络连接