题解分享
题解分享简介
好数(编程题) - 题解
位运算写法
```cpp
#include <iostream>
using namespace std;
using ll = long long;
int main() {
int res = 0;
int n = 0;
cin >> n;
for (int i = 1; i <= n; i += 2) {
bool tag = 1;
int x = i;
while (x) {
if ((x & 1) ^ tag)
goto End;
tag ^= 1;
x /= 10;
}
++res;
End:
;
}
cout << res << '\n';
return 0;
}
```
查看全文
0
0
1
0
好数(编程题) - 题解
```
暴力
#include <iostream>
using namespace std;
int a[10001];
int ans=0;
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
if(i<10&&i%2!=0)
ans++;
if(i>=10&&i<100&&(i%10)%2!=0&&(i/10)%2==0)
ans++;
if(i>=100&&i<1000&&i%10%2!=0&&i%100/10%2==0&&i/100%2!=0)
ans++;
if(i>=1000&&i<10000&&i%10%2!=0&&i%100/10%2==0&&i%1000/100%2!=0&&i/1000%2==0)
ans++;
if(i>=10000&&i<100000&&i%10%2!=0&&i%100/10%2==0&&i%1000/100%2!=0&&i%10000/1000%2==0&&i/10000%2!=0)
ans++;
if(i>=100000&&i<1000000&&i%10%2!=0&&i%100/10%2==0&&i%1000/100%2!=0&&i%10000/1000%2==0&&i%100000/10000%2!=0&&i/100000%2==0)
ans++;
if(i>=1000000&&i<10000000&&i%10%2!=0&&i%100/10%2==0&&i%1000/100%2!=0&&i%10000/1000%2==0&&i%100000/10000%2!=0&&i%1000000/100000%2==0&&i/1000000%2!=0)
ans++;
}
cout<<ans;
return 0;
}
```
查看全文
0
0
3
0
好数(编程题) - 题解
```
#include <bits/stdc++.h>
using namespace std;
int main(){
int n,ans=0;
cin>>n;
for(int i=1;i<=n;i+=2){
string s=to_string(i);
int len=s.size();
int k=0;
for(int i=len-1;i>=0;i--,++k){
if((s[i]%2!=0&&k%2==0)||(s[i]%2==0&&k%2!=0)) continue;
else break;
//TO
}
if(k==len) ans++;
}
printf("%d",ans);
return 0;
}
```
一种方法
查看全文
0
0
2
0
好数(编程题) - 题解
```
#include <bits/stdc++.h>
using namespace std;
int n;
int main() {
cin >> n;
int cnt = 0;
for (int i = 1; i <= n; i++) {
int v = i;
int u = 1;
int f = 1;
do {
if (v % 2 != u)
f = 0;
if (u == 1)
u = 0;
else
u = 1;
} while (v /= 10);
if (f)
cnt++;
}
cout << cnt << endl;
return 0;
}
```
查看全文
0
0
1
0
好数(编程题) - 题解
签到题就不多说了,献上第一份我的弱鸡题解。
```
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a;cin >> a;//输入的整数
int ans = 0;//输出的结果
for(int i = 1;i <= a; ++ i)//枚举从1到a所有数
{
int cnt = 1;//用来记录奇数位还是偶数位
int flag = 1;//用来判断是否好数的标记
int rec = i;//获得i值取出各个位置的值进行判断
while(rec)
{
int j = rec % 10;//取出最后一位
if(cnt % 2 == 1)//若奇数位
{
if(j % 2 != 1)//若奇数位不为奇数
{
flag = 0;
}
}
if(cnt % 2 == 0)//若偶数位
{
if(j % 2 != 0)//若偶数位不为偶数
{
flag = 0;
}
}
cnt ++;//第几位增加(从后往前)
rec /= 10;//去掉最后一位
}
if(flag == 1)ans ++;//判断是否好数
}
cout << ans;//输出答案
return 0;
}
```
查看全文
0
0
1
0
好数(编程题) - 题解
```
#include<bits/stdc++.h>
using namespace std;
#define int long long
int n;
bool check(int x){
int flag=1;
while(x>0){
if(flag==1){
if(x%2==0) return false;
else{
flag=0;
x=x/10;
}
}else if(flag==0){
if(x%2==1) return false;
else{
flag=1;
x=x/10;
}
}
}
return true;
}
signed main(){
cin>>n;
int ans=0;
for(int i=1;i<=n;i++){
if(check(i)){
ans++;
}
if(i>10&&(i/10)%2==1&&!check(i)){
i+=9;
}
}
cout<<ans;
return 0;
}
```
查看全文
0
0
0
4
好数(编程题) - 题解
```
#include <iostream>
using namespace std;
int main() {
long long N;
cin >> N;
int cnt = 0;
for (int i = 1; i <= N; i++) {
int num = i;
bool flag = true;
bool isGood = true;
while (num != 0) {
int a = num % 10;
num /= 10;
if (flag) {
if (a % 2 != 1) {
isGood = false;
break;
}
} else {
if (a % 2 != 0) {
isGood = false;
break;
}
}
flag = !flag;
}
if (isGood) cnt++;
}
cout << cnt << endl;
return 0;
}
```
查看全文
0
0
0
0
好数(编程题) - 题解
```
#include <bits/stdc++.h>
using namespace std;
bool isValid(int num) {
string s = to_string(num);
int len = s.length();
int k = 0;
for (int i = len - 1; i >= 0; i--, ++k)
{
int digit = s[i] - '0';
if (k % 2 == 0 && digit % 2 == 0) return false;
if (k % 2 == 1 && digit % 2 == 1) return false;
}
return true;
}
int main()
{
int N;
cin >> N;
int count = 0;
for (int i = 1; i <= N; i++)
{
if (isValid(i)) count++;
}
cout << count << endl;
return 0;
}
```
查看全文
0
0
0
0
好数(编程题) - 题解
```
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
using ll = long long;
using ULL = unsigned long long;
const int N = 1e7+5;
int n;
bool check(int x) {
int cnt = 1;
while (x) {
int a = x % 10;
if (cnt & 1) {
if (a % 2 == 0) return false;
}else {
if (a & 1) return false;
}
x /= 10;
cnt++;
}
return true;
}
inline void solve() {
cin >> n;
int ans = 0;
for (int i = 1; i <= n; i++) {
if (check(i))
ans++;
}
cout << ans << endl;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int _ = 1;
//int _; cin >> _;
while (_--) solve();
return 0;
}
```
查看全文
0
0
0
0
好数(编程题) - 题解
笨蛋解法
```
#include<iostream>
using namespace std;
int main() {
int N;
int number = 0;
cin >> N;
for (int k = 1; k <=N; k++)
{
if (k % 2)
{
if (k >= 1 && k <= 9) {
number++;
}
if (k / 10 >= 1 && k / 10 <= 9) {
int ten = k / 10;
int temp = ten % 2;
if (!temp) {
number++;
}
}
if (k / 100 >= 1 && k / 100 <= 9) {
int hunder = k / 100;
int ten = (k / 10)%10;
int temp = ten % 2;
if (hunder % 2) {
if (!temp) {
number++;
}
}
}
if (k / 1000 >= 1 && k / 1000 <= 9) {
int t = k/ 1000;
int hunder = (k / 100) % 10;
int ten = (k / 10) % 10;
if (t % 2 == 0) {
if (hunder % 2) {
if (ten % 2 == 0) {
number++;
}
}
}
}
if (k / 10000 >= 1 && k / 10000 <= 9) {
int wan = k / 10000;
int t = (k / 1000) % 10;
int hunder = (k / 100) % 10;
int ten = (k / 10) % 10;
if (wan % 2) {
if (t % 2 == 0) {
if (hunder % 2) {
if (ten % 2 == 0) {
number++;
}
}
}
}
}
if (k / 100000 >= 1 && k / 100000 <= 9) {
int tenwan = k / 100000;
int wan = (k / 10000)%10;
int t = (k / 1000) % 10;
int hunder = (k / 100) % 10;
int ten = (k / 10) % 10;
if (tenwan % 2 == 0) {
if (wan % 2) {
if (t % 2 == 0) {
if (hunder % 2) {
if (ten % 2 == 0) {
number++;
}
}
}
}
}
}
if (k / 1000000 >= 1 && k / 1000000 <= 9) {
int hwan = k / 1000000;
int tenwan = (k / 100000) % 10;
int wan = (k / 10000) % 10;
int t = (k / 1000) % 10;
int hunder = (k / 100) % 10;
int ten = (k / 10) % 10;
if (hwan % 2) {
if (tenwan % 2 == 0) {
if (wan % 2) {
if (t % 2 == 0) {
if (hunder % 2) {
if (ten % 2 == 0) {
number++;
}
}
}
}
}
}
}
if (k / 10000000 >= 1 && k / 10000000 <= 9) {
int twan = k / 10000000;
int hwan = (k / 1000000)%10;
int tenwan = (k / 100000) % 10;
int wan = (k / 10000) % 10;
int t = (k / 1000) % 10;
int hunder = (k / 100) % 10;
int ten = (k / 10) % 10;
if (twan % 2 == 0) {
if (hwan % 2) {
if (tenwan % 2 == 0) {
if (wan % 2) {
if (t % 2 == 0) {
if (hunder % 2) {
if (ten % 2 == 0) {
number++;
}
}
}
}
}
}
}
}
}
}
cout << number;
return 0;
}
```
查看全文
0
0
0
0



